Pagini recente » Cod sursa (job #71997) | Cod sursa (job #2610384) | Cod sursa (job #3194730) | Cod sursa (job #2399048) | Cod sursa (job #2497518)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n,m;int t[100005];int h[100005];
int calc(int x){int y=x;
while(t[y]!=y)y =t[y];
while(t[x]!=y)
{int z=x;x=t[x] ;t[z]=y;}
return y;}
void join(int x, int y)
{x=calc(x);y=calc(y);if(x==y)return;
if(h[x]==h[y])
{t[x]=y;h[x]++;return;}
if(h[x]>h[y]){ t[y]=x;return;}
if(h[x]<h[y] ){t[x]=y;return;}}
bool is(int x, int y)
{if(calc(x)==calc(y))return true;
return false;
}int main()
{f>>n>>m;
for(int i=1;i<100005;++i)
{t[i]=i; h[i]=1;}
for(int i=1;i<=m; ++i){int c,x,y;
f>>c>>x>>y;if(c==1) {join(x,y);
} else {if(is(x,y ))g<<"DA\n";
else g<<"NU\n";}} return 0;
}