Pagini recente » Cod sursa (job #3316094) | Cod sursa (job #3349372) | Cod sursa (job #1782896) | Cod sursa (job #2854565) | Cod sursa (job #3300934)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int cod = 1;
int find_root(vector<int>&father ,int son)
{
if(father[son]<=-1) return son;
return father[son] = find_root(father , father[son]);
}
void Union(vector<int>& father , int x , int y)
{
int rx = find_root(father , x);
int ry = find_root(father , 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;
}