Mai intai trebuie sa te autentifici.
Cod sursa(job #2728665)
Utilizator | Data | 23 martie 2021 15:40:12 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 70 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.65 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int sef[100005];
int findd(int x)
{
if(sef[x]==x)return x;
return findd(sef[x]);
}
void uneste(int x,int y)
{
sef[findd(x)]=findd(y);
}
int main()
{
int n,m,i,x,y,c;
fin>>n>>m;
for(int i=1; i<=n; i++)
{
sef[i]=i;
}
for(i=1; i<=m; i++)
{
fin>>c>>x>>y;
if(c==1)
{
uneste(x,y);
}
else
{
if(findd(x)==findd(y)) fout<<"DA";
else fout<<"NU";
fout<<'\n';
}
}
return 0;
}