답안 #929001

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
929001 2024-02-17T12:57:13 Z 79brue Pipes (CEOI15_pipes) C++17
0 / 100
5000 ms 55416 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];
int lost[100002];

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

inline void sps_dfs(int x, int p){
    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);
            val[x] += val[y];
        }
    }
//    if(val[x]) lost[x] = 1;
//    val[x] = 0;
}

inline bool merge(int x, int y){
    int rx = find(x), ry = find(y);
    if(rx==ry) return false;
    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, sz[rx] += sz[ry];
    depth[y] = depth[x] + 1;
    sps_dfs(y, x);
    return true;
}

inline int getLCA(int x, int y){
    if(depth[x] > depth[y]) swap(x, y);
    for(int d=0; 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;

inline 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(!merge(x, y)){
            val[x]++, val[y]++;
            val[getLCA(x, y)] -= 2;
        }
    }
    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] && !lost[i]) printf("%d %d\n", par[i], i);
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7004 KB Output is correct
2 Incorrect 1 ms 7004 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7260 KB Output is correct
2 Incorrect 6 ms 7004 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 218 ms 7012 KB Output is correct
2 Incorrect 230 ms 7000 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 518 ms 7256 KB Output is correct
2 Runtime error 529 ms 18516 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 863 ms 9880 KB Output is correct
2 Runtime error 749 ms 23848 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1734 ms 13644 KB Output is correct
2 Runtime error 1315 ms 31160 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2920 ms 13736 KB Output is correct
2 Runtime error 2367 ms 47124 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4166 ms 14136 KB Output is correct
2 Runtime error 3501 ms 55416 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4649 ms 18688 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5055 ms 25184 KB Time limit exceeded
2 Halted 0 ms 0 KB -