Pagini recente » Cod sursa (job #75528) | Cod sursa (job #1854848) | Cod sursa (job #1764621) | Cod sursa (job #41914) | Cod sursa (job #2575844)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int N = 100005;
int tt[N], rnk[N];
int root(int x)
{
int R;
for(R=x; R!=tt[R]; R=tt[R]);
int y;
while(x != tt[x])
{
y = tt[x];
tt[x] = R;
x = y;
}
return R;
}
void unite(int x, int y)
{
if(rnk[x] > rnk[y])
{
tt[y] = x;
}
else
{
tt[x] = y;
}
if(rnk[x] == rnk[y])
{
rnk[y]++;
}
}
int main()
{
int i, n, m, p, x, y;
fin>>n>>m;
for(i=1; i<=n; i++)
{
tt[i] = i;
rnk[i] = 1;
}
for(i=1; i<=m; i++)
{
fin>>p>>x>>y;
if(p == 1)
{
unite(root(x), root(y));
}
else
{
int rx = root(x), ry = root(y);
if(rx == ry)
{
fout<<"DA\n";
}
else
{
fout<<"NU\n";
}
}
}
fin.close();
fout.close();
return 0;
}