Pagini recente » Cod sursa (job #897601) | Cod sursa (job #413153) | Cod sursa (job #470887) | Cod sursa (job #2708278) | Cod sursa (job #2640381)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int> t;
int find(int x)
{
if (t[x] == 0)
{
return x;
}
t[x] = find(t[x]);
return t[x];
}
void unite(int x, int y)
{
int rx = find(x);
int ry = find(y);
t[rx] = ry;
}
int main()
{
int n, m;
fin >> n >> m;
t.resize(n + 1);
for (int i = 0; i < m; i++)
{
int c, x, y;
fin >> c >> x >> y;
if (c == 1)
{
unite(x, y);
}
else
{
if (find(x) == find(y))
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
}
}
}
/*
*/