답안 #146267

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146267 2019-08-23T09:03:32 Z evpipis Pipes (CEOI15_pipes) C++11
40 / 100
4605 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 vis[len], sum[len], po[len], val[2*len], pre[2*len], n, m;
vector<ii> adj[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 fix(int u){
    vis[u] = 1;

    while (po[u] > 0){
        int v = val[po[u]];
        po[u] = pre[po[u]];

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

        if (vis[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 (vis[v.fi])
            continue;

        //par[v.fi] = u;
        fix(v.fi);
        loc.join(u, v.fi);
    }

    //order[cnt++] = u;
}

void dfs(int u){
    vis[u] = 1;

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

        if (!vis[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){
    vis[u] = 1;

    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (vis[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);

        loc.init();
        for (int i = 1; i <= n; i++){
            vis[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{
                //printf("cross edge\n");
                val[2*i] = b;
                pre[2*i] = po[a];
                po[a] = 2*i;

                val[2*i+1] = a;
                pre[2*i+1] = po[b];
                po[b] = 2*i+1;
            }
        }

        for (int i = 1; i <= n; i++)
            if (!vis[i])
                fix(i);

        for (int i = 1; i <= n; i++)
            vis[i] = 0;
        for (int i = 1; i <= n; i++)
            if (!vis[i])
                dfs(i);
    }

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

Compilation message

pipes.cpp: In function 'void fix(int)':
pipes.cpp:57: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:73: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:93: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:105: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:126: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 5 ms 2936 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3192 KB Output is correct
2 Correct 10 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 8412 KB Output is correct
2 Correct 233 ms 8280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 460 ms 13184 KB Output is correct
2 Correct 564 ms 14952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 816 ms 20988 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 1324 ms 29432 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 2138 ms 43004 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 3055 ms 55140 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 3781 ms 65536 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 4605 ms 65536 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -