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

    题意:给出一个图,每次加入一条边问加边后还剩下多少桥。
    sl:之前做了一个类似的学到使一个图变成 边——双联通 至少应该加多少边,我们是通过求出缩点后的树的叶子节点,每次求出两两叶子节点的公共祖先最靠
    上的将这两个点连接可以构造双联通。总的边数为(叶子节点数+1)/2; 
    此题是要实现一个查询,我们每加一条边就有可能构成一个圈,假设这两个点不在缩点后的同一颗子树上,那么和显然就是求下公共祖先就可以。
    如果在同一子树上,还是求下公共祖先就行。综上最终结果就是求下LCA。可能是数据太水,直接求LAC也能过。不过我差点挂了。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<vector>
      5 using namespace std;
      6 const int MAX = 200000+10;
      7 int pre[MAX],dfs_clock;
      8 int low[MAX],fa[MAX],iscut[MAX],cut_cnt;
      9 vector<int> G[MAX];
     10 void add_edge(int from,int to)
     11 {
     12     G[from].push_back(to);
     13     G[to].push_back(from);
     14 }
     15 void init()
     16 {
     17     memset(pre,0,sizeof(pre));
     18     memset(low,0,sizeof(low));
     19     memset(fa,0,sizeof(fa));
     20     memset(iscut,0,sizeof(iscut));
     21     dfs_clock=cut_cnt=0;
     22     for(int i=0;i<MAX;i++) G[i].clear();
     23 }
     24 int dfs(int u,int f)
     25 {
     26     int lowu=pre[u]=++dfs_clock;
     27     for(int i=0;i<G[u].size();i++)
     28     {
     29         int v=G[u][i];
     30         if(!pre[v])
     31         {
     32             fa[v]=u;
     33             int lowv=dfs(v,u);
     34             lowu=min(lowu,lowv);
     35             if(lowv>pre[u])
     36             iscut[v]=1,cut_cnt++;
     37         }
     38         else if(pre[v]<pre[u]&&v!=f)
     39         {
     40             lowu=min(lowu,pre[v]);
     41         }
     42     }
     43 
     44     low[u]=lowu;
     45     return lowu;
     46 }
     47 
     48 int LCA(int a,int b)
     49 {
     50     if(pre[a]<pre[b]) swap(a,b);
     51     while(pre[a]>pre[b])
     52     {
     53         if(iscut[a])
     54         {
     55             cut_cnt--;
     56             iscut[a]=0;
     57         }
     58         a=fa[a];
     59     }
     60     while(a!=b)
     61     {
     62         if(iscut[a])
     63         {
     64             iscut[a]=0;
     65             cut_cnt--;
     66         }
     67         if(iscut[b])
     68         {
     69             iscut[b]=0;
     70             cut_cnt--;
     71         }
     72         a=fa[a]; b=fa[b];
     73     }
     74 
     75     return cut_cnt;
     76 }
     77 
     78 int main()
     79 {
     80     int n,m,a,b,cnt=0,q;
     81     while(scanf("%d %d",&n,&m)==2&&(n||m))
     82     {
     83         init();
     84         for(int i=0;i<m;i++)
     85         {
     86             scanf("%d %d",&a,&b);
     87             add_edge(a,b);
     88         }
     89         scanf("%d",&q);
     90         dfs(1,-1);
     91         printf("Case %d: ",++cnt);
     92         for(int i=0;i<q;i++)
     93         {
     94             scanf("%d %d",&a,&b);
     95             int ans=LCA(a,b);
     96             printf("%d ",ans);
     97         }
     98 
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    Graceful degradation versus progressive enhancement
    表现与数据分离
    避免写出IE Bug
    js控制元素的显示与隐藏
    EntityManager方法简介
    JPA EntityManager详解(一)
    Springmvc中 同步/异步请求参数的传递以及数据的返回
    JPA详解
    单向关系中的JoinColumn
    Hibernate一对多和多对一关系详解 (转载)
  • 原文地址:https://www.cnblogs.com/acvc/p/3640229.html
Copyright © 2011-2022 走看看