Pagini recente » Cod sursa (job #1076949) | Cod sursa (job #198053) | Cod sursa (job #1113601) | Cod sursa (job #2007319) | Cod sursa (job #1612876)
#include<fstream>
#include<vector>
#define MAXN 100001
using namespace std;
ifstream is("disjoint.in");
ofstream os("disjoint.out");
int n, m;
int rang[MAXN], root[MAXN];
int Find(int x);
int Find2(int x);
void Union(int x, int y);
int main()
{
is >> n >> m;
//os << n << " " << m;
r
for(int i = 1; i <= n; ++i)
{
rang[i] = 1;
root[i] = i;
}
int x, y, op;
for(int i = 1; i <= m; ++i)
{
is >> op >> x >> y;
if(op == 1)
{
Union(x, y);
}
else
{
if(Find(x) == Find(y))
{
os << "DA" << '\n';
//os << Find(x) << ' ' << Find(y) << '\n';
}
else
os << "NU" << '\n';
}
}
is.close();
os.close();
return 0;
}
int Find(int x)
{
int rt;
for(rt = x; root[rt] != rt; rt = root[rt])
{}
int y;
while(root[x] != x)
{
y = root[x];
root[x] = rt;
x = y;
}
// os << x << ' ' << rt << '\n';
return rt;
}
void Union(int x, int y)
{
int rx = Find(x);
int ry = Find(y);
if(rang[x] > rang[y])
{
root[y] = x;
}
else
root[x] = y;
if(rang[x] == rang[y]) rang[y]++;
}
int Find2(int x)
{
if(root[x] == x)
return x;
return Find2(root[x]);
}