Pagini recente » Cod sursa (job #858231) | Cod sursa (job #1087961) | Cod sursa (job #985619) | Cod sursa (job #1994083) | Cod sursa (job #2808959)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
//vector<pair<int,pair<int,int>>> edges;
//vector<pair<int,int>> res;
int parent[200005], dim[200005];
int n,m,x,y,tip;
//radacina arborelui in care se afla x
int find_root(int node)
{
while(node != parent[node])
node=parent[node];
return node;
}
void union_k(int x,int y)
{
int root_x=find_root(x);
int root_y=find_root(y);
if(dim[root_x]>=dim[root_y])
{
parent[root_y]=root_x;
dim[root_x]+=dim[root_y];
}
else
{
parent[root_x] = root_y;
dim[root_y] += dim[root_x];
}
}
int main()
{
in>>n>>m;
for(int i=1; i<=n; ++i)
{
dim[i]=1;
parent[i]=i;
}
for(int i=1; i<=m; i++)
{
in>>tip>>x>>y;
//edges.push_back({w,{x,y}});
if(tip == 1)
union_k(x,y);
else
{
if(find_root(x) != find_root(y))
{
out<<"NU"<<"\n";
}
else
out<<"DA"<<"\n";
}
}
return 0;
}