Pagini recente » Profil cristiana24 | Cod sursa (job #2465092) | Cod sursa (job #1557117) | Cod sursa (job #1285334) | Cod sursa (job #2713845)
#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) printf("DA \n");
else printf("NU \n");
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%i %i",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,t;
scanf("%i %i %i",&t,&x,&y);
update(x,y,t);
}
return 0;
}