Pagini recente » Cod sursa (job #948830) | Cod sursa (job #2862695) | Cod sursa (job #2442176) | Cod sursa (job #459069) | Cod sursa (job #1437500)
#include <fstream>
#define dim 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int RG[dim],T[dim],i,n,m,op,x,y;
int find(int node)
{
int x;
int R;
for(R=node;R!=T[R];R=T[R]);
for(;node!=T[node];)
{
x=T[node];
T[node]=R;
node=x;
}
return R;
}
void unite(int x, int y)
{
if(RG[x]>RG[y])
T[y]=x;
else
T[x]=y;
if(RG[x]==RG[y])
RG[y]++;
}
int main()
{
fin>>n>>m;
for(i=1;i<=n;i++)
{
RG[i]=1;
T[i]=i;
}
for(i=1;i<=m;i++)
{
fin>>op>>x>>y;
if(op==1)
{
if(find(x)!=find(y))
unite(find(x),find(y));
continue;
}
if(op==2)
{
if(find(x)==find(y))
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
continue;
}
}
return 0;
}