Pagini recente » Borderou de evaluare (job #2099310) | Cod sursa (job #1866059) | Cod sursa (job #761067) | Borderou de evaluare (job #1710654) | Cod sursa (job #2291022)
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<vector>
#include<list>
#include <fstream>
#include <iostream>
using namespace std;
#define MAXN 100000
#define MAXM 100000
int M,N;
int A[MAXN+1];
list<int> L[MAXN+1];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
void reuniune(int x,int y){
int nx=L[A[x]].size();
int ny=L[A[y]].size();
list<int>::iterator it;
int ax=A[x];
int ay=A[y];
if(nx>ny){
for(it=L[ay].begin();it!=L[ay].end();it++){
A[*it]=A[x];
}
L[A[x]].splice(L[A[x]].end(), L[ay]);
}
else{
for(it=L[ax].begin();it!=L[ax].end();it++){
A[*it]=A[y];
}
L[A[y]].splice(L[A[y]].end(), L[ax]);
}
}
int main(){
fin >> N >> M;
for(int i=1;i<=N;i++){
A[i]=i;
L[i].push_back(i);
}
int x,y,cod;
for(int i=0;i<M;i++){
fin >> cod >> x >> y;
if(cod==1){
reuniune(x,y);
}
else{
if(A[x]==A[y])
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
fin.close();
fout.close();
return 0;
}