Pagini recente » Cod sursa (job #2439941) | Cod sursa (job #2086567) | Cod sursa (job #873722) | Cod sursa (job #564424) | Cod sursa (job #2485192)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N, M;
int root[Nmax], father[Nmax], h[Nmax];
void change_root(int node, int newRoot) {
int curr = node;
while (curr != 0) {
int nxt = father[curr];
root[curr] = father[curr] = root[node];
curr = nxt;
}
}
void unit(int x, int y) {
if (h[x] >= h[y]) change_root(y, root[x]);
else change_root(x, root[y]);
if (h[x] == h[y]) ++h[x];
}
int main()
{
f >> N >> M;
for (int i = 1; i <= N; ++i)
root[i] = i, h[i] = 1;
for (int i = 1; i <= M; ++i) {
int tip, x, y;
f >> tip >> x >> y;
if (tip == 2) {
if (root[x] == root[y]) g << "DA\n";
else g << "NU\n";
}
else unit(x, y);
}
for(int i = 1; i <= N; ++i)
g << h[i] << " ";
return 0;
}