POJ 3694 Network

来源:互联网 发布:python安装cv2库 编辑:程序博客网 时间:2024/05/16 05:14

D - Network
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3694
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.

解题思路: 强联通缩点+ LCA
我们可以利用缩点求出的信息来进行对LCA的求解,因为tarjain本身就是个DFS过程
这里写图片描述
第一种情况,为对现有图边加一条边的情况。
可以看出,B和F的最近公共祖先为A,如何找到A,可以以F为始,逆向向上,找到第一个前序编号不比B小的情况,此为最近公共节点。由图可以看出,必须以前序编号大的节点为始,进行逆向,因为前序编号小的节点逆向向上的节点,前序编号更小,没法判断最近公共祖先。
第二种情况类似,区别在于找到等于pre[B]为止

#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <map>using namespace std;#define MAXN 111111#define MAXE 422222struct edge{    int u, v, next, used;} e[MAXE];int head[MAXE], cnt;int vis[MAXN], dfn[MAXN], fa[MAXN], low[MAXN];bool bri[MAXN];int dep, blo, nbri;void init( ){    memset(fa, -1, sizeof(fa));    cnt = blo = dep = nbri = 0;    memset(vis, 0, sizeof(vis));    memset(head, -1, sizeof(head));    memset(dfn, 0, sizeof(dfn));    memset(bri, 0, sizeof(bri));}void Addedge(int uu, int vv){    e[cnt].u = uu, e[cnt].v = vv, e[cnt].used = 0;    e[cnt].next = head[uu], head[uu] = cnt++;}void dfs(int u){    vis[u] = 1;    dfn[u] = low[u] = ++dep;    for(int k=head[u]; k!=-1; k=e[k].next)        if(!e[k].used)        {            e[k].used = e[k^1].used = 1;            int v = e[k].v;            if(!vis[v])            {                fa[v] = u;                dfs(v);                low[u] = min(low[u] , low[v]);                if(dfn[u] < low[v])                {                    nbri++;                    bri[v] = true;                }            }            else if(vis[v] == 1) low[u] = min(low[u],dfn[v]);        }    vis[u] = 2;}void LCA(int u,int v){    if(dfn[u] < dfn[v]) swap(u,v);    while(dfn[u] > dfn[v])    {        if(bri[u]) nbri--;        bri[u] = false;        u = fa[u];    }    while(u!=v)    {        if(bri[u]) nbri--;        if(bri[v]) nbri--;        bri[u] = bri[v] = false;        u = fa[u];        v = fa[v];    }}int main(){    int n, m, a, b, q;    int ca = 1;    while(scanf("%d %d",&n, &m) != EOF &&( n + m))    {        init();        printf("Case %d:\n",ca++);        for( int i = 0; i < m; i++)        {            scanf("%d %d",&a, &b);            Addedge(a, b);            Addedge(b,a);        }        fa[1] = 1;        dfs(1);        scanf("%d",&q);        while(q--)        {            int u,v;            scanf("%d%d",&u,&v);            LCA(u,v);            printf("%d\n",nbri);        }        printf("\n");    }    return 0;}
0 0
原创粉丝点击