Pagini recente » Cod sursa (job #2532572) | Cod sursa (job #165173) | Cod sursa (job #1358758) | Cod sursa (job #2505796) | Cod sursa (job #2808233)
#include<bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int> parent;
vector<int> height;
int findParent(int x){
while(parent[x]!=x)
x=parent[x];
return x;
}
int main(){
int n,m,tip,a,b,i;
f>>n>>m;
for(i=0;i<n;++i){
parent.push_back(i);
height.push_back(1);
}
for(i=0;i<m;++i){
f>>tip>>a>>b;
if(tip==1){
int x=findParent(a-1);
int y=findParent(b-1);
if(height[x]>=height[y]){
parent[y]=x;
height[x]=height[x]+height[y];
}
else{
parent[x]=y;
height[y]=height[y]+height[x];
}
}
else if(tip==2){
int x=findParent(a-1);
int y=findParent(b-1);
if(x==y)
g<<"DA\n";
else
g<<"NU\n";
}
}
return 0;
}