Pagini recente » Cod sursa (job #2206248) | Cod sursa (job #2424340) | Cod sursa (job #1418213) | Cod sursa (job #457279) | Cod sursa (job #2423455)
#include <iostream>
#include <fstream>
using namespace std;
int tata[100005];
int inaltime[100005];
int find_father(int x)
{
if(x==tata[x])
return x;
int nod=find_father(tata[x]);
tata[x]=nod;
return nod;
}
void unite(int x, int y)
{
if(inaltime[x]>inaltime[y])
{
tata[y]=x;
}
else
{
tata[x]=y;
}
if(inaltime[x]==inaltime[y])
inaltime[y]++;
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int i,N,M,x,y,cod,tx,ty;
f>>N>>M;
for(i=1;i<=N;i++)
{
tata[i]=i;
inaltime[i]=1;
}
for(i=1;i<=M;i++)
{
f>>cod>>x>>y;
tx=find_father(x);
ty=find_father(y);
if(cod==1)
unite(tx,ty);
if(cod==2)
{
if(tx==ty)
g<<"DA"<<endl;
else
g<<"NU"<<endl;
}
}
return 0;
}