Pagini recente » Cod sursa (job #341219) | Cod sursa (job #2947132) | Cod sursa (job #468723) | Cod sursa (job #2043298) | Cod sursa (job #2832315)
#include <iostream>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int theRoots[100005];
struct nod
{
int root,grad;
};
nod getRoot(nod a){
if(a.root != theRoots[a.root])
return getRoot({theRoots[a.root],a.grad+1});
else
return a;
}
void setUnion(int a,int b)
{
theRoots[b] = a;
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1;i<=n;i++)
theRoots[i] = i;
for(int i=0; i < m; i++)
{
int t,x,y;
fin>>t>>x>>y;
nod r1 = getRoot({x,0});
nod r2 = getRoot({y,0});
if(t == 1)
{
if(r1.root != r2.root)
{
if(r1.grad > r2.grad)
setUnion(r1.root,r2.root);
else
setUnion(r2.root,r1.root);
}
}
else{
if(r1.root != r2.root)
fout<<"NU"<<'\n';
else
fout<<"DA"<<'\n';
}
}
return 0;
}