Pagini recente » Cod sursa (job #2150854) | Cod sursa (job #1480460) | Cod sursa (job #895776) | Cod sursa (job #1389060) | Cod sursa (job #3193338)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
int v[10001];
int root(int x)
{
if(v[x]==x)
{
return x;
}
v[x]=root(v[x]);
return v[x];
}
void unite(int x,int y)
{
x=root(x),y=root(y);
if(rand() & 1)
{
v[x]=y;
}
else
{
v[y]=x;
}
}
int main()
{
fin >> n >> m;
for(int i=1;i<=n;i++)
{
v[i]=i;
}
while(m--)
{
int t,x,y;
fin >> t >> x >> y;
if(t==1)
{
unite(x,y);
}
else
{
if(root(x)==root(y))
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
}
}
return 0;
}