Pagini recente » Cod sursa (job #640782) | Cod sursa (job #501125) | Cod sursa (job #1200100) | Cod sursa (job #704307) | Cod sursa (job #968713)
Cod sursa(job #968713)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int N = 100005;
int n, m, t[N], h[N];
int find(int x)
{
int r = x, y = x, tat;
while(t[r]) r = t[r];
while(y != r){
tat = t[y];
t[y] = r;
y = tat;
}
return r;
}
void Unite(int x, int y)
{
if(h[x] > h[y]) t[y] = x;
else{
t[x] = y;
if(h[x] == h[y]) h[y]++;
}
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
h[i] = 1;
while(m--)
{
int cod, x, y;
fin>>cod>>x>>y;
if(cod == 1)
Unite(find(x), find(y));
else{
if(find(x) == find(y)) fout<<"DA\n";
else fout<<"NU\n";
}
}
return 0;
}