Pagini recente » Cod sursa (job #2474793) | Cod sursa (job #2116466) | Cod sursa (job #2511122) | Cod sursa (job #2740695) | Cod sursa (job #2397181)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int gasesteParinte(vector<int> &p, int i)
{
while(p[i]!=i)
{
p[i]=p[p[i]];
i=p[i];
}
return i;
}
void Union(vector<int> &com,vector<int> &parinte, int x, int y)
{
if(com[x]<com[y])
{
parinte[x]=parinte[y];
com[y]+=com[x];
}else
{
parinte[y]=parinte[x];
com[x]+=com[y];
}
}
int main()
{
int n,m;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
int op,op1,op2;
vector<int> componente(n+1,1);
vector<int> parinte(n+1);
for(int i=1;i<=n;i++)
parinte[i]=i;
for(int i=0;i<m;i++)
{
f>>op>>op1>>op2;
switch(op)
{
case 1:
{
Union(componente,parinte,op1,op2);
break;
}
case 2:
{
if(gasesteParinte(parinte,op1)==gasesteParinte(parinte,op2))
g<<"DA"<<endl;
else g<<"NU"<<endl;
break;
}
}
}
return 0;
}