Submission #25731

# Submission time Handle Problem Language Result Execution time Memory
25731 2017-06-23T19:26:59 Z gs14004 Pipes (CEOI15_pipes) C++11
100 / 100
531 ms 14836 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int,int> pi;
  
struct disj{
    int pa[100005];
    void init(int n){
        for(int i=0; i<=n; i++) pa[i] = i;
    }
    int find(int x){
        return pa[x] = (pa[x] == x ? x : find(pa[x]));
    }
    bool uni(int p, int q){
        p = find(p);
        q = find(q);
        if(p == q) return 0;
        pa[q] = p;
        find(q);
        return 1;
    }
}disj1, disj2;
  
int n, m;
pi bri[100005];
vector<int> graph[100005];
  
int par[100005][17], dep[100005];
bool vis[100005];
  
void dfs(int x, int pa){
    vis[x] = 1;
    par[x][0] = pa;
    for (auto &i : graph[x]){
        if(i == pa) continue;
        dep[i] = dep[x] + 1;
        dfs(i,x);
    }
}
  
int lca(int x, int y){
    if(dep[x] < dep[y]) swap(x,y);
    int dx = dep[x] - dep[y];
    for(int i=0; i<17; i++){
        if((dx >> i) & 1) x = par[x][i];
    }
    for(int i=16; i>=0; i--){
        if(par[x][i] != par[y][i]){
            x = par[x][i];
            y = par[y][i];
        }
    }
    if(x != y) x = par[x][0];
    return x;
}
  
int up[100005];
  
void dfs2(int x, int pa){
    vis[x] = 1;
    for (auto &i : graph[x]){
        if(i == pa) continue;
        dfs2(i,x);
        up[x] = max(up[x], up[i] - 1);
    }
    if(pa != 0 && up[x] == 0) printf("%d %d\n",pa,x);
}

int main(){
    scanf("%d %d\n",&n,&m);
    disj1.init(n);
    disj2.init(n);
    int c = 0;
    while(m--){
        int u = 0, v = 0;
        char str[15];
        fgets(str,15,stdin);
        int pos = 0;
        while(str[pos] != ' '){
            u = (10 * u) + (str[pos] - '0');
            pos++;
        }
        pos++;
        while(str[pos] != '\n' && str[pos]){
            v = (10 * v) + (str[pos] - '0');
            pos++;
        }
        if(disj1.uni(u,v)){
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        else if(disj2.uni(u,v)) bri[c++] = pi(u, v);
    }
    for(int i=1; i<=n; i++){
        if(!vis[i]) dfs(i, 0);
    }
    memset(vis, 0, sizeof(vis));
    for(int i=1; i<17; i++){
        for(int j=1; j<=n; j++){
            par[j][i] = par[par[j][i-1]][i-1];
        }
    }
    for(int k=0; k<c; k++){
        pi i = bri[k];
        int l = lca(i.first,i.second);
        up[i.first] = max(up[i.first],dep[i.first] - dep[l]);
        up[i.second] = max(up[i.second],dep[i.second] - dep[l]);
    }
    for(int i=1; i<=n; i++){
        if(!vis[i]) dfs2(i, 0);     
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d\n",&n,&m);
     ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:79:14: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         fgets(str,15,stdin);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2816 KB Output is correct
2 Correct 3 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3328 KB Output is correct
2 Correct 6 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 3140 KB Output is correct
2 Correct 33 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 3936 KB Output is correct
2 Correct 62 ms 3896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 5624 KB Output is correct
2 Correct 105 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 11252 KB Output is correct
2 Correct 186 ms 11244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 12484 KB Output is correct
2 Correct 278 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 14800 KB Output is correct
2 Correct 363 ms 14812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 14812 KB Output is correct
2 Correct 430 ms 14836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 531 ms 14188 KB Output is correct
2 Correct 486 ms 14428 KB Output is correct