Pagini recente » Cod sursa (job #3221065) | Cod sursa (job #439197) | Cod sursa (job #1479902) | Cod sursa (job #91299) | Cod sursa (job #1612875)
#include<fstream>
#include<vector>
#define MAXN 100020
using namespace std;
ifstream is("disjoint.in");
ofstream os("disjoint.out");
int n, m;
int rang[MAXN], root[MAXN];
int Find(int x);
void Union(int x, int y);
int main()
{
is >> n >> m;
//os << n << " " << m;
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(Find(x) ,Find(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;
}
return rt;
}
void Union(int x, int y)
{
if(rang[x] > rang[y])
{
root[y] = x;
}
else
root[x] = y;
if(rang[x] == rang[y]) rang[y]++;
}