Pagini recente » Cod sursa (job #2974268) | Cod sursa (job #3151428) | Cod sursa (job #2132037) | Cod sursa (job #204366) | Cod sursa (job #2943620)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int> T;
vector<int> h;
int cautaretata(int x)
{
while(T[x]!=x)
x=T[x];
return x;
}
int main()
{
int n,m,cod,a,b,i;
f>>n>>m;
for(i=0;i<n;++i)
{
T.push_back(i);
h.push_back(1);
}
for(i=0;i<m;++i)
{
f>>cod>>a>>b;
if(cod==1)
{
int x=cautaretata(a-1);
int y=cautaretata(b-1);
if(h[x]<=h[y])
{
T[x]=y;
h[y]=h[y]+h[x];
}
else
{ T[y]=x;
h[x]=h[x]+h[y];
}
}
else if(cod==2)
{
int x=cautaretata(a-1);
int y=cautaretata(b-1);
if(x==y)
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
}
return 0;
}