Pagini recente » Cod sursa (job #749628) | Cod sursa (job #968258) | Cod sursa (job #2598114) | Cod sursa (job #2948319) | Cod sursa (job #1394343)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N,M,x,y,i,cod,t[100005];
int find_comp(int x)
{
if(x!=t[x])
return (t[x]=find_comp(t[x]));
else
return x;
}
void unite(int x, int y)
{
int tx=find_comp(x);
int ty=find_comp(y);
t[tx]=ty;
}
void test(int x, int y)
{
if(find_comp(x)==find_comp(y))
g<<"DA"<<endl;
else
g<<"NU"<<endl;
}
int main()
{
f>>N>>M;
for(i=1;i<=N;i++)
t[i]=i;
for(i=1;i<=M;i++)
{
f>>cod>>x>>y;
if(cod==1)
unite(x,y);
else
test(x,y);
}
}