Pagini recente » Cod sursa (job #1989521) | Cod sursa (job #235672) | Cod sursa (job #500010) | Cod sursa (job #2988827) | Cod sursa (job #1394377)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N,M,x,y,i,cod,t[100005],rang[100005];
int find_comp(int x)
{
if(x!=t[x])
{
int tata=find_comp(t[x]);
t[x]=tata;
}
return t[x];
}
void unite(int x, int y)
{
int tx=find_comp(x);
int ty=find_comp(y);
if(rang[tx]<=rang[ty])
{ t[tx]=ty; rang[ty]++; }
else
{ t[ty]=tx; rang[tx]++; }
}
void test(int x, int y)
{
if(find_comp(x)==find_comp(y))
g<<"DA"<<endl;
else
g<<"NU"<<endl;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&N, &M);
for(i=1;i<=N;i++)
t[i]=i;
for(i=1;i<=M;i++)
{
scanf("%d%d%d", &cod, &x, &y);
if(cod==1)
unite(x,y);
else
test(x,y);
}
}