# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
362690 |
2021-02-04T06:22:01 Z |
ijxjdjd |
Pipes (CEOI15_pipes) |
C++17 |
|
3478 ms |
44296 KB |
#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int MAXN = 100000;
const int MAXK = 17;
int up[MAXN][MAXK];
int sm[MAXN];
int rt[MAXN];
int sz[MAXN];
bool bad[MAXN];
int depth[MAXN];
vector<int> ch[MAXN];
int cnt = 0;
void makeBad(int n, int p) {
for (int e : ch[n]) {
if (e != p) {
makeBad(e, n);
sm[n] += sm[e];
}
}
if (sm[n] > 0) {
bad[n] = true;
}
}
void reset(int n, int p, int d, int o) {
up[n][0] = p;
sm[n] = 0;
rt[n] = o;
depth[n] = d;
for (int i = 1; i < MAXK; i++) {
up[n][i] = up[up[n][i-1]][i-1];
}
for (int e : ch[n]) {
if (e != p) {
reset(e, n, d+1, o);
}
}
}
int getUp(int u, int k) {
for (int i = 0; i < MAXK; i++) {
if (k&(1<<i)) {
u = up[u][i];
}
}
return u;
}
int lca(int u, int v) {
if (depth[u] > depth[v]) {
swap(u, v);
}
v = getUp(v, depth[v] - depth[u]);
if (u == v) {
return u;
}
for (int k = MAXK-1; k >= 0; k--) {
if (up[u][k] != up[v][k]) {
u = up[u][k];
v = up[v][k];
}
}
return up[u][0];
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int N, M;
cin >> N >> M;
FR(i, N) {
up[i][0] = -1;
}
FR(i, M) {
int u, v;
cin >> u >> v;
u--, v--;
if (up[u][0] == -1 && up[v][0] == -1) {
ch[u].push_back(v);
ch[v].push_back(u);
reset(u, u, 0, u);
sz[u] = 2;
}
else {
if (up[u][0] == -1 || up[v][0] == -1) {
ch[u].push_back(v);
ch[v].push_back(u);
if (up[v][0] == -1) {
swap(u, v);
}
reset(u, v, depth[v]+1, rt[v]);
sz[rt[v]]++;
}
else {
if (rt[u] == rt[v]) {
int x = lca(u, v);
sm[x] -= 2;
sm[u]++;
sm[v]++;
}
else {
if (sz[rt[u]] > sz[rt[v]]) {
swap(u, v);
}
int j = u;
bool last = false;
makeBad(rt[u], rt[u]);
while (true) {
swap(bad[j], last);
if (j == up[j][0]) {
break;
}
j = up[j][0];
}
ch[u].push_back(v);
ch[v].push_back(u);
sz[rt[v]] += sz[rt[u]];
reset(u, v, depth[v]+1, rt[v]);
}
}
}
}
FR(i, N) {
if (up[i][0] == i) {
makeBad(i, i);
}
}
FR(i, N) {
if (up[i][0] != i && up[i][0] != -1 && !bad[i]) {
cout << i+1 << " " << up[i][0] + 1 << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3308 KB |
Output is correct |
2 |
Correct |
8 ms |
3328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
5484 KB |
Output is correct |
2 |
Correct |
177 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
6252 KB |
Output is correct |
2 |
Correct |
375 ms |
6252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
578 ms |
7788 KB |
Output is correct |
2 |
Runtime error |
490 ms |
20228 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1080 ms |
10732 KB |
Output is correct |
2 |
Runtime error |
932 ms |
28908 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1743 ms |
14384 KB |
Output is correct |
2 |
Runtime error |
1381 ms |
24300 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2380 ms |
16492 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2961 ms |
14188 KB |
Output is correct |
2 |
Runtime error |
2962 ms |
35308 KB |
Memory limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3478 ms |
13548 KB |
Output is correct |
2 |
Runtime error |
2902 ms |
44296 KB |
Memory limit exceeded |