Pagini recente » Cod sursa (job #594859) | Cod sursa (job #6811) | Cod sursa (job #1991999) | Cod sursa (job #2560025) | Cod sursa (job #2515278)
#include <bits/stdc++.h>
using namespace std;
int tata[100001], rg[100001], n, m, t, x, y, px, py;
int findroot(int x)
{
if(tata[x] != x) findroot(tata[x]);
else return x;
}
void Union(int x, int y)
{
px = findroot(x);
py = findroot(y);
if(px != py)
{
if(tata[px] < tata[py])
{
tata[px] = py;
rg[px]++;
}
else
{
tata[py] = px;
rg[py]++;
}
}
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f >> n >> m;
for(int i = 1; i <= n; ++ i)
{
tata[i] = i;
rg[i] = 1;
}
while(m --)
{
f >> t >> x >> y;
if(t == 1)Union(x, y);
else if(findroot(x) == findroot(y))g << "DA" << "\n";
else g << "NU" << "\n";
}
return 0;
}