Submission #146315

# Submission time Handle Problem Language Result Execution time Memory
146315 2019-08-23T12:39:03 Z evpipis Pipes (CEOI15_pipes) C++
90 / 100
5000 ms 15464 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);
             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5496 KB Output is correct
2 Correct 13 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 5480 KB Output is correct
2 Correct 291 ms 5444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 558 ms 6052 KB Output is correct
2 Correct 655 ms 6064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 975 ms 7536 KB Output is correct
2 Correct 729 ms 8052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1644 ms 12400 KB Output is correct
2 Correct 1388 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2663 ms 13268 KB Output is correct
2 Correct 2177 ms 12848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3826 ms 15092 KB Output is correct
2 Correct 3613 ms 15436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4743 ms 15176 KB Output is correct
2 Correct 4763 ms 15464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5005 ms 14684 KB Time limit exceeded
2 Halted 0 ms 0 KB -