zoukankan      html  css  js  c++  java
  • HDU 2460 Network 傻逼Tarjan

    Network

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1014    Accepted Submission(s): 206


    Problem 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
     
    Source
     

    题意

    给你一个无向图,有多次操作。每次操作加入一条边,然后询问有多少桥。

    题解

    就先Tarjan求出有多少桥,然后每次操作,就在搜索树上求个lca,在求的过程中遇到的桥都会失效,因为构成了环。详见代码

    #pragma comment(linker, "/STACK:102400000,102400000") 
    #include<iostream>
    #include<vector>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<stack>
    #define MAX_N 100005
    using namespace std;
    
    vector<int> G[MAX_N];
    
    int father[MAX_N];
    
    int dfn[MAX_N],low[MAX_N],ind=0;
    bool vis[MAX_N];
    
    int n,m;
    
    int sum = 0;
    bool isBridge[MAX_N];
    
    int p;
    
    void Tarjan(int u) {
        father[u] = p;
        dfn[u] = low[u] = ++ind;
        vis[u] = 1;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (v == p)continue;
            if (!vis[v]) {
                int tmp = p;
                p = u;
                Tarjan(v);
                p = tmp;
                low[u] = min(low[u], low[v]);
                if (low[v] > dfn[u]) {
                    sum++;
                    isBridge[v] = 1;
                }
            }
            else
                low[u] = min(dfn[v], low[u]);
        }
    }
    
    int main() {
        int cas = 0;
        cin.sync_with_stdio(false);
        while (cin >> n >> m) {
            if (n == 0 && m == 0)break;
            memset(vis, 0, sizeof(vis));
    
            for (int i = 0; i <= n; i++)G[i].clear();
            sum = 0;
            memset(isBridge, 0, sizeof(isBridge));
            ind = 0;
            memset(father, 0, sizeof(father));
            for (int i = 0; i < m; i++) {
                int u, v;
                cin >> u >> v;
                G[u].push_back(v);
                G[v].push_back(u);
            }
            p = 0;
            Tarjan(1);
            int q;
            cin >> q;
            cout << "Case " << ++cas << ":" << endl;
            while (q--) {
                int u, v;
                cin >> u >> v;
                if (dfn[u] < dfn[v])swap(u, v);
                while (dfn[u] > dfn[v]) {
                    if (isBridge[u])sum--;
                    isBridge[u] = 0;
                    u = father[u];
                }
                while (u != v) {
                    if (isBridge[v])sum--;
                    isBridge[v] = 0;
                    v = father[v];
                }
                cout << sum << endl;
            }
            cout << endl;
        }
        return 0;
    }

    注意要扩栈

  • 相关阅读:
    启动hadoop集群的时候只能启动一个namenode,另一个报错There appears to be a gap in the edit log. We expected txid 6, but got txid 10.
    YARN的HA
    CSS3 文本效果:text-shadow、box-shadow、text-overflow、word-wrap、word-break
    CSS3 渐变(Gradients):在两个或多个指定的颜色之间显示平稳的过渡
    CSS3 背景:几个新的背景属性,提供更大背景元素控制
    CSS3 圆角:使用 CSS3 border-radius 属性,你可以给任何元素制作 "圆角"
    CSS3 边框:创建圆角边框,添加阴影框
    CSS 网页布局的集中实现方式
    CSS 计数器:通过一个变量来设置,根据规则递增变量
    CSS 表单:使用 CSS 来渲染 HTML 的表单元素
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4716340.html
Copyright © 2011-2022 走看看