# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
423016 |
2021-06-10T15:47:06 Z |
kostia244 |
Pipes (CEOI15_pipes) |
C++17 |
|
2843 ms |
65536 KB |
#include<bits/stdc++.h>//memory usage is jumpting too much
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int maxn = 1e5+1, lg = 11;
struct dsu {
int p[maxn];
dsu(int n = 0) {
for(int i = 0; i <= n; i++) p[i] = -1;
}
int par(int i) { return p[i] < 0 ? i : p[i] = par(p[i]); }
void join(int u, int v) {
u = par(u), v = par(v);
if(u == v) return;
p[u] += p[v];
p[v] = u;
}
bool connected(int u, int v) {
return par(u) == par(v);
}
};
dsu comp;
int n, m;
int h[maxn], eval[maxn], lval[maxn], p[maxn][lg];
array<int, 2> elist[maxn];
vector<array<int, 2>> g[maxn];
void accumulate(int v, int p, int eid) {
for(auto [i, id] : g[v]) if(i != p) {
accumulate(i, v, id);
lval[v] += lval[i];
}
if(eid != -1 && lval[v]) {
eval[eid] += lval[v];
//cout << elist[eid][0] << " " << elist[eid][1] << " += " << lval[v] << endl;
}
}
void attach(int v, int pr) {
lval[v] = 0;
p[v][0] = pr;
for(int i = 1; i < lg; i++) {
p[v][i] = p[v][i-1];
p[v][i] = p[p[v][i]][i-1];
p[v][i] = p[p[v][i]][i-1];
}
for(auto [i, id] : g[v]) if(i != pr) {
h[i] = h[v]+1;
attach(i, v);
}
}
int ESZ = 0;
int lca(int u, int v) {
if(h[u] > h[v]) swap(u, v);
int dif = h[v]-h[u];
for(int i = 0; i < lg; i++) {
if(dif%3) v = p[v][i], dif--;
if(dif%3) v = p[v][i];
dif /= 3;
}
if(u == v) return u;
for(int i = lg; i--;) {
if(p[v][i] != p[u][i]) v = p[v][i], u = p[u][i];
if(p[v][i] != p[u][i]) v = p[v][i], u = p[u][i];
}
return p[u][0];
}
void add_edge(int u, int v) {
if(comp.connected(u, v)) {
int L = lca(u, v);
//cout << u << " " << v << " lca = " << L << endl;
lval[u]++, lval[v]++, lval[L] -= 2;
} else {
int ru = comp.par(u), rv = comp.par(v);
if(-comp.p[ru] < -comp.p[rv]) swap(u, v), swap(ru, rv);
//cout << u<< " is god " << endl;
comp.join(ru, rv);
h[v] = h[u]+1;
accumulate(rv, -1, -1);
attach(v, u);
g[u].push_back({v, ESZ});
g[v].push_back({u, ESZ});
elist[ESZ++] = {u, v};
}
}
void debug(int v, int p = -1) {
for(auto [i, id] : g[v]) if(i != p) {
cout << v << " -> " << i << endl;
debug(i, v);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
comp = dsu(n+1);
for(int f, t; m--;) {
cin >> f >> t;
add_edge(f, t);
//cout << m << endl;
//for(int i = 1; i <= n; i++) if(comp.par(i) == i) {
// debug(i);
//}
}
accumulate(comp.par(1), -1, -1);
for(int i = 0; i < ESZ; i++) if(!eval[i]) cout << elist[i][0] << " " << elist[i][1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3832 KB |
Output is correct |
2 |
Correct |
7 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
3788 KB |
Output is correct |
2 |
Correct |
181 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
4360 KB |
Output is correct |
2 |
Correct |
395 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
576 ms |
5724 KB |
Output is correct |
2 |
Runtime error |
555 ms |
20076 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
813 ms |
10340 KB |
Output is correct |
2 |
Runtime error |
754 ms |
28500 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1264 ms |
11332 KB |
Output is correct |
2 |
Runtime error |
1262 ms |
44504 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1959 ms |
13136 KB |
Output is correct |
2 |
Runtime error |
2005 ms |
54540 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2456 ms |
16264 KB |
Output is correct |
2 |
Runtime error |
2382 ms |
65536 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2843 ms |
28920 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |