zoukankan      html  css  js  c++  java
  • POJ 3694 Network (算竞进阶习题)

    边双联通分量

    数据很水,随便乱搞。。

    先把所有边双找出来,然后缩点。之后如果新加的边在同一个边双,那桥的数量不变,如果在不同的边双,就一直往上跳到两个点的LCA,通过的边如果没被标记,就标记一下,标记了就不再是桥了。。超级暴力!

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 200005;
    int n, m, cnt, k, tot, t, ans, head[N], dfn[N], low[N], c[N], first[N], cur, p[N][20], depth[N], from[N];
    bool bri[N<<2], kills[N<<2];
    struct Edge { int v, next; } edge[N<<2], e[N<<2];
    
    void addEdge(int a, int b){
        edge[cnt].v = b, edge[cnt].next = head[a], head[a] = cnt ++;
    }
    
    void link(int a, int b){
        e[cur].v = b, e[cur].next = first[a], first[a] = cur ++;
    }
    
    void tarjan(int s, int pre){
        dfn[s] = low[s] = ++k;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(!dfn[u]){
                tarjan(u, i);
                low[s] = min(low[s], low[u]);
                if(low[u] > dfn[s]) bri[i] = bri[i^1] = true;
            }
            else if(i != (pre^1)) low[s] = min(low[s], dfn[u]);
        }
    }
    
    void dfs(int s){
        c[s] = tot;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(c[u] || bri[i]) continue;
            dfs(u);
        }
    }
    
    void dfs(int s, int fa){
        p[s][0] = fa, depth[s] = depth[fa] + 1;
        for(int i = 1; i <= t; i ++){
            p[s][i] = p[p[s][i - 1]][i - 1];
        }
        for(int i = first[s]; i != -1; i = e[i].next){
            int u = e[i].v;
            if(u == fa) continue;
            from[u] = i;
            dfs(u, s);
        }
    }
    
    int lca(int x, int y){
        if(depth[x] < depth[y]) swap(x, y);
        for(int i = t; i >= 0; i --){
            if(depth[p[x][i]] >= depth[y]) x = p[x][i];
        }
        if(x == y) return y;
        for(int i = t; i >= 0; i --){
            if(p[x][i] == p[y][i]) continue;
            x = p[x][i], y = p[y][i];
        }
        return p[y][0];
    }
    
    void build(){
        cnt = 2;
        k = tot = t = ans = cur = 0;
        full(head, -1), full(first, -1);
        full(dfn, 0), full(low, 0);
        full(c, 0), full(depth, 0);
        full(p, 0), full(from, 0);
        full(bri, false), full(kills, false);
    }
    
    int main(){
    
        int _ = 0;
        n = read(), m = read();
        while(n && m){
            build();
            for(int i = 0; i < m; i++){
                int u = read(), v = read();
                addEdge(u, v), addEdge(v, u);
            }
            for(int i = 1; i <= n; i++){
                if(!dfn[i]) tarjan(i, 0);
            }
            for(int i = 1; i <= n; i++){
                if(!c[i]) ++tot, dfs(i);
            }
            for(int i = 2; i < cnt; i += 2){
                int u = edge[i^1].v, v = edge[i].v;
                if(c[u] != c[v]) link(c[u], c[v]), link(c[v], c[u]);
                if(bri[i]) ans++;
            }
            t = (int) (log(n) / log(2)) + 1;
            dfs(1, 0);
            int q = read();
            printf("Case %d:
    ", ++_);
            while(q--){
                int u = read(), v = read();
                if(c[u] == c[v]) printf("%d
    ", ans);
                else{
                    int num = 0;
                    int f = lca(c[u], c[v]);
                    int tmp = c[u];
                    while(tmp != f){
                        if(!kills[from[tmp]]){
                            kills[from[tmp]] = kills[from[tmp]^1] = true;
                            num++;
                        }
                        tmp = p[tmp][0];
                    }
                    tmp = c[v];
                    while(tmp != f){
                        if(!kills[from[tmp]]){
                            kills[from[tmp]] = kills[from[tmp]^1] = true;
                            num++;
                        }
                        tmp = p[tmp][0];
                    }
                    ans -= num;
                    printf("%d
    ", ans);
                }
            }
            n = read(), m = read();
        }
        return 0;
    }
    
  • 相关阅读:
    empty()与remove([expr])的区别.转
    ThinkPHP验证码刷新随机数
    ThinkPHP的cookide保存二维数组的方法
    ThinkPHP 关联模型中查询某条记录的父级(非查询子级)
    DWZ的选择带回功能无法带回第一个value中的值
    IS_POST:判断是否存在POST提交
    ThinkPHP中Xheditor编辑器报错
    一台电脑安装两个xampp的方法
    ajax中文传送到模板显示为null
    【MySQL】MySQL事务回滚脚本
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10833858.html
Copyright © 2011-2022 走看看