Pagini recente » Cod sursa (job #2515239) | Cod sursa (job #1723308) | Cod sursa (job #768847) | Cod sursa (job #152100) | Cod sursa (job #3248693)
#include<fstream>
std::ifstream fin("disjoint.in");
std::ofstream fout("disjoint.out");
const int NMAX=100005;
int t[NMAX], depth[NMAX];
int n, q;
void init()
{
fin>>n>>q;
for(int i=1; i<=n; ++i)
t[i]=i;
}
inline int root(int val)
{
if(t[val]==val)
return val;
int ans=root(t[t[val]]);
t[val]=ans;
return ans;
}
inline void unite(int a, int b)
{
int x=root(a), y=root(b);
if(depth[x]==depth[y])
{
t[y]=x;
++depth[x];
return;
}
if(depth[x]<depth[y])
{
t[x]=y;
return;
}
t[y]=x;
}
void solve()
{
int type, a, b;
for(int i=0; i<q; ++i)
{
fin>>type>>a>>b;
if(type==1)
unite(a, b);
else
fout<<((root(a)==root(b))?"DA\n":"NU\n");
}
}
int main()
{
init();
solve();
return 0;
}