Pagini recente » Rating Matei Gabriel (gmatei) | Cod sursa (job #156926) | Cod sursa (job #2018641) | Cod sursa (job #2247069) | Cod sursa (job #3286544)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int S[100005], t[100005];
int n, m;
int FindRoot(int x)
{
if(x != t[x]) return FindRoot(t[x]);
return t[x];
}
void Union(int x, int y)
{
if(S[y] == 0) return;
x = FindRoot(x);
y = FindRoot(y);
if(x != y)
{
if(S[x] < S[y]) swap(x, y);
t[y] = x;
S[x] += S[y];
}
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i, x, y, p;
fin >> n >> m;
for(i=1;i<=n;i++)
{
S[i] = 1;
t[i] = i;
}
for(i=1;i<=m;i++)
{
fin >> p >> x >> y;
if(p == 1) Union(x, y);
else
{
if(FindRoot(x) == FindRoot(y)) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}