Mai intai trebuie sa te autentifici.
Cod sursa(job #1907073)
Utilizator | Data | 6 martie 2017 17:41:26 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.96 kb |
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int t[100003], c[100003];
void Initializare()
{
int i, j;
for (i=1; i<=N; i++)
t[i] = 0, c[i] = 1;
}
int Find(int x)
{
int y, z;
y=x;
while (t[x]!=0)
x=t[x];
while (t[y]!=0)
{
z=t[y];
t[y]=x;
y=z;
}
return x;
}
void Union(int x, int y)
{
if (c[x] > c[y])
{
c[x] += c[y];
c[y] = 0;
t[y] = x;
}
else // c[y] <c[x]
{
c[y] += c[x];
c[x] = 0;
t[x] = y;
}
}
int main ()
{
int i, q, x, y;
fin >> N >> M;
Initializare();
for (i=1; i<=M; i++)
{
fin >> q >> x >> y;
if (q==1)
Union(Find(x),Find(y));
else
{
if (Find(x)==Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}