Pagini recente » Cod sursa (job #3327478) | Cod sursa (job #3300929)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int cod = 1;
int find_root(int son)
{
if(father[son]<=-1)
{
return son;
}
int root = find_root(father[son]);
father[son] = root;
return father[son];
}
void Union(vector<int> &father,int x, int y)
{
int rx = find_root(x);
int ry = find_root(y);
if(rx == ry)
{
fout<<"DA"<<'\n';;
return;
}
if(cod==2)
{
fout<<"NU"<<'\n';
return;
}
if(father[rx]<father[ry])
{
swap(rx,ry);
}
father[ry]+=father[rx]; ///mica optimizare ,daca e radacins in ea retinem si numarul de noduri
father[rx]=y;
}
int main()
{
int n,m;
fin>>n>>m;
vector<int>father(n+1,-1);
for(int i=1;i<=m;i++)
{
int x,y;
fin>>cod>>x>>y;
Union(father,x , y);
}
return 0;
}