Pagini recente » Cod sursa (job #1425746) | Cod sursa (job #616223) | Cod sursa (job #1460921) | Cod sursa (job #2071734) | Cod sursa (job #1906917)
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int t[100003], c[100003];
void Initializare()
{
int i, j;
for (i=1; i<=N; i++)
t[i] = 0, c[i] = 1;
}
int Find(int x)
{
int y, z;
y=x;
while (t[x]!=0)
x=t[x];
while (t[y]!=0)
{
z=t[y];
t[y]=x;
y=z;
}
return x;
}
void Union(int x, int y)
{
if (c[x] > c[y])
{
c[x] += c[y];
c[y] = 0;
t[y] = x;
}
else // c[y] <c[x]
{
c[y] += c[x];
c[x] = 0;
t[x] = y;
}
}
int main ()
{
int i, q, x, y;
fin >> N >> M;
Initializare();
for (i=1; i<=M; i++)
{
fin >> q >> x >> y;
if (q==1)
Union(x,y);
else
{
if (Find(x)==Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}