Pagini recente » Cod sursa (job #2920366) | Cod sursa (job #836067) | Cod sursa (job #1444215) | Monitorul de evaluare | Cod sursa (job #744527)
Cod sursa(job #744527)
#include <fstream>
#include <iostream>
using namespace std;
#define mAX 100001
int A[mAX], R[mAX];
int n, m;
int find(int x)
{
int R, y;
for (R = x; A[R] != R; R = A[R]);
for (; A[x] != x;)
{
y = A[x];
A[x] = R;
x = y;
}
return R;
}
void reunion(int x, int y)
{
if (R[x] > R[y])
A[y] = x;
else A[x] = y;
if (R[x] == R[y]) R[y]++;
}
int main()
{
fstream f("disjoint.in",ios::in);
fstream g("disjoint.out",ios::out);
f>>n>>m;
int i, x, y, cd;
for (i = 1; i <= n; i++)
{
A[i] = i;
R[i] = 1;
}
for (i = 1; i <= m; i++)
{
f>>cd>>x>>y;
if (cd == 2){
if (find(x) == find(y)) g<<"DA\n";
else g<<"NU\n";
}
else
{
if (find(x) == find(y)) {g<<i;return 0;}
reunion(find(x), find(y));
}
}
f.close();
g.close();
return 0;
}