# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
784194 |
2023-07-15T20:46:42 Z |
NK_ |
Pipes (CEOI15_pipes) |
C++17 |
|
649 ms |
10756 KB |
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
using pi = pair<int, int>;
template<class T> using V = vector<T>;
struct DSU {
V<int> e; void init(int N) { e = V<int>(N, -1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int x, int y) { return get(x) == get(y); }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return 0;
e[y] = x; return 1;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
V<V<int>> ADJ(N);
V<int> ID(N), SIZ(N), PAR(N), DEP(N);
DSU D; D.init(N); // for BCC
// ID and SIZ are part of the DSU
// while ADJ, PAR, DEP, and S store info about all the nodes
// of the spanning "tree" in its current state (AKA not necessarily connected and subject to change)
for(int i = 0; i < N; i++) {
ID[i] = i, SIZ[i] = 1;
PAR[i] = -1;
ADJ[i] = {};
}
int id = -1;
V<int> upd;
function<void(int)> dfs = [&](const int &u) {
if (PAR[u] != -1 && D.sameSet(PAR[u], u)) upd.pb(u);
for(auto &v : ADJ[u]) if (v != PAR[u]) {
assert(ID[v] == ID[u]);
PAR[v] = u, DEP[v] = DEP[u] + 1;
dfs(v);
}
ID[u] = id;
};
int xi, yi;
auto merge = [&](int x, int y) {
xi = ID[x], yi = ID[y]; if (xi == yi) return 0;
if (SIZ[xi] < SIZ[yi]) swap(xi, yi), swap(x, y);
// cout << xi << " " << yi << endl;
// yi is the smaller one
SIZ[xi] += SIZ[yi];
// RECOMPUTE PARENTS FOR yi AND CHANGE ID TO xi FOR EACH NODE
// ADD EDGE BETWEEN x AND y
PAR[y] = x;
DEP[y] = DEP[x] + 1;
id = xi;
// dfs(y);
ADJ[x].pb(y); ADJ[y].pb(x);
for(auto &u : upd) D.e[u] = PAR[u];
upd = {};
return 1;
};
V<pair<int, int>> E;
int u, v, ui, vi;
for(int e = 0; e < M; e++) {
cin >> u >> v; u = u - 1, v = v - 1;
// if (!merge(u, v)) { // if not part of the spanning "tree"
// while(1) {
// ui = D.get(u), vi = D.get(v);
// // cout << u << " -> " << ui << endl;
// // cout << v << " -> " << vi << endl;
// if (ui == vi) break;
// if (DEP[ui] < DEP[vi]) swap(ui, vi);
// D.unite(PAR[ui], ui); // MAKE PAR[ui] the parent of ui
// }
// } else { // part of spanning "tree" (possible bridge)
// // cout << "ST: " << u << " <-> " << v << endl;
// E.pb({u, v});
// }
}
// for(auto& e : E) if (!D.sameSet(e.f, e.s)) {
// cout << e.f + 1 << " " << e.s + 1 << endl;
// }
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:61:7: warning: variable 'merge' set but not used [-Wunused-but-set-variable]
61 | auto merge = [&](int x, int y) {
| ^~~~~
pipes.cpp:90:12: warning: unused variable 'ui' [-Wunused-variable]
90 | int u, v, ui, vi;
| ^~
pipes.cpp:90:16: warning: unused variable 'vi' [-Wunused-variable]
90 | int u, v, ui, vi;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
468 KB |
Output is correct |
2 |
Incorrect |
62 ms |
340 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
724 KB |
Output is correct |
2 |
Incorrect |
117 ms |
724 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
1236 KB |
Output is correct |
2 |
Incorrect |
143 ms |
1492 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
3312 KB |
Output is correct |
2 |
Incorrect |
173 ms |
3300 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
3768 KB |
Output is correct |
2 |
Incorrect |
319 ms |
3740 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
4572 KB |
Output is correct |
2 |
Incorrect |
400 ms |
4580 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
523 ms |
4568 KB |
Output is correct |
2 |
Incorrect |
494 ms |
4580 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
649 ms |
4356 KB |
Output is correct |
2 |
Incorrect |
635 ms |
10756 KB |
Wrong number of edges |
3 |
Halted |
0 ms |
0 KB |
- |