Cod sursa(job #217681)

Utilizator blahblahblahblah blahblah Data 29 octombrie 2008 18:34:29
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <stdio.h>
#include <vector>
using namespace std;

#define NMAX 250010

int N, M, beg, end;

vector <pair<int, int> > leg[1010];

int tata[NMAX];

int unite(int x, int y)
{
	if (rand() & 1) tata[x] = y;
	else tata[y] = x;
}

int father(int x)
{
	if (tata[x] == x) return x;
	return tata[x] = father(tata[x]);
}

int bg;
char s[100];

void GET(int &x)
{
	for (; s[bg] && !('0' <= s[bg] && s[bg] <= '9'); bg++);
	
	for (x = 0; '0' <= s[bg] && s[bg] <= '9'; bg++)
		x = x * 10 + s[bg] - '0';
}

int main()
{
	int i, x, y, c;
	
	freopen("pscnv.in", "r", stdin);
	freopen("pscnv.out", "w", stdout);
	
	scanf("%d %d %d %d\n", &N, &M, &beg, &end);
	
	for (i = 1; i <= N; i++) tata[i] = i;
	
	for (i = 1; i <= M; i++) {
//		fgets(s, 100, stdin); bg = 0;
//		GET(x); GET(y); GET(c);
		scanf("%d %d %d", &x, &y, &c);
		
		leg[c].push_back(make_pair(x, y));
	}
	
	for (i = 1; i <= 1000; i++) {
		for (vector <pair<int, int> > :: iterator it = leg[i].begin(); it != leg[i].end(); ++it) {
			x = father(it->first); y = father(it->second);
			if (x != y) unite(x, y);
		}

			
		if (father(beg) == father(end)) break;
	}
	
	printf("%d\n", i);
	
return 0;
}