Submission #146279

# Submission time Handle Problem Language Result Execution time Memory
146279 2019-08-23T09:38:43 Z evpipis Pipes (CEOI15_pipes) C++11
70 / 100
5000 ms 65032 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 = 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);
             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3192 KB Output is correct
2 Correct 10 ms 3192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 246 ms 3064 KB Output is correct
2 Correct 243 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 484 ms 3704 KB Output is correct
2 Correct 569 ms 3892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 822 ms 5200 KB Output is correct
2 Correct 598 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1280 ms 9728 KB Output is correct
2 Correct 1079 ms 9840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2124 ms 10472 KB Output is correct
2 Correct 1893 ms 10248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3159 ms 12136 KB Output is correct
2 Runtime error 3140 ms 53412 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
# Verdict Execution time Memory Grader output
1 Correct 4014 ms 12364 KB Output is correct
2 Runtime error 3805 ms 65032 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
# Verdict Execution time Memory Grader output
1 Execution timed out 5017 ms 11720 KB Time limit exceeded
2 Halted 0 ms 0 KB -