Mai intai trebuie sa te autentifici.
Cod sursa(job #2973588)
| Utilizator | Data | 1 februarie 2023 12:45:29 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.8 kb |
#include <fstream>
using namespace std;
int n,m,c,x,y,root[100001],len[100001];
int cauta(int x)
{
if (root[x]==x)
return x;
root[x]=cauta(root[x]);
return root[x];
}
int merg (int a,int b)
{
if (len[a]<len[b])
swap(a,b);
len[a]+=len[b];
root[b]=a;
}
int main()
{
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
f>>n>>m;
for (int i=1;i<=n;i++)
root[i]=i,len[i]=1;
for (int i=1;i<=m;i++)
{
f>>c>>x>>y;
if (c==1)
{
int xs=cauta(x),ys=cauta(y);
if (xs!=ys)
merg(xs,ys);
}
else
{
int xs=cauta(x),ys=cauta(y);
if (xs==ys) g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
}
