Pagini recente » Cod sursa (job #1804288) | Cod sursa (job #2028138) | Cod sursa (job #2228992) | Cod sursa (job #653384) | Cod sursa (job #2936713)
#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;
}