Pagini recente » Cod sursa (job #11696) | Cod sursa (job #2725087) | Cod sursa (job #2148186) | Cod sursa (job #1120072) | Cod sursa (job #2641773)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int> t;
int n, m;
void rd()
{
f>>n>>m;
t.resize(n+1);
for(int i=0; i<=n; i++)
t[i]=i;
}
int tata(int x)
{
if(t[x]!=x)
t[x]=tata(t[x]);
return t[x];
}
inline void unite(int x, int y)
{
t[tata(x)]=tata(y);
}
inline bool q(int x, int y)
{
return (tata(x)==tata(y));
}
int main()
{
rd();
int op, x, y;
for(int i=0; i<m; i++)
{
f>>op>>x>>y;
if(op==1)
unite(x, y);
else
g<<((q(x, y))?"DA":"NU")<<'\n';
}
f.close();
g.close();
return 0;
}