Cod sursa(job #2014160)

Utilizator victoreVictor Popa victore Data 23 august 2017 00:07:16
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<cstdio>
#include<algorithm>

using namespace std;

const int nmax=200005;

struct muchie
{
    int x,y,ind;
    long long c1,c2;
}edge[nmax];

int n,m,h[nmax],tata[nmax],st[nmax],top;

inline int FindSet(int x)
{
    while(tata[x]!=x)
        x=tata[x];
    return x;
}

inline void UnionSet(int tx,int ty)
{
    if(h[tx] == h[ty])
    {
        tata[ty]=tx;
        h[tx]++;
    }
    else if(h[ty] > h[tx])
        tata[tx]=ty;
    else
        tata[ty]=tx;
}

inline bool cmp(const muchie a, const muchie b)
{
    if(a.c1 == b.c1)
        return a.c2 > b.c2;
    return a.c1 < b.c1;
}

int main()
{
    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);
    int i,j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;++i)
        scanf("%d%d%lld%lld",&edge[i].x,&edge[i].y,&edge[i].c1,&edge[i].c2),edge[i].ind=i;
    for(i=1;i<=n;++i)
        h[i]=1,tata[i]=i;
    sort(edge+1,edge+m+1,cmp);
    int tx,ty;
    for(i=1;i<=m&&top<n-1;++i)
    {
        tx=FindSet(edge[i].x);
        ty=FindSet(edge[i].y);
        if(tx != ty)
        {
            UnionSet(tx , ty);
            st[++top]=edge[i].ind;
        }
    }
    for(i=1;i<=top;++i)
        printf("%d\n",st[i]);
}