Cod sursa(job #3193338)
Utilizator | Data | 14 ianuarie 2024 14:41:19 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 40 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.85 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
int v[10001];
int root(int x)
{
if(v[x]==x)
{
return x;
}
v[x]=root(v[x]);
return v[x];
}
void unite(int x,int y)
{
x=root(x),y=root(y);
if(rand() & 1)
{
v[x]=y;
}
else
{
v[y]=x;
}
}
int main()
{
fin >> n >> m;
for(int i=1;i<=n;i++)
{
v[i]=i;
}
while(m--)
{
int t,x,y;
fin >> t >> x >> y;
if(t==1)
{
unite(x,y);
}
else
{
if(root(x)==root(y))
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
}
}
return 0;
}