Cod sursa(job #1246597)

Utilizator DenisacheDenis Ehorovici Denisache Data 21 octombrie 2014 12:48:58
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <list>
//#include <windows.h>
//#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <limits.h>
#include <string>
#include <math.h>
using namespace std;
#define forA(V,it) for (typeof(V.begin()) it=V.begin();it!=V.end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define MOD 1000000007
#define INF (1<<31)-1
#define MINF -(1<<31)
#define vi vector <int>
#define vll vector <ll>
#define pii pair <int,int>
#define pll pair <ll,ll>
#define newl printf("\n")
#define DIM 100005
int N,M,X[200005],Y[200005],i,ind[200005],RG[200005],TT[200005];
ll efort[200005],cost[200005];
bool cmp(int a,int b)
{
    if (efort[a]==efort[b]) return cost[a]>cost[b];
    return efort[a]<efort[b];
}
vi ans;
int find(int x)
{
    int R;
    for (R=x;R!=TT[R];R=TT[R]);
    for (;x!=TT[x];)
    {
        int y=TT[x];
        TT[x]=R;
        x=y;
    }
    return R;
}
void unite(int x,int y)
{
    if (RG[x]>RG[y]) TT[y]=x;
    else TT[x]=y;
    if (RG[x]==RG[y]) RG[y]++;
}
int main()
{
    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);
    scanf("%d %d",&N,&M);
    for (i=1;i<=M;i++) scanf("%d %d %lld %lld",&X[i],&Y[i],&efort[i],&cost[i]),ind[i]=TT[i]=i,RG[i]=1;
    sort(ind+1,ind+M+1,cmp);
    for (i=1;i<=M;i++)
    {
        int poz=ind[i];
        int a=find(X[poz]),b=find(Y[poz]);
        if (a!=b)
        {
            unite(a,b);
            ans.pb(poz);
        }
    }
    for (vi::iterator it=ans.begin();it!=ans.end();it++)
        printf("%d\n",*it);
    return 0;
}