Pagini recente » Cod sursa (job #1511063) | Cod sursa (job #947976) | Cod sursa (job #1290642)
# include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 5;
int s[nmax],v[nmax];
int poz(int x)
{
int r=x,aux;
while (s[r] != r) r=s[r];
while (s[x] != x) aux=s[x],s[x]=r,x=aux;
return x;
}
void comb(int x,int y)
{
if (v[x] > v[y]) s[y]=x;else s[x]=y;
if (v[x] == v[y]) ++v[y];
}
int main(void)
{
int n,m,t,x,y;
ifstream fi("disjoint.in");
ofstream fo("disjoint.out");
fi>>n>>m;
for (int i=1;i<=n;++i) s[i]=i,v[i]=1;
while (m --)
{
fi>>t>>x>>y;
if (t == 1) comb(poz(x),poz(y));else fo << (poz(x) == poz(y) ? "DA\n":"NU\n");
}
return 0;
}