답안 #146276

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146276 2019-08-23T09:26:50 Z evpipis Pipes (CEOI15_pipes) C++11
40 / 100
5000 ms 46360 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+5;
int vis[len], sum[len], n, m;
vector<ii> adj[len];
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 fix(int u){
    vis[u] = 1;
 
    for (int j = 0; j < lca[u].size(); j++){
        int v = lca[u][j];
        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;
 
        fix(v.fi);
        loc.join(u, v.fi);
    }
}
 
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;
            lca[i].clear();
        }
 
        for (int i = 0; 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)){
                adj[a].pb(mp(b, 1));
                adj[b].pb(mp(a, 1));
                gen.join(a, b);
            }
            else{
                lca[a].pb(b);
                lca[b].pb(a);
            }
        }
 
        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:45:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < lca[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
pipes.cpp:54: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:67: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:87: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:99: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:118: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 5112 KB Output is correct
2 Correct 6 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5496 KB Output is correct
2 Correct 13 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 274 ms 5652 KB Output is correct
2 Correct 261 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 524 ms 6484 KB Output is correct
2 Correct 610 ms 6348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1051 ms 8152 KB Output is correct
2 Runtime error 784 ms 21864 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
# 결과 실행 시간 메모리 Grader output
1 Correct 1786 ms 13500 KB Output is correct
2 Runtime error 1532 ms 31628 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
# 결과 실행 시간 메모리 Grader output
1 Correct 3098 ms 15052 KB Output is correct
2 Runtime error 2224 ms 46360 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4153 ms 17392 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 5034 ms 18604 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5015 ms 31112 KB Time limit exceeded
2 Halted 0 ms 0 KB -