Mai intai trebuie sa te autentifici.
Cod sursa(job #2753209)
Utilizator | Data | 21 mai 2021 15:43:58 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.79 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m, cod, x, y, v[100041];
int fiind(int x)
{
if (v[x]==x)
return v[x];
else
{
v[x]=fiind(v[x]);
return v[x];
}
}
void uni(int x, int y)
{
v[fiind(y)]=fiind(x);
}
void check(int x, int y)
{
if (fiind(x)==fiind(y))
fout << "DA\n";
else
fout << "NU\n";
}
void read()
{
fin >> n >> m;
for (int i=0;i<=n;++i)
v[i]=i;
for (int i=0;i<m;++i)
{
fin >> cod >> x >> y;
if (cod==1)
uni(x,y);
else
{
check(x,y);
}
}
}
int main()
{
read();
return 0;
}