답안 #32832

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
32832 2017-10-15T20:09:25 Z imaxblue Pipes (CEOI15_pipes) C++14
100 / 100
1164 ms 10028 KB
#include<vector>
#include<iostream>
#include<cstdio>
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)

#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;

int n, m, t, a, b, X2, Y2, P,
    r[100001], p[100001], h[100001], d[100001];
bool u[100001];
vector<int> com[100001];
bool mrg(int A, int B){
    if (A==B) return 0;
    if (com[A].size()<com[B].size()) return mrg(B, A);
    for (auto i:com[B]){
        com[A].pb(i);
        r[i]=A;
    }
    return 1;
}
void dfs(int N, int P){
    if ((p[N]=P)>0) d[N]=d[P]+1;
    fox(l, com[N].size()){
        if (com[N][l]==P) continue;
        dfs(com[N][l], N);
    }
}
int lca(int X, int Y){
    X=r[X]; Y=r[Y];
    while(X!=Y){
        if (d[X2=h[X]]>d[Y2=h[Y]]){
            u[X2]=1;
            t=h[r[p[X2]]];
            mrg(X, r[p[X2]]);
            h[X]=t;
        } else {
            u[Y2]=1;
            t=h[r[p[Y2]]];
            mrg(Y, r[p[Y2]]);
            h[Y]=t;
        }
        X=r[X]; Y=r[Y];
    }
}
int q[100001];
int main(){
    scan(n); scan(m);
    fox1(l, n){
        r[l]=l;
        com[l].clear(); com[l].pb(l);
    }
    fox(l, m){
        scan(a); scan(b);
        if (mrg(r[a], r[b])){
            //cout << l << endl;
            h[P]=a;
            d[P]=b;
            q[P++]=l;
        }
    }
    fox1(l, n){
        com[l].clear();
    }
    fox(l, P){
        com[h[l]].pb(d[l]);
        com[d[l]].pb(h[l]);
    }
    fox1(l, n){
        if (p[l]==0){
            dfs(l, -1);
        }
    }
    fox1(l, n){
        r[l]=l; h[l]=l;
        com[l].clear(); com[l].pb(l);
    }
    P=0;
    rewind(stdin);
    scan(n); scan(m);
    fox(l, m){
        scan(a); scan(b);
        (l==q[P]) ? ++P : lca(a, b);
    }
    fox1(l, n){
        if (!u[l] && p[l]>0){
            printf("%i %i\n", l, p[l]);
        }
    }
    return 0;
}

Compilation message

pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:13:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fox(k, x) for (int k=0; k<x; ++k)
pipes.cpp:34:9:
     fox(l, com[N].size()){
         ~~~~~~~~~~~~~~~~          
pipes.cpp:34:5: note: in expansion of macro 'fox'
     fox(l, com[N].size()){
     ^~~
pipes.cpp: In function 'int lca(int, int)':
pipes.cpp:55:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3072 KB Output is correct
2 Correct 7 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 2936 KB Output is correct
2 Correct 103 ms 2972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 3444 KB Output is correct
2 Correct 180 ms 3472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 4304 KB Output is correct
2 Correct 250 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 7800 KB Output is correct
2 Correct 421 ms 8052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 604 ms 8424 KB Output is correct
2 Correct 616 ms 8724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 840 ms 9788 KB Output is correct
2 Correct 799 ms 9896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1016 ms 9836 KB Output is correct
2 Correct 946 ms 9912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1164 ms 9220 KB Output is correct
2 Correct 1141 ms 10028 KB Output is correct