Pagini recente » Cod sursa (job #2423036) | Cod sursa (job #2920757) | Cod sursa (job #1257993) | Cod sursa (job #3227974) | Cod sursa (job #3285263)
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <queue>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <stack>
#include <climits>
#include <unordered_map>
#include <map>
#include <set>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, t[100002];
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int rad, y;
rad = x;
while (t[rad] != 0)
rad = t[rad];
/// comprimarea drumului
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int i, j, x, y, op;
fin >> n >> m;
for (i = 1; i <= n; i++)
t[i] = 0;
while (m--)
{
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1) Union(x, y);
else if (x == y) fout << "DA\n";
else fout << "NU\n";
}
return 0;
}