zoukankan      html  css  js  c++  java
  • Network-POJ3694(最小公共祖先LCA+Tarjin)

    http://poj.org/problem?id=3694

    这一题  为什么要找最小祖先呢

    当两个节点连到一块的时候  找最小公共节点就相当于找强连通分支

    再找最小公共节点的过程中直到找到  这个过程中所有的点就是一个强连通分支

    现在要求桥   只需用没有加边的时候的桥数减去后来找到的强连通分支里的桥数就得到加边后的桥数

    Network
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 7720   Accepted: 2823

    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

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<vector>
    
    using namespace std;
    #define N 200000
    
    int low[N],dfn[N],n,fa[N],Stack[N],bridge[N];
    int Time,top,ans;
    vector<vector <int> >G;
    
    void Inn()
    {
        G.clear();
        G.resize(n+1);
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        memset(fa,0,sizeof(fa));
        memset(bridge,0,sizeof(bridge));
        memset(Stack,0,sizeof(Stack));
        Time=top=ans=0;
    }
    
    void Tarjin(int u,int f)
    {
        dfn[u]=low[u]=++Time;
        fa[u]=f;
        int len=G[u].size(),v;
        for(int i=0; i<len; i++)
        {
            v=G[u][i];
            if(!dfn[v])
            {
                Tarjin(v,u);
                low[u]=min(low[u],low[v]);
                if(low[v]>dfn[u])
                {
                    bridge[v]++;
                    ans++;
                }
            }
            else if(v!=f)
                low[u]=min(low[u],dfn[v]);
        }
    }
    void LCA(int a,int b)
    {
        if(a==b)
            return;
        if(dfn[a]>dfn[b])
        {
            int v=fa[a];
            if(bridge[a]>0)
            {
                bridge[a]=0;
            ans--;
            }
            LCA(v,b);
        }
        else
        {
            int v=fa[b];
            if(bridge[b]>0)
            {
                bridge[b]=0;
            ans--;
            }
            LCA(a,v);
        }
    }
    
    int main()
    {
        int m,a,b,q,i,t=1;
        while(scanf("%d %d",&n,&m),n+m)
        {
            Inn();
            for(i=1; i<=m; i++)
            {
                scanf("%d %d",&a,&b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
            Tarjin(1,0);
            scanf("%d",&q);
            printf("Case %d:
    ",t++);
            while(q--)
            {
                scanf("%d %d",&a,&b);
                LCA(a,b);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    leetcode 850. Rectangle Area II
    leetcode 699. Falling Squares 线段树的实现
    leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
    leetcode 843. Guess the Word
    javaMail实现收发邮件(三)
    javaMail实现收发邮件(二)
    javaMail实现收发邮件(一)
    springboot整合websocket实现一对一消息推送和广播消息推送
    jieba分词/jieba-analysis(java版)
    java实现两个不同list对象合并后并排序
  • 原文地址:https://www.cnblogs.com/linliu/p/4917702.html
Copyright © 2011-2022 走看看