zoukankan      html  css  js  c++  java
  • POJ 3694 Network(无向图求桥的个数,有重边)

    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

    题意:有n台电脑,它们之间是互相连通的,现在有Q次查询,每次查询时,又给该网络增加了一次连接,问有了这次连接之后,该网络有几座桥(必不可少的边)

    由于直接统计边很不方便,所以可以统计每条边的终点,以此代替边

    Tarjan函数:求出了以v为终点的边的个数,以及最初的网络中桥的个数
    LCR函数:在找到与添加的边的终点相同的桥时,桥的个数需要减1,依次递归,直至这条边的两个端点相同,求出最终桥的个数

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #define N 100010
    using namespace std;
    int dfn[N], low[N];
    int f[N], bri[N]; //bri数组统计终点一样的边的个数,由于边不便统计,所以,以该数组下标为边的终点来代替桥
    int Time, ans, n; //ans统计桥的个数
    vector<vector<int> >G;
    void Init()
    {
        G.clear();
        G.resize(n+1);
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(f, 0, sizeof(f));
        memset(bri, 0, sizeof(bri));
        Time = ans = 0;
    }
    void Tarjan(int u, int fa)
    {
        int i, len, v;
        dfn[u] = low[u] = ++Time;
        f[u] = fa;
        len = G[u].size();
        for (i = 0; i < len; i++)
        {
            v = G[u][i];
            if (!dfn[v])
            {
                Tarjan(v, u);
                low[u] = min(low[u], low[v]);
                if (dfn[u] < low[v])
                {
                    bri[v]++; //说明边终点为v的个数
                    ans++; //该点之前没有遍历过,说明该边可能是桥
                }
            }
            else if (v != fa)
            {
                low[u] = min(low[u], dfn[v]);
                if (dfn[u] < low[v])
                {
                    bri[v]++;
                    ans--; //该点之前被遍历过,说明还有其他路径可以达到此点,则该边不是桥
                }
            }
        }
    }
    void LCR(int a, int b)
    {
        if (a == b) return ; //两个点相同时不需要再进行
        if (dfn[a] < dfn[b])
        {
            if (bri[b] == 1) //如果终点为b的边只有一条,那么它肯定是桥,现在又加了一条终点为b的边,那么以b为终点的边就不是桥了,ans--
            {
                bri[b] = 0;
                ans--;
            }
            LCR(a, f[b]); //dfn[a] < dfn[b]说明a点比b点更早遍历,那么下次就应该比较a和b的父节点,因为父节点总是比子节点更早的遍历
        }
        else
        {
            if (bri[a] == 1)
            {
                bri[a] = 0;
                ans--;
            }
            LCR(f[a], b);
        }
    }
    int main ()
    {
        int m, q, a, b, k = 0;
        while (scanf("%d%d", &n, &m), n+m)
        {
            Init();
            k++;
            while (m--)
            {
                scanf("%d%d", &a, &b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
            Tarjan(1, 0);
            scanf("%d", &q);
            printf("Case %d:
    ", k);
            while (q--)
            {
                scanf("%d%d", &a, &b);
                LCR(a, b);
                printf("%d
    ", ans);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    平板涂色
    速算游戏_NOI导刊2011提高(04)
    信息学奥赛一本通——配套刷题网站
    求10000以内n的阶乘
    大整数的因子
    计算2的N次方
    大整数加法
    带余除法
    A/B 高精度
    A*B 高静度
  • 原文地址:https://www.cnblogs.com/syhandll/p/4709999.html
Copyright © 2011-2022 走看看