Pagini recente » Cod sursa (job #857704) | Cod sursa (job #1717889) | Cod sursa (job #1803496) | Cod sursa (job #917695) | Cod sursa (job #2397707)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int gasesteParinte(vector<int> &p, int i)
{
if(p[i]==-1)
return i;
else return gasesteParinte(p,p[i]);
}
void Union(vector<int> &p, int x, int y)
{
int xset = gasesteParinte(p, x);
int yset = gasesteParinte(p, y);
if(xset!=yset){
p[xset] = yset;
}
}
int main()
{
long int n,m;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
int op,op1,op2;
vector<int> parinte(n+1,-1);
for(int i=0;i<m;i++)
{
f>>op>>op1>>op2;
switch(op)
{
case 1:
{
if(gasesteParinte(parinte,op1)!=gasesteParinte(parinte,op2))
Union(parinte,op1,op2);
break;
}
case 2:
{
if(gasesteParinte(parinte,op1)==gasesteParinte(parinte,op2))
g<<"DA\n";
else g<<"NU\n";
break;
}
}
}
return 0;
}