Pagini recente » Cod sursa (job #1290701) | Cod sursa (job #2732081) | Cod sursa (job #655770) | Cod sursa (job #2625601) | Cod sursa (job #2936371)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
vector <int> tata,inaltime;
//vector <int> dimensiune;
int n, k;
void Init()
{
tata.resize(n+1, 0);
inaltime.resize(n+1, 0);
//dimensiune.resize(n+1, 1);
}
int Find(int u)
{
if(tata[u]==0)
return u;
tata[u]=Find(tata[u]);
return tata[u];
}
void Union (int u, int v) //o facem int daca vrem sa returneze dim arborelui obt
{
int reprezU=Find(u);
int reprezV=Find(v);
if (reprezU==reprezV) //nu e neaparat ca se poate
return; //0 daca facem cu int //face testul inainte de apelare
if(inaltime[reprezU]==inaltime[reprezV])
{
tata[reprezV]=reprezU;
//inaltime[reprezU]++;
//dimensiune[reprezU]+=dimensiune[reprezV];
//return dimensiune[reprezU];
}
else if(inaltime[reprezU]>inaltime[reprezV])
{
tata[reprezV]=reprezU;
//dimensiune[reprezU]+=dimensiune[reprezV];
//return dimensiune[reprezU];
}
else
{
tata[reprezU]=reprezV;
//dimensiune[reprezV]+=dimensiune[reprezU];
//return dimensiune[reprezV];
}
}
void afis()
{
for (int i=1; i<=n; i++)
cout<<tata[i]<<" ";
cout<<endl;
for (int i=1; i<=n; i++)
cout<<inaltime[i]<<" ";
cout<<endl;
}
int main()
{
f>>n>>k;
Init();
//int dimMax=0, dimNoua;
for (int i=0; i<k; i++)
{
int op;
f>>op;
int x, y;
f>>x>>y;
if(op==1)
Union(x, y);
else
{
if(Find(x)!=Find(y))
g<<"NU\n";
else g<<"DA\n";
}
}
//afis();
return 0;
}