Pagini recente » Cod sursa (job #505610) | Cod sursa (job #2680490) | Cod sursa (job #264683) | Cod sursa (job #1843088) | Cod sursa (job #2115636)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#define For(i, x, y) for(int i = x; i <= y; ++i)
#define Forr(i, x, y) for(int i = x; i <= y; --i)
#define sz() size()
#define nMax 100005
using namespace std;
int P[nMax], h[nMax];
int root(int x){
if(P[x] != x) P[x] = root(P[x]);
return P[x];
}
void unite(int x, int y)
{
int Rx = root(x);
int Ry = root(y);
if(h[Rx] >= h[Ry])
P[Ry] = Rx;
else
P[Rx] = Ry;
if(h[Rx] == h[Ry]) h[Rx]++;
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, type, x, y;
fin >> n >> m;
For(i, 1, n){
P[i] = i;
h[i] = 1;
}
For(i, 1, m){
fin >> type >> x >> y;
if(type == 1) unite(x, y);
else if(root(x) == root(y)) fout << "DA\n";
else fout << "NU\n";
}
return 0;
}