Pagini recente » Cod sursa (job #891888) | Cod sursa (job #1877) | Cod sursa (job #551132) | Cod sursa (job #966942) | Cod sursa (job #1834258)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
#define nmax 100005
int tt[nmax], rang[nmax];
int find(int x)
{
int root, y;
for (root = x; tt[root] != root; root = tt[root]);
for (; tt[x] != x;)
{
y = tt[x];
tt[x] = root;
x = y;
}
return root;
}
void unite(int a, int b)
{
if(rang[a]>rang[b])
tt[b]=a;
else tt[a]=b;
if(rang[a]==rang[b]) rang[b]++;
}
int main()
{
int n, m, i;
f >> n;
f >> m;
for (i = 1; i <= n; i++)
{
tt[i] = i;
rang[i] = 1;
}
while (m--)
{
int b, x, y;
f >> b >> x >> y;
if (b == 1)
{
unite(find(x), find(y));
}
else {
if (find(x) == find(y)) g << "DA"<<'\n';
else g << "NU" <<'\n';
}
}
}