# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
546542 |
2022-04-07T18:49:57 Z |
Soumya1 |
Pipes (CEOI15_pipes) |
C++17 |
|
524 ms |
65536 KB |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 101 + 1;
const int lg = 18;
int s[mxN];
int par[mxN][lg];
int p[mxN];
vector<int> ad[mxN];
int d[mxN], sz[mxN];
void dfs(int u, int pp) {
for (int j = 1; j < lg; j++) par[u][j] = par[par[u][j - 1]][j - 1];
for (int v : ad[u]) {
if (v == pp) continue;
d[v] = d[u] + 1;
dfs(v, u);
}
}
void unite_node(int u, int v) {
if (sz[par[u][lg - 1]] > sz[par[v][lg - 1]]) swap(u, v);
sz[par[v][lg - 1]] += sz[par[u][lg - 1]];
par[u][0] = v;
d[u] = d[v] + 1;
ad[u].push_back(v);
ad[v].push_back(u);
dfs(u, v);
}
int find(int u) {
return p[u] = (u == p[u] ? u : find(p[u]));
}
void unite_component(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
if (s[u] > s[v]) swap(u, v);
p[u] = v;
s[v] += s[u];
}
int find_next(int u) {
for (int j = lg - 1; j >= 0; j--) {
if (find(par[u][j]) == find(u)) {
u = par[u][j];
}
}
if (find(u) == find(par[u][0])) return -1;
return par[u][0];
}
int lca(int u, int v) {
if (d[u] > d[v]) swap(u, v);
for (int j = lg - 1; j >= 0; j--) {
if (d[u] <= d[par[v][j]]) v = par[v][j];
}
if (u == v) return u;
for (int j = lg - 1; j >= 0; j--) {
if (par[u][j] != par[v][j]) u = par[u][j], v = par[v][j];
}
return par[u][0];
}
void unite_path(int u, int v) {
int l = lca(u, v);
vector<int> vv;
while (u != l && u != -1) {
vv.push_back(u);
u = find_next(u);
}
if (u != -1) vv.push_back(u);
while (v != -1 && v != l) {
vv.push_back(v);
v = find_next(v);
}
for (int x : vv) unite_component(x, u);
}
void testCase() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
p[i] = i;
s[i] = 1;
for (int j = 0; j < lg; j++) par[i][j] = i;
sz[i] = 1;
}
while (m--) {
int u, v;
cin >> u >> v;
if (par[u][lg - 1] != par[v][lg - 1]) {
unite_node(u, v);
} else if (find(u) != find(v)) {
unite_path(u, v);
}
}
for (int i = 1; i <= n; i++) {
if (find(i) == i && par[i][0] != i) {
cout << i << " " << par[i][0] << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
524 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
476 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |