Pagini recente » Cod sursa (job #3141152) | Cod sursa (job #191120) | Cod sursa (job #3192683) | Cod sursa (job #2381881) | Cod sursa (job #775524)
Cod sursa(job #775524)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int> v[200001];
int t[200001],rank[200001];
int find_root(int x)
{
if(t[x] != x)
{
t[x] = find_root(t[x]);
}
return t[x];
}
void uniune(int x, int y)
{
int xroot = find_root(x);
int yroot = find_root(y);
if(xroot == yroot)
return;
if(rank[xroot] > rank[yroot])
t[yroot] = xroot;
else if(rank[xroot] < rank[yroot])
t[xroot] = yroot;
else
{
t[yroot] = xroot;
++ rank[yroot];
}
}
int main()
{
int n,m,op,x,y;
fin>>n>>m;
for(int i = 1;i<=n;i++)
{
t[i] = i;
rank[i] = 1;
}
for(int i=1;i<=m;i++)
{
fin>>op>>x>>y;
if(op == 1)
{
uniune(x,y);
}
else
{
if(find_root(x) == find_root(y))
fout<<"DA";
else fout<<"NU";
fout<<'\n';
}
}
fin.close();
fout.close();
return 0;
}