Pagini recente » Cod sursa (job #711972) | Cod sursa (job #1962345) | Cod sursa (job #1351133) | Cod sursa (job #676621) | Cod sursa (job #2936716)
#include <iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int>tata,h;
int Find(int u)
{
if (tata[u]==0)return u;
tata[u]=Find(tata[u]);
return tata[u];
}
void Union(int repU, int repV)
{
if(repU==repV)return;
if(h[repU]==h[repV])
{
tata[repV]=repU;
//h[repU]++;
//return;
}
else
if(h[repU]>h[repV])
{
tata[repV]=repU;
//h[repU]++;
// return;
}
else
{
tata[repU]=repV;
// return;
}
}
int main()
{
int n,m;
f>>n>>m;
tata.resize(n+1,0);
h.resize(n+1,0);
int cod,x,y;
for(int i=0;i<m;i++)
{
f>>cod>>x>>y;
int repU=Find(x);
int repV=Find(y);
if(cod==1)
{
Union(repU,repV);
}
else
{
if(repU==repV)
g<<"DA"<<endl;
else
g<<"NU"<<endl;
}
}
return 0;
}