Pagini recente » Cod sursa (job #2652584) | Cod sursa (job #2734008) | Cod sursa (job #3263629) | Cod sursa (job #2847862) | Cod sursa (job #2936334)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int N = 1e5+1;
int T[N], rang[N];
int find(int x)
{
if(T[x] == 0)
return x;
else
{
int y = find(T[x]);
T[x] = y;
return y;
}
}
void Union(int x, int y)
{
if(rang[x] > rang[y])
T[y] = x;
else
{
T[x] = y;
if(rang[x] == rang[y])
rang[y]++;
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1;i<=n;i++)
rang[i] = 1;
while(m--)
{
int t,x,y;
fin>>t>>x>>y;
x = find(x);
y = find(y);
if(t == 1)
{
if(x!=y)
Union(x,y);
}
else
{
if(x == y)
fout<<"DA\n";
else fout<<"NU\n";
}
}
}