답안 #927856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927856 2024-02-15T12:09:42 Z 79brue Pipes (CEOI15_pipes) C++17
20 / 100
5000 ms 15404 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){
    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);
        }
    }
}

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, sz[rx] += sz[ry];
    depth[y] = depth[x] + 1;
    sps_dfs(y, 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:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Incorrect 1 ms 6488 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6748 KB Output is correct
2 Incorrect 5 ms 6748 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 6748 KB Output is correct
2 Incorrect 193 ms 6748 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 484 ms 7256 KB Output is correct
2 Incorrect 471 ms 7000 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 797 ms 9600 KB Output is correct
2 Correct 709 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1604 ms 12884 KB Output is correct
2 Incorrect 1223 ms 12884 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2553 ms 13580 KB Output is correct
2 Correct 2161 ms 13568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3533 ms 14252 KB Output is correct
2 Incorrect 3134 ms 14128 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4615 ms 14104 KB Output is correct
2 Incorrect 3744 ms 15404 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5021 ms 13908 KB Time limit exceeded
2 Halted 0 ms 0 KB -