Pagini recente » Cod sursa (job #1643368) | Cod sursa (job #384719) | Cod sursa (job #2415664) | Cod sursa (job #1636780) | Cod sursa (job #1488306)
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100003], n, m, i, op, x, x2, y, y2;
int rad(int x)
{
while(t[x]>0)
x=t[x];
return x;
}
int main()
{
in>>n>>m;
for(i=1; i<=n; i++)
t[i]=-1;
for(;m--;)
{
in>>op>>x>>y;
x2=rad(x);
y2=rad(y);
if(op==1)
{
if(x2!=y2)
{
if(t[x2]<t[y2])
{
t[x2]+=t[y2];
t[y2]=x2;
}
else
{
t[y2]+=t[x2];
t[x2]=y2;
}
}
}
else
{
if(x2==y2)
out<<"DA\n";
else
out<<"NU\n";
}
}
}