Submission #54917

# Submission time Handle Problem Language Result Execution time Memory
54917 2018-07-05T10:59:40 Z kdh9949 Pipes (CEOI15_pipes) C++17
30 / 100
3169 ms 14132 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100001, H = 17;

int n, m, p[N], c[N], v[N], w[N], d[N], s[N][17], q[N];
vector<int> e[N];

int f(int x){ return p[x] = (x == p[x] ? x : f(p[x])); }

int g(int x, int y, int z){
    if(z) c[x] = 1;
    for(int i : e[x]) if(i != y) v[x] += g(i, x, z);
    v[x] += w[x]; w[x] = 0;
    if(y && z && !v[x]) printf("%d %d\n", x, y);
    return v[x];
}

void h(int x, int y, int z){
    s[x][0] = y;
    for(int i = 1; i < H; i++) s[x][i] = s[s[x][i - 1]][i - 1];
    for(int i : e[x]) if(i != y) h(i, x, z + 1);
}

int a(int x, int y){
    if(d[x] < d[y]) swap(x, y);
    for(int i = H - 1; i >= 0; i--) if(d[x] - (1 << i) >= d[y]) x = s[x][i];
    if(x == y) return x;
    for(int i = H - 1; i >= 0; i--) if(s[x][i] != s[y][i]){
        x = s[x][i]; y = s[y][i];
    }
    return s[x][0];
}

int main(){
    scanf("%d%d", &n, &m);
    iota(p, p + n + 1, 0);
    fill(c + 1, c + n + 1, 1);
    for(int x, y; m--; ){
        scanf("%d%d", &x, &y);
        if(f(x) != f(y)){
            if(c[f(x)] < c[f(y)]) swap(x, y);
            g(f(y), 0, 0);
            e[x].push_back(y); e[y].push_back(x);
            h(y, x, d[x] + 1);
            c[f(x)] += c[f(y)]; p[f(y)] = f(x);
        }
        else{ w[x]++; w[y]++; w[a(x, y)] -= 2; }
    }
    fill(c + 1, c + n + 1, 0);
    for(int i = 1; i <= n; i++) if(!c[i]) g(i, 0, 1);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Incorrect 3 ms 2660 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3200 KB Output is correct
2 Incorrect 10 ms 3200 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 165 ms 3072 KB Output is correct
2 Incorrect 163 ms 3072 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 312 ms 3824 KB Output is correct
2 Incorrect 363 ms 3804 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 553 ms 5416 KB Output is correct
2 Correct 433 ms 6008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 785 ms 10660 KB Output is correct
2 Incorrect 735 ms 10648 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 1216 ms 11772 KB Output is correct
2 Correct 1139 ms 11900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1840 ms 13956 KB Output is correct
2 Incorrect 1999 ms 14132 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 2449 ms 13956 KB Output is correct
2 Incorrect 2318 ms 13884 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 3169 ms 13432 KB Output is correct
2 Correct 2336 ms 14048 KB Output is correct