Pagini recente » Cod sursa (job #1883785) | Cod sursa (job #1744223) | Cod sursa (job #2960614) | Cod sursa (job #1608852) | Cod sursa (job #2955940)
#include <fstream>
using namespace std;
int T[100005],depth[100005];
int findRT(int node)
{
if(T[node]==node)
return node;
T[node]=findRT(T[node]);
return T[node];
}
void UNION(int a,int b)
{
if(depth[a]==depth[b])
{
T[a]=b;
depth[b]++;
}
else if(depth[a]<depth[b])
{
T[a]=b;
}
else
{
T[b]=a;
}
}
int main()
{
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,cer,a,b;
cin>>n>>m;
for(int i=1;i<=n;i++)
T[i]=i,depth[i]=1;
for(int i=1;i<=m;i++)
{
cin>>cer>>a>>b;
if(cer==1)
{
UNION(a,b);
}
else
{
if(findRT(a)==findRT(b))
cout<<"DA\n";
else
cout<<"NU\n";
}
}
return 0;
}