Pagini recente » Cod sursa (job #1064649) | Cod sursa (job #2677800) | Cod sursa (job #1918277) | Cod sursa (job #1749513) | Cod sursa (job #2089294)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[100001],x,y;
int tata(int x)
{
if(t[x]==x)
return x;
return t[x]=tata(t[x]);
}
void join(int x, int y)
{
int rx,ry;
rx=tata(x);
ry=tata(y);
t[rx]=ry;
}
int main()
{
int n,m,op;
f>>n>>m;
for(int i=1;i<=n;i++)
t[i]=i;
for(int i=1;i<=m;i++)
{
f>>op>>x>>y;
if(op==1) join(x,y);
if(op==2)
if(tata(x)==tata(y))
g<<"DA"<<endl;
else g<<"NU"<<endl;
}
f.close();
g.close();
return 0;
}