Pagini recente » Cod sursa (job #2391358) | Cod sursa (job #3218602) | Cod sursa (job #2769117) | Cod sursa (job #1604672) | Cod sursa (job #1394369)
#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()
{
srand(time(NULL));
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);
}
}