Pagini recente » Cod sursa (job #805903) | Cod sursa (job #2679021) | Cod sursa (job #2096534) | Cod sursa (job #3176438) | Cod sursa (job #2062329)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#define NMax 100001
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int rang[NMax], point[NMax];
int n, m, cod, x, y;
void unite(int x, int y)
{
if(rang[x] > rang[y]) point[y] = x;
else point[x] = y;
if(rang[x] == rang[y]) rang[y]++;
}
int verif(int x)
{
int R, y;
for(R = x; point[R] != R; R = point[R]); /// parcurg arborele
/// compresia drumurilor
while(point[x] != x)
{
y = point[x];
point[x] = R;
x = y;
}
return R;
}
int main()
{
f >> n >> m;
for(int i = 1; i <= n; ++i)
{
point[i] = i;
rang[i] = 1;
}
for(int i = 1; i <= m; ++i)
{
f >> cod >> x >> y;
if(cod == 2)
{
if(verif(x) == verif(y)) g << "DA" << '\n';
else g << "NU" << '\n';
}
else
{
unite(verif(x), verif(y));
}
}
return 0;
}