Pagini recente » Cod sursa (job #2348077) | nou | Cod sursa (job #2515494) | Cod sursa (job #3233745) | Cod sursa (job #3268771)
#include <bits/stdc++.h>
using namespace std;
#define TITLE "disjoint"
ifstream f (TITLE".in");
ofstream g (TITLE".out");
int Fathers[100002];
int Rang[100002];
int Root(int x)
{
if(Fathers[x]==x)
return x;
return Fathers[x]=Root(Fathers[x]);
}
void Union(int x, int y)
{
int Rootx=Root(x), Rooty=Root(y);
if(Rang[Rootx]<Rang[Rooty])
Fathers[Rootx]=Fathers[Rooty];
else
Fathers[Rooty]=Fathers[Rootx];
if(Rang[Rootx]==Rang[Rooty])
Rang[Rootx]++;
}
int main()
{
int n,m;
f>>n>>m;
for(int i=1; i<=n; i++)
{
Fathers[i]=i;
Rang[i]=1;
}
while(m--)
{
int c, x , y;
f>>c>>x>>y;
if(c==1)
Union(x,y);
else
{
if(Root(x)==Root(y))
g<<"DA";
else
g<<"NU";
g<<'\n';
}
}
return 0;
}