Submission #927855

# Submission time Handle Problem Language Result Execution time Memory
927855 2024-02-15T12:07:39 Z 79brue Pipes (CEOI15_pipes) C++17
20 / 100
5000 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int K = 17;

vector<int> link[100002];
int par[100002], LCADB[100002][K], sz[100002], depth[100002];
int val[100002];

inline int find(int x){
    if(x==par[x]) return x;
    return find(par[x]);
}

inline void sps_dfs(int x, int p){
    sz[x] = 1;
    for(int i=1; i<K; i++) LCADB[x][i] = LCADB[LCADB[x][i-1]][i-1];
    for(int y: link[x]){
        if(p != y){
            par[y] = LCADB[y][0] = x;
            depth[y] = depth[x] + 1;
            sps_dfs(y, x);
            sz[x] += sz[y];
        }
    }
}

inline void merge(int x, int y){
    int rx = find(x), ry = find(y);
    if(rx==ry) return;
    if(sz[rx] < sz[ry]) swap(rx, ry), swap(x, y);
    link[x].push_back(y), link[y].push_back(x);
    LCADB[y][0] = par[y] = x;
    depth[y] = depth[x] + 1;
    sps_dfs(y, x);

    int tmp = sz[y];
    while(true){
        sz[x] += tmp;
        y = x;
        if(x==par[x]) break;
        x = par[x];
    }
}

inline int getLCA(int x, int y){
    if(depth[x] > depth[y]) swap(x, y);
    for(int d=1; d<K; d++) if((depth[y]-depth[x])&(1<<d)) y = LCADB[y][d];
    if(x==y) return x;
    for(int d=K-1; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
    return par[x];
}

int n, m;

void dfs(int x, int p=-1){
    for(int y: link[x]){
        if(y==p) continue;
        dfs(y, x);
        val[x] += val[y];
    }
}

int main(){
    scanf("%d %d", &n, &m);
    for(int i=1; i<=n; i++) par[i] = i, sz[i] = 1;
    while(m--){
        int x, y;
        scanf("%d %d", &x, &y);
        if(find(x) == find(y)){
            val[x]++, val[y]++;
            val[getLCA(x, y)] -= 2;
        }
        else merge(x, y);
    }
    for(int i=1; i<=n; i++) if(par[i] == i) dfs(i);
    for(int i=1; i<=n; i++){
        if(par[i] != i && !val[i]) printf("%d %d\n", par[i], i);
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Incorrect 1 ms 6492 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6744 KB Output is correct
2 Incorrect 5 ms 6876 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 211 ms 6748 KB Output is correct
2 Incorrect 208 ms 6824 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 492 ms 7044 KB Output is correct
2 Incorrect 495 ms 7036 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 853 ms 9604 KB Output is correct
2 Correct 724 ms 8756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1707 ms 12660 KB Output is correct
2 Incorrect 1285 ms 12644 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2638 ms 13760 KB Output is correct
2 Correct 2262 ms 13332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3676 ms 14188 KB Output is correct
2 Incorrect 3344 ms 14316 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4677 ms 14112 KB Output is correct
2 Runtime error 3905 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5031 ms 13920 KB Time limit exceeded
2 Halted 0 ms 0 KB -