Pagini recente » Cod sursa (job #370374) | Cod sursa (job #3178054) | Cod sursa (job #1370417) | Cod sursa (job #740011) | Cod sursa (job #2426459)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int NMAX = 100005;
int n,m,next[NMAX],rang[NMAX];
int found(int x)
{
if(x != next[x])
next[x] = found(next[x]);
return next[x];
}
void unite(int x, int y){
if(rang[x] > rang[y])
next[y] = x;
else
next[x] = y;
if(rang[x] == rang[y])
rang[y]++;
}
int main(){
int i,j,cod,x,y;
f >> n >> m;
for(i = 1 ; i <= n ; i++){
next[i] = i;
rang[i] = 1;
}
for(i = 1 ; i <= m ; i++){
f >> cod >> x >> y;
//g << found(x) << " " << found(y) << "\n";
if(cod == 2){
if(found(x) == found(y))
g << "DA\n";
else
g << "NU\n";
}else
if(found(x) != found(y))
unite(found(x) , found(y));
}
return 0;
}