Pagini recente » Cod sursa (job #2128025) | Cod sursa (job #735865) | Cod sursa (job #1283177) | Cod sursa (job #1513665) | Cod sursa (job #1478094)
#include<iostream>
#include<fstream>
#define LIM 100005
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int t[LIM],c[LIM],n,m;
int Find(int x)
{
int y,z;
y=x;
while (t[x]!=0)
x=t[x];
return x;
}
inline void Union (int x, int y)
{
if (c[x] > c[y])
{
c[x]+=c[y];
t[y]=x;
}
else /// c[y] > c[x]
{
c[y]+=c[x];
t[x]=y;
}
}
void CitescRezolv()
{
int i,cod,x,y;
for (i=1; i<=LIM; i++)
c[i]=1;
fin>>n>>m;
for (i=1; i<=m; i++)
{
fin>>cod>>x>>y;
x=Find(x); y=Find(y);
if (cod == 1)
Union(x,y);
else
{
if (x==y) fout<<"DA\n";
else fout<<"NU\n";
}
}
}
int main ()
{
CitescRezolv();
fin.close();
fout.close();
return 0;
}