Pagini recente » Cod sursa (job #1916006) | Cod sursa (job #1687986) | Cod sursa (job #1576415) | Cod sursa (job #383541) | Cod sursa (job #2203045)
#include <bits/stdc++.h>
#define N_MAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tat[N_MAX],n,m;
int cauta(int a)
{
int x=a;
while(tat[a]!=-1)
{
a=tat[a];
}
while(tat[x]!=-1)
{
int p=tat[x];
tat[x]=a;
x=p;
}
return a;
}
void unite(int a, int b)
{
tat[a]=b;
}
int main()
{
in >> n >> m;
for(int i=1; i<=n; i++)
{
tat[i]=-1;
}
for(int i=0; i<m; i++)
{
int x,y,z;
in >> x >> y >> z;
if(x==1)
{
if(cauta(y)!=cauta(z))
{
unite(y,z);
}
else
return 0;
}
else
{
if(cauta(y)==cauta(z))
out << "DA\n";
else
out << "NU\n";
}
}
return 0;
}