Pagini recente » Cod sursa (job #1595348) | Cod sursa (job #43893) | Cod sursa (job #2892303) | Cod sursa (job #779435) | Cod sursa (job #2419775)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector <int> graph[1000005];
int tata[1000005];
int grad[1000005];
int findfath( int node)
{
if (tata[node] == node)
return node;
int ans=findfath(tata[node]);
tata[node]=ans;
return ans;
}
int main()
{
int n,m, i;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for(i=0;i<n;i++)
{
tata[i]=i;
grad[i]=1;
}
for(i=0;i<m;i++)
{
int a,x,y;
f>>a>>x>>y;
int tx,ty;
tx=findfath(x);
ty=findfath(y);
if (a==1)
{
if (tx!=ty)
{
if (grad[tx]<grad[ty])
{
tata[tx]=ty;
grad[ty]+=grad[tx];
}
else
{
tata[ty]=tx;
grad[tx]+=grad[ty];
}
}
}
else
{
if (tx==ty)
g<<"DA\n";
else
g<<"NU\n";
}
}
}