Pagini recente » Cod sursa (job #2889411) | Cod sursa (job #255959) | 123 | Cod sursa (job #2342741) | Cod sursa (job #1394351)
#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];
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);
int r=rand()%2;
if(r==0)
t[tx]=ty;
else
t[ty]=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);
}
}