Pagini recente » Cod sursa (job #2744098) | Cod sursa (job #720229) | Cod sursa (job #2044912) | Cod sursa (job #270076) | Cod sursa (job #1818123)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int Nmax=100005;
int V[Nmax],TT[Nmax];
int N,M;
int Father(int Nod)
{
while(Nod!=TT[Nod])
Nod=TT[Nod];
return Nod;
}
void Unite(int x,int y)
{
if(V[x] > V[y])
TT[y] = x;
if(V[y] > V[x])
TT[x] = y;
if(V[x] == V[y])
{
TT[x] = y;
V[y]++;
}
}
void ReadandPrint()
{
fin>>N>>M;
for(int i=1;i<=N;++i)
TT[i]=i;
while(M--)
{
int op,x,y;
fin>>op>>x>>y;
if(op==1) Unite(Father(x),Father(y));
if(op==2)
{
if(Father(x)==Father(y)) fout<<"DA\n";
else fout<<"NU\n";
}
}
}
int main()
{
ReadandPrint();
}