# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
563843 |
2022-05-18T07:26:56 Z |
piOOE |
Pipes (CEOI15_pipes) |
C++17 |
|
2818 ms |
21116 KB |
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define sz(x) ((int)size(x))
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
const int N = 100000, M = 6000000;
int p[2][N];
int get(int a, int i) {
return (p[i][a] != a ? p[i][a] = get(p[i][a], i) : a);
}
bool same(int a, int b, int i) {
return get(a, i) == get(b, i);
}
bool mrg(int a, int b, int i) {
a = get(a, i), b = get(b, i);
if (a == b) return false;
p[i][b] = a;
return true;
}
vector<pair<int, int>> g[N];
//binary jumps with O(n) memory
//https://peltorator.ru/posts/linear_binups/
int jump[N], parent[N], depth[N], id_p[N];
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
while (depth[a] > depth[b]) {
if (depth[jump[a]] >= depth[b]) {
a = jump[a];
} else {
a = parent[a];
}
}
while (a != b) {
if (jump[a] != jump[b]) {
a = jump[a];
b = jump[b];
} else {
a = parent[a];
b = parent[b];
}
}
return a;
}
void add_leaf(int v, int par) {
assert(par != -1 && par != v);
parent[v] = par;
depth[v] = depth[par] + 1;
if (depth[par] - depth[jump[par]] == depth[jump[par]] - depth[jump[jump[par]]]) {
jump[v] = jump[jump[par]];
} else {
jump[v] = par;
}
}
int A[N], B[N], sz[N];
bool usedE[N];
//used edges
//d1 is just our tree, d2 is for merging in a path
void dfs(int v, int par, int id) {
add_leaf(v, par);
p[1][v] = v;
id_p[v] = id;
for (auto [to, i]: g[v]) {
if (to != par) {
dfs(to, v, i);
}
}
}
void union_path(int a, int b) {
int c = lca(a, b);
while (depth[a = get(a, 1)] > depth[c]) {
usedE[id_p[a]] = true;
mrg(parent[a], a, 1);
}
while (depth[b = get(b, 1)] > depth[c]) {
usedE[id_p[b]] = true;
mrg(parent[b], b, 1);
}
}
void merge(int a, int b, int idx) {
int pa = get(a, 0);
int pb = get(b, 0);
if (sz[pa] < sz[pb]) {
swap(a, b);
swap(pa, pb);
}
g[a].emplace_back(b, idx);
g[b].emplace_back(a, idx);
dfs(b, a, idx);
mrg(pa, pb, 0);
sz[pa] += sz[pb];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
iota(jump, jump + N, 0);
iota(parent, parent + N, 0);
iota(p[0], p[0] + N, 0);
iota(p[1], p[1] + N, 0);
fill(sz, sz + N, 1);
int n, m;
cin >> n >> m;
int idx = 0;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
if (same(a, b, 0)) {
union_path(a, b);
} else {
A[idx] = a, B[idx] = b;
merge(a, b, idx);
++idx;
}
}
for (int i = 0; i < idx; ++i) {
if (!usedE[i]) {
cout << A[i] + 1 << " " << B[i] + 1 << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4820 KB |
Output is correct |
2 |
Correct |
6 ms |
4820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
4820 KB |
Output is correct |
2 |
Correct |
138 ms |
4820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
5076 KB |
Output is correct |
2 |
Correct |
271 ms |
5124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
423 ms |
5844 KB |
Output is correct |
2 |
Correct |
354 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
654 ms |
8268 KB |
Output is correct |
2 |
Correct |
576 ms |
8396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1039 ms |
8756 KB |
Output is correct |
2 |
Correct |
988 ms |
8640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1520 ms |
9752 KB |
Output is correct |
2 |
Correct |
1433 ms |
9884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2263 ms |
9804 KB |
Output is correct |
2 |
Correct |
2559 ms |
9884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2818 ms |
9548 KB |
Output is correct |
2 |
Runtime error |
2398 ms |
21116 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |