Pagini recente » Cod sursa (job #1665340) | Cod sursa (job #2447239) | Cod sursa (job #13585) | Cod sursa (job #665366) | Cod sursa (job #3273558)
#include <bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[100002], h[100002];
int getroot(int x)
{
int root=x;
while(tata[root]!=root)
root=tata[root];
while(x!=tata[x]) //x!=root
{
int tx=tata[x];
tata[x]=root;
x=tx;
}
return root;
}
void unite(int x, int y)
{
if(h[x]<h[y])
tata[x]=y;
else if(h[x]==h[y]){
tata[x]=y;
h[y]++;}
else
tata[y]=x;
}
int main()
{
int N, M, x, y, op;
for(int i=1; i<=M; i++)
{
fin>>op>>x>>y;
if(op==1)
unite(x, y);
else
if(getroot(x)==getroot(y))
fout<<"DA"<<"\n";
else fout<<"NU"<<"\n";
}
for(int i=1;i<=N;i++)
{
tata[i]=i;
h[i]=1;
}
return 0;
}