Pagini recente » Cod sursa (job #1871337) | Cod sursa (job #515760) | Cod sursa (job #1029779) | Cod sursa (job #1737126) | Cod sursa (job #2713842)
#include <bits/stdc++.h>
#define nmax 100000
using namespace std;
int p[nmax];
int n,m;
void update(int x, int y, int t)
{
vector<int> stiva;
int sx=0,sy=0;
while(p[x]!=0) {stiva.push_back(x); x=p[x];}
sx=stiva.size();
while(!stiva.empty()) {p[stiva.back()]=x;stiva.pop_back();}
while(p[y]!=0) {stiva.push_back(y); y=p[y];}
sy=stiva.size();
while(!stiva.empty()) {p[stiva.back()]=y;stiva.pop_back();}
if(t==1&&x!=y)
{
if(sx<sy) p[x]=y;
else p[y]=x;
}
if(t==2)
{
if(x==y) cout<<"DA"<<'\n';
else cout<<"NU"<<'\n';
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
cin>>n>>m;
for(int i=0;i<m;i++)
{
int x,y,t;
cin>>t>>x>>y;
update(x,y,t);
}
return 0;
}