• poj3694 缩点边双连通分量


    Network
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 8669   Accepted: 3175

    Description

    A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.

    You are to help the administrator by reporting the number of bridges in the network after each new link is added.

    Input

    The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
    Each of the following M lines contains two integers A and B ( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
    The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
    The i-th line of the following Q lines contains two integer A and B (1 ≤ A ≠ B ≤ N), which is the i-th added new link connecting computer A and B.

    The last test case is followed by a line containing two zeros.

    Output

    For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

    Sample Input

    3 2
    1 2
    2 3
    2
    1 2
    1 3
    4 4
    1 2
    2 1
    2 3
    1 4
    2
    1 2
    3 4
    0 0
    

    Sample Output

    Case 1:
    1
    0

    Case 2:
    2
    0
     
    题意:
    有一张无向图,q次操作。每次操作,添加变(x,y),问添加这条边后,有多少个桥。
     
    思路:
    对于给予的图,先进行缩点,新的图一定是一棵树。然后每次操作都是在树上进行操作。
    对于每次操作,可以从x点对应的树上的点出发,进行dfs,在x,y这条链上的点,用并查集合并,
    这样只要判断并查集中根的个数即可。
    /*
     * Author:  sweat123
     * Created Time:  2016/6/22 15:00:44
     * File Name: main.cpp
     */
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<time.h>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1<<30
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 100010;
    struct node{
        int to;
        int next;   
    }edge[MAXN<<2]; 
    int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind,pa[MAXN];
    int px[MAXN],py[MAXN],cnt,f[MAXN],num;
    void add(int x,int y){
        edge[ind].to = y;
        edge[ind].next = pre[x];
        pre[x] = ind ++;   
    }
    void init(){
        cnt = 0;
        num = 0;
        memset(f,-1,sizeof(f));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));   
    }
    int find(int x){
        if(x != pa[x])pa[x] = find(pa[x]);
        return pa[x];   
    }
    void dfs(int rt,int k,int fa){
        dfn[rt] = low[rt] = k;
        for(int i = pre[rt]; i != - 1; i = edge[i].next){
            int t = edge[i].to;
            if(!dfn[t]){
                dfs(t,k+1,rt);
                low[rt] = min(low[rt],low[t]);
                if(low[t] > dfn[rt]){
                    px[cnt] = rt,py[cnt++] = t;   
                } else{
                    int fx = find(rt);
                    int fy = find(t);
                    pa[fx] = fy;   
                }
            } else if(t != fa){
                   low[rt] = min(low[rt],dfn[t]);
            }
        }
    }
    int dfs2(int rt,int k){
        vis[rt] = 1;
        if(rt == k){
            return 1;
        }   
        for(int i = pre[rt]; i != -1; i = edge[i].next){
            int t = edge[i].to;
            if(!vis[t]){
                int p = dfs2(t,k);
                if(p == 1){
                    int fx = find(rt);
                    int fy = find(t);
                    pa[fx] = fy;
                    return 1;      
                }
            }   
        }
        return 0;
    }
    int main(){
        int ff = 0;
        while(~scanf("%d%d",&n,&m)){
            if(n == 0 && m == 0)break;
            ind = 0;
            memset(pre,-1,sizeof(pre));
            for(int i = 1; i <= m; i++){
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,y),add(y,x);
            }
            for(int i = 1; i <= n; i++){
                pa[i] = i;   
            }
            init();
            dfs(1,1,-1);
            for(int i = 1; i <= n; i++){
                int fx = find(i);
                if(f[fx] == -1)f[fx] = ++num;
                f[i] = f[fx];
            }
            ind = 0;
            memset(pre,-1,sizeof(pre));
            for(int i = 0; i < cnt; i++){
                int x = f[px[i]];
                int y = f[py[i]];
                add(x,y),add(y,x);
            }
            int q;
            printf("Case %d:
    ",++ff);
            scanf("%d",&q);
            for(int i = 1; i <= n; i++){
                pa[i] = i;   
            }
            while(q--){
                int x,y;
                scanf("%d%d",&x,&y);
                x = f[x];
                y = f[y];
                memset(vis,0,sizeof(vis));
                dfs2(x,y);
                int ans = 0;
                for(int i = 1; i <= num; i++){
                    int fx = find(i);
                    if(fx == i)ans += 1;
                }   
                printf("%d
    ",ans - 1);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    python
    python
    打开python自带IDLE出的问题
    Python GUI编程(Tkinter)8、CheckButton多选框控件
    Python GUI编程(Tkinter)7、带滚动条的Text
    Python GUI编程(Tkinter)6、Text控件
    Python GUI编程(Tkinter)5、点击按钮输出输入框中的内容
    Python GUI编程(Tkinter)4、Entry控件
    Python GUI编程(Tkinter)3、Button控件
    Python GUI编程(Tkinter)2、Label控件
  • 原文地址:https://www.cnblogs.com/sweat123/p/5607724.html
Copyright © 2020-2023  润新知