Pagini recente » Cod sursa (job #2564990) | Cod sursa (job #1617138) | Cod sursa (job #420724) | Cod sursa (job #2973147) | Cod sursa (job #2521640)
#include<bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int h[100001];
int t[100001];
int n,m;
void read()
{
in>>n>>m;
for(int i=1; i<=n; i++)
{
t[i]=0;
h[i]=1;
}
}
int find_root(int x)
{
while(t[x])
{
x=t[x];
}
return x;
}
void merge_trees(int x,int y)
{
if(h[x]==h[y])
{
t[x]=y;
h[y]++;
}
else if(h[x]>h[y])
{
t[y]=x;
}
else
{
t[x]=y;
}
}
void query(int type,int a,int b)
{
switch(type)
{
case 1:
merge_trees(find_root(a),find_root(b));
break;
case 2:
find_root(a)==find_root(b) ? out<<"DA\n" : out<<"NU\n";
}
}
void solve()
{
int a,b,c;
for(int i=0; i<m; i++)
{
in>>a>>b>>c;
query(a,b,c);
}
}
int main()
{
read();
solve();
}