답안 #146314

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146314 2019-08-23T12:37:41 Z evpipis Pipes (CEOI15_pipes) C++11
70 / 100
5000 ms 18772 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;

const int len = 1e5+3;
int n, m, cnt;
int par[len], sum[len], star[len], ord[len];
vector<ii> adj[len], vec;
vector<int> lca[len];

struct dsu{
    int anc[len], sz[len], low[len];

    void init(){
        for (int i = 1; i <= n; i++)
            anc[i] = i, sz[i] = 1, low[i] = i;
    }

    int fin(int i){
        if (anc[i] == i) return i;
        return anc[i] = fin(anc[i]);
    }

    void join(int i, int j){
        i = fin(i), j = fin(j);
        if (i == j) return;

        if (sz[i] > sz[j])
            sz[i] += sz[j], anc[j] = i;
        else
            sz[j] += sz[i], anc[i] = j;
        low[j] = low[i];
    }
};

dsu gen, loc;

void build(int u){
    cnt++;
    star[u] = cnt;
    ord[cnt] = u;

    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (v.fi == par[u])
            continue;

        par[v.fi] = u;
        build(v.fi);
    }
}


void fix(int u){
    for (int j = 0; j < lca[u].size(); j++){
        int v = lca[u][j];

        //printf("lca: %d - %d\n", u, v);

        sum[u]++;
        sum[v]++;
        sum[loc.low[loc.fin(v)]] -= 2;
    }

    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (par[u] == v.fi)
            continue;

        fix(v.fi);
        loc.join(u, v.fi);
    }
}

void dfs(int u){
    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];

        if (par[u] != v.fi){
            //dfs(v.fi);
            sum[u] += sum[v.fi];

            if (sum[v.fi])
                adj[u][j].se = 0;
        }
        else if (sum[u])
            adj[u][j].se = 0;
    }
}

void print(int u){
    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (par[u] == v.fi)
            continue;

        if (v.se)
            printf("%d %d\n", u, v.fi);
        print(v.fi);
    }
}

int main(){
    scanf("%d %d", &n, &m);

    gen.init();

    while (m > 0){
        int k = min(n, m);
        m -= k;

        //printf("round starts with k = %d\n", k);

        cnt = 0;
        loc.init();
        vec.clear();
        for (int i = 1; i <= n; i++){
            par[i] = 0;
            sum[i] = 0;
            lca[i].clear();
            //vector<int>().swap(lca[i]);
        }

        for (int i = 1; i <= k; i++){
            int a, b;
            scanf("%d %d", &a, &b);

            //printf("edge: a = %d, b = %d\n", a, b);

            if (gen.fin(a) != gen.fin(b)){
                //printf("tree edge\n");
                adj[a].pb(mp(b, 1));
                adj[b].pb(mp(a, 1));
                gen.join(a, b);
            }
            else{
                vec.pb(mp(a, b));
                //printf("cross edge\n");
            }
        }

        for (int i = 1; i <= n; i++)
            if (!par[i])
                build(i);

        for (int i = 0; i < vec.size(); i++){
            int a = vec[i].fi, b = vec[i].se;
            if (star[a] > star[b])
                swap(a, b);

            lca[b].pb(a);
            //val[i+1] = a;
            //pre[i+1] = po[b];
            //po[b] = i+1;
        }

        for (int i = 1; i <= n; i++)
            if (!par[i])
                fix(i);
        for (int i = n; i >= 1; i--)
            dfs(ord[i]);
    }

    for (int i = 1; i <= n; i++)
        if (!par[i])
            print(i);
    return 0;
}

Compilation message

pipes.cpp: In function 'void build(int)':
pipes.cpp:48:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp: In function 'void fix(int)':
pipes.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < lca[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp:70:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp: In function 'void dfs(int)':
pipes.cpp:81:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp: In function 'void print(int)':
pipes.cpp:97:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:151:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < vec.size(); i++){
                         ~~^~~~~~~~~~~~
pipes.cpp:109: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:131:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5116 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5624 KB Output is correct
2 Correct 13 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 6164 KB Output is correct
2 Correct 262 ms 6144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 553 ms 7004 KB Output is correct
2 Correct 608 ms 6712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1014 ms 8564 KB Output is correct
2 Correct 717 ms 8820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1723 ms 13756 KB Output is correct
2 Correct 1341 ms 13468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2815 ms 15304 KB Output is correct
2 Correct 2037 ms 13564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3773 ms 17464 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4960 ms 18772 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5036 ms 17612 KB Time limit exceeded
2 Halted 0 ms 0 KB -