Pagini recente » Cod sursa (job #1897654) | Cod sursa (job #138987) | Cod sursa (job #529532) | Cod sursa (job #830955) | Cod sursa (job #2720319)
#include <bits/stdc++.h>
using namespace std;
const string FILENAME = "disjoint";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");
int n, m;
int t[100001];
int find(int x)
{
int r = x, y;
while(t[r] != r) r = t[r];
while(t[x] != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
void uni(int x, int y)
{
t[y] = x;
}
int main()
{
int op, x, y;
fin >> n >> m;
for(int i = 1; i <= n; i++)
t[i] = i;
while(m--)
{
fin >> op >> x >> y;
if(op == 1)
uni(find(x), find(y));
else
{
if(find(x) == find(y))
fout << "DA\n";
else fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}