답안 #146319

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146319 2019-08-23T12:52:15 Z evpipis Pipes (CEOI15_pipes) C++
70 / 100
5000 ms 18800 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)
            vec.pb(mp(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();
        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]);
      
      	vec.clear();
    }
 
    for (int i = 1; i <= n; i++)
        if (!par[i])
            print(i);
  
  	for (int i = 0; i < vec.size(); i++)
      printf("%d %d\n", vec[i].fi, vec[i].se);
    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:150:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < vec.size(); i++){
                         ~~^~~~~~~~~~~~
pipes.cpp:170:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for (int i = 1; i <= n; i++)
     ^~~
pipes.cpp:174:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
    for (int i = 0; i < vec.size(); i++)
    ^~~
pipes.cpp:174:22: 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:130: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 4984 KB Output is correct
2 Correct 8 ms 5112 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 301 ms 10924 KB Output is correct
2 Correct 291 ms 10744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 562 ms 7544 KB Output is correct
2 Correct 663 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1018 ms 9216 KB Output is correct
2 Correct 796 ms 9688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1882 ms 13296 KB Output is correct
2 Correct 1427 ms 13204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2899 ms 14696 KB Output is correct
2 Correct 2224 ms 14192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3987 ms 17104 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 4947 ms 18800 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 5091 ms 18628 KB Time limit exceeded
2 Halted 0 ms 0 KB -