Mai intai trebuie sa te autentifici.
Cod sursa(job #1591518)
| Utilizator | Data | 6 februarie 2016 13:04:20 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 70 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.81 kb |
#include <fstream>
#define NMax 100010
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, queries, set[NMax];
void doUnion(int x, int y)
{
while (set[x] > 0)
x = set[x];
while (set[y] > 0)
y = set[y];
if (x != y) {
if (-set[x] > -set[y]) {
set[x] += -set[y];
set[y] = x;
}
else {
set[y] += -set[x];
set[x] = y;
}
}
}
char* sameSet(int x, int y)
{
while (set[x] > 0)
x = set[x];
while (set[y] > 0)
y = set[y];
if (x == y)
return "DA\n";
else
return "NU\n";
}
int main()
{
f >> n >> queries;
for (int i = 1; i <= n; i++)
set[i] = -1;
int cmd, x, y;
for (int i = 1; i <= queries; i++) {
f >> cmd >> x >> y;
if (cmd == 1)
doUnion(x, y);
else
g << sameSet(x, y);
}
}