Pagini recente » Cod sursa (job #1110008) | Cod sursa (job #2863349) | Cod sursa (job #2486451) | Cod sursa (job #1775462) | Cod sursa (job #2390569)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
vector<int>parinte(100);
vector<int>adancime (100);
int reprezentant (int x)
{
if (x==parinte[x])
return x;
return reprezentant(parinte[x]);
}
void reuneste (int nod1,int nod2)
{
int rep1=reprezentant(nod1);
int rep2=reprezentant(nod2);
if (adancime[rep1]<adancime[rep2])
parinte[rep1]=rep2;
else
parinte[rep2]=rep1;
if (adancime[rep1]==adancime[rep2])
adancime[rep1]++;
}
bool verifica (int nod1, int nod2)
{
return reprezentant(nod1)==reprezentant(nod2);
}
int main()
{
int n,m,cod,x,y;
f>>n>>m;
for (int i=1; i<=n; i++)
parinte[i]=i;
for (int i=0; i<m; i++)
{
f>>cod>>x>>y;
if (cod==1)
reuneste (x,y);
else
if (verifica (x,y))g<<"DA"<<endl;
else g<<"NU"<<endl;
}
}