Pagini recente » Cod sursa (job #2872167) | Cod sursa (job #3227236) | Cod sursa (job #2791955) | Cod sursa (job #167354) | Cod sursa (job #2832383)
#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;
}
nod getRootIterativ(nod a)
{
while(theRoots[a.root] != a.root)
{
a.root = theRoots[a.root];
a.grad++;
}
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 = getRootIterativ({x,0});
nod r2 = getRootIterativ({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;
}