Cod sursa(job #543909)

Utilizator PavelRazvanPavel Razvan PavelRazvan Data 28 februarie 2011 18:45:02
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include<algorithm>
using namespace std;
#include<vector>

#define pb push_back
#define mp make_pair
#define poz first.first
#define x first.second.first
#define y first.second.second
#define c1 second.first
#define c2 second.second
#define DIM 200005

vector <pair <pair<int,pair<int,int> >,pair<int,int> > > lst;
int n,m,t[DIM],k,h[DIM];

struct cmp
{
    bool operator ()(const pair <pair<int,pair<int,int> >,pair<int,int> > &a,const pair <pair<int,pair<int,int> >,pair<int,int> > &b) const
    {
        return a.c1<b.c1 || (a.c1==b.c1 && a.c2>=b.c2);
    }
};


int find (int i)
{
    if(t[i]!=i)
        t[i]=find(t[i]);
    return t[i];
}
inline void unite (int i,int j)
{
    if(h[i]<h[j])
    {
        t[i]=j;
        h[j]+=h[i];
    }
    else
    {
        t[j]=i;
        h[i]+=h[j];
    }

}

int main ()
{
    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);
    int i,nr1,nr2,nr3,nr4,t1,t2;

    scanf("%d%d",&n,&m);
    for(i=1;i<=n;++i)
        t[i]=i;
    for(i=1;i<=m;++i)
    {
        scanf("%d%d%d%d",&nr1,&nr2,&nr3,&nr4);
        lst.pb (mp(mp(i,mp(nr1,nr2)),mp(nr3,nr4)));
    }

    sort(lst.begin (),lst.end (),cmp ());

    for(i=0;i<(int)lst.size ();++i)
    {
        t1=find (lst[i].x);
        t2=find (lst[i].y);
        if(t1!=t2)
        {
            printf("%d\n",lst[i].poz);
            if(++k==n-1)
                return 0;
            unite(t1,t2);
        }
    }
        //printf("%d %d %d %d %d\n",lst[i].poz,lst[i].x,lst[i].y,lst[i].c1,lst[i].c2);
    return 0;
}