답안 #146281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146281 2019-08-23T09:41:36 Z evpipis Pipes (CEOI15_pipes) C++11
90 / 100
4510 ms 65536 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], po[len], val[len], pre[len], star[len], ord[len];
vector<ii> adj[len], vec;

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){
    while (po[u] > 0){
        int v = val[po[u]];
        po[u] = pre[po[u]];

        //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;
            po[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);

            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 = 1; i <= n; i++)
          //  if (!par[i])
            //    dfs(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:47: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: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:99: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:154:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < vec.size(); i++){
                         ~~^~~~~~~~~~~~
pipes.cpp:111: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:134: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 4 ms 2808 KB Output is correct
2 Correct 4 ms 2744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3192 KB Output is correct
2 Correct 10 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 3064 KB Output is correct
2 Correct 237 ms 3148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 466 ms 3704 KB Output is correct
2 Correct 557 ms 3744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 818 ms 5072 KB Output is correct
2 Correct 591 ms 5620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1210 ms 9812 KB Output is correct
2 Correct 1049 ms 9880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2091 ms 10348 KB Output is correct
2 Correct 1688 ms 10308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2756 ms 12140 KB Output is correct
2 Correct 2697 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3742 ms 12136 KB Output is correct
2 Correct 3421 ms 12212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4510 ms 11616 KB Output is correct
2 Runtime error 3645 ms 65536 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)