Pagini recente » Cod sursa (job #1956185) | Cod sursa (job #1535416) | Cod sursa (job #2126973) | Cod sursa (job #3271174) | Cod sursa (job #2408862)
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
//map<int,int> fatherOf;
int fatherOf[100002];
int grade[100002];
//map<int,int> grade;
int findFather(int node){
if(fatherOf[node] == node){
return node;
}
fatherOf[node] = findFather(fatherOf[node]);
return fatherOf[node];
}
int main() {
int n,m,x,y,A,B,o;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for(int i=1;i<=n;i++){
fatherOf[i] = i;
grade[i] = 1;
}
for(int i=1;i<=m;i++){
f>>o;
f>>x>>y;
A = findFather(x); B = findFather(y);
if(o==2 && A==B && A != 0) g<<"DA\n";
else if(o==2) g<<"NU\n";
if(o==2) continue;
if(A == B) continue;
if(grade[A] < grade[B]){
fatherOf[A] = B;
grade[B] += grade[A];
} else{
fatherOf[B] = A;
grade[A] += grade[B];
}
}
return 0;
}