IOIBIN - Các thùng nước

Tác giả: khuc_tuan

Ngôn ngữ: C++

#include <iostream>
#include <cstdio>

using namespace std;

int f[10010];

int root(int i) { return f[i]<0 ? i : root(f[i]); }

int main() {
	int p;
	scanf("%d", &p);
	memset( f, -1, sizeof(f));
	for(int kk=0;kk<p;++kk) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		if(z==1) {
			x = root(x);
			y = root(y);
			if(x!=y) {
				if(f[x]>f[y]) swap(x,y);
				f[x] += f[y];
				f[y] = x;
			}
		}
		else printf("%d\n", root(x)==root(y) );	
	}	
	return 0;
}

Download