Cod sursa(job #546186)

Utilizator AndreyPAndrei Poenaru AndreyP Data 4 martie 2011 16:12:39
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <cstdio>
#include <algorithm>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define ll long long
#define pll pair< ll,ll >
#define pii pair< int,int >
#define N 200010

int n,m;
pair< pii,pll > v[N];
int ind[N];
int deg[N],t[N];

inline void citire() {
	scanf("%d%d",&n,&m);

	for(int i=1; i<=m; ++i) {
		ind[i] = i;
		scanf("%d%d%lld%lld",&v[i].fs.fs,&v[i].fs.sc,&v[i].sc.fs,&v[i].sc.sc);
	}
}

inline int find(int x) {
	int r;

	for(r=x; t[r]!=r; r=t[r])
		;

	int aux;
	while(x!=r) {
		aux = t[x];
		t[x] = r;
		x = aux;
	}

	return r;
}

inline void unite(int x,int y) {
	x = find(x);
	y = find(y);

	if(deg[x]<deg[y])
		t[x] = y;
	else
		t[y] = x;

	if(deg[x]==deg[y])
		++deg[x];
}

bool compar(const int &x,const int &y) {
	return ((v[x].sc.fs!=v[y].sc.fs) ? (v[x].sc.fs<v[y].sc.fs) : (v[x].sc.sc>v[y].sc.sc));
}

inline void rezolva() {
	sort(ind+1,ind+m+1,compar);
	for(int i=1; i<=n; ++i) {
		t[i] = i;
		deg[i] = 1;
	}

	int left = n-1;

	int x,y;
	for(int i=1; left>0 && i<=m; ++i) {
        	x = v[ind[i]].fs.fs;
		y = v[ind[i]].fs.sc;

		if(find(x)==find(y))
			continue;
		
		unite(x,y);
		printf("%d\n",ind[i]);
		--left;
	}
}

int main() {
	freopen("lazy.in","r",stdin);
	freopen("lazy.out","w",stdout);

	citire();
	rezolva();

	return 0;
}