zoukankan      html  css  js  c++  java
  • POJ 3694 Network

    Network
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 5535   Accepted: 1906

    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≤ AB ≤ 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 ≤ ABN), 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

    2008 Asia Hefei Regional Contest Online by USTC
        考察点: 割边 和lca算法,有重边的情况在lca中都去掉了,所以在Tarjan算法中不用加判重边处理
    /***************************************************************
    	> File Name:    netework.cpp
    	> Author:       SDUT_GYX
    	> Mail:         2272902662@qq.com
    	> Created Time: 2013/5/26 20:03:46
     **************************************************************/
    
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <queue>
    #include <cstdlib>
    #include <iomanip>
    #include <string>
    #include <vector>
    #include <map>
    #include <cmath>
    #include <stack>
    #define LL long long
    #define N 100000
    using namespace std;
    struct num
    {
    	int end,next;
    }a[10*N];
    int point[N+10],low[N+10],dfn[N+10],cou,res;
    int fat[N+10],pt[N+10];
    int main()
    {
    	//freopen("data1.in","r",stdin);
    	void Tarjan(int u,int fa);
    	void Lca(int x,int y);
    	int n,m,T;
    	T=1;
    	while(scanf("%d %d",&n,&m)!=EOF)
    	{
    		if(n==0&&m==0)
    		{
    			break;
    		}
    		memset(point,-1,sizeof(point));
    		for(int i=0,j=0;i<=m-1;i++)
    		{
    			int x,y;
    			scanf("%d %d",&x,&y);
    			a[j].end=y; a[j].next=point[x]; point[x]=j; j++;
    			a[j].end=x; a[j].next=point[y]; point[y]=j; j++;
    		}
    		res=0;
    		for(int i=1; i<=n; i++)
    		{
    			pt[i] = i;
    		}
    		res = cou = 0;
    		memset(dfn,0,sizeof(dfn));
    		Tarjan(1,-1);
    		scanf("%d",&m);
    		printf("Case %d:\n",T++);
    		while(m--)
    		{
    			int x,y;
    			scanf("%d %d",&x,&y);
    			Lca(x,y);
    			printf("%d\n",res);
    		}
    		printf("\n");
    	}
    	return 0;
    }
    int find(int x)
    {
    	int k1,k2;
    	k1 = x;
    	while(x != pt[x])
    	{
    		x= pt[x];
    	}
    	while(k1 != pt[k1])
    	{
    		k2 = pt[k1];
    		pt[k1] = x;
    		k1 = k2;
    	}
    	return x;
    }
    int link(int x,int y)
    {
    	x = find(x);
    	y = find(y);
    	if(x != y)
    	{
    		pt[x] = y;
    		return 1;
    	}
    	return 0;
    }
    void Tarjan(int u,int fa)
    {
    	low[u] = dfn[u] = ++cou;
    	for(int i=point[u]; i!=-1; i=a[i].next)
    	{
    		int v = a[i].end;
    		if(!dfn[v])
    		{
    			Tarjan(v,u);
    			fat[v] = u;
    			low[u] = min(low[v],low[u]);
    			if(low[v]>dfn[u])
    			{
    				res++;
    			}else 
    			{
    				link(u,v);
    			}
    		}else if(dfn[v]<dfn[u]&&v!=fa)
    		{
    			low[u] = min(low[u],dfn[v]);
    		}
    	}
    }
    void Lca(int x,int y)
    {
    	while(x!=y)
    	{
    		while(dfn[x]<dfn[y]&&x!=y)
    		{
    			if(link(y,fat[y]))
    			{
    				res--;
    			}else
    			{
    				y=fat[y];
    			}
    		}
    		while(dfn[x]>dfn[y]&&x!=y)
    		{
    			if(link(x,fat[x]))
    			{
    				res--;
    			}else
    			{
    				x = fat[x];
    			}
    		}
    	}
    }
    


  • 相关阅读:
    【模板】快速幂&取余运算
    【模板】ST表
    LOJ #10070 最小生成树计数
    【模板】KMP字符串匹配
    PL/SQL编程要点和注意点
    数据不同方式的提取和提交的性能测试(转载)
    Oracle分析函数之开窗子句-即WINDOWING子句
    Oracle树反向查询的优化(转载)
    oracle默认的hr用户使用脚本安装
    展开BOM并使用最终用量的算法(转载)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3100596.html
Copyright © 2011-2022 走看看