Pagini recente » Cod sursa (job #911760) | Cod sursa (job #2037716) | Cod sursa (job #1192735) | Monitorul de evaluare | Cod sursa (job #2419696)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[20005],grad[20005];
int n,m,op;
int find_father(int node)
{
if(tata[node]!=node)
tata[node]=find_father(tata[node]);
return tata[node];
}
int main()
{
int i,x,y;
f>>n>>m;
for(i=1;i<=n;i++)
{
tata[i]=i;
grad[i]=1;
}
for(i=0;i<m;i++)
{
f>>op>>x>>y;
if(op==2)
{
int fx=find_father(x);
int fy=find_father(y);
if(fx==fy)
g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
else
if(op==1)
{
int fx=find_father(x);
int fy=find_father(y);
if(fx==fy)
continue;
if(grad[fx]<grad[fy])
{
tata[fx]=fy;
grad[fy]+=grad[fx];
}
else
{
grad[fx]+=grad[fy];
tata[fy]=fx;
}
}
}
f.close();
g.close();
return 0;
}