Pagini recente » Cod sursa (job #213620) | Cod sursa (job #2718439) | Cod sursa (job #2632694) | Cod sursa (job #1119348) | Cod sursa (job #2086004)
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 100002;
int father[NMAX];
int findr(int node) {
if (father[node] != node) {
father[node] = findr(father[node]);
}
return father[node];
}
void unite(int A, int B) {
int rootA = findr(A);
int rootB = findr(B);
if (rand() % 2) {
father[rootB] = rootA;
} else {
father[rootA] = rootB;
}
}
int main()
{
int n,t;
in>>n>>t;
for(int i=1;i<=n;i++)
{
father[i]=i;
}
for(int i=1;i<=t;i++)
{
int p,x,y;
in>>p>>x>>y;
if(p==1)
{
unite(x,y);
}
else
{
if(findr(x)==findr(y))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
}