zoukankan      html  css  js  c++  java
  • HDU4738 Caocao's Bridges —— 边双联通分量 + 重边

    题目链接:https://vjudge.net/problem/HDU-4738

    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

    题解:

    1.由于有n个点,而边最多有n^2条, 所以可能会有重边,即两点之间的边数可能大于1。

    2.用Tarjan算法求出边双联通分量,如果:

    1)原图不连通,则不需要派人去毁桥,即输出0。

    2)如果原图为边双联通图,即分量的个数为1,则无法实现作战任务,即输出-1。

    3)找出边权最小的桥(割边),如果权值为0,则输出1(至少也得派一个人过去吧);否则输出权值。

    代码如下:

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 typedef long long LL;
      4 const int INF = 2e9;
      5 const LL LNF = 9e18;
      6 const int MOD = 1e9+7;
      7 const int MAXN = 1e3+10;
      8 
      9 struct Edge
     10 {
     11     int to, next, w;
     12     bool iscut;
     13 }edge[MAXN*MAXN*2];
     14 int tot, head[MAXN];
     15 
     16 int index, low[MAXN], dfn[MAXN];
     17 int top, Stack[MAXN], instack[MAXN];
     18 int block, belong[MAXN];
     19 
     20 void addedge(int u, int v, int w)
     21 {
     22     edge[tot].iscut = false;
     23     edge[tot].to = v;
     24     edge[tot].w = w;
     25     edge[tot].next = head[u];
     26     head[u] = tot++;
     27 }
     28 
     29 void Tarjan(int u, int pre)
     30 {
     31     low[u] = dfn[u] = ++index;
     32     Stack[top++] = u;
     33     instack[u] = true;
     34     for(int i = head[u]; i!=-1; i = edge[i].next)
     35     {
     36         if((i^1)==pre) continue;
     37         int v = edge[i].to;
     38         if(!dfn[v])
     39         {
     40             Tarjan(v, i);
     41             low[u] = min(low[u], low[v]);
     42             if(low[v]>low[u])
     43                 edge[i].iscut = edge[i^1].iscut = true;
     44         }
     45         else if(instack[v])
     46             low[u] = min(low[u], dfn[v]);
     47     }
     48 
     49     if(low[u]==dfn[u])
     50     {
     51         block++;
     52         int v;
     53         do
     54         {
     55             v = Stack[--top];
     56             instack[v] = false;
     57             belong[v] = block;
     58         }while(v!=u);
     59     }
     60 }
     61 
     62 void init()
     63 {
     64     tot = 0;
     65     memset(head, -1, sizeof(head));
     66 
     67     index = 0;
     68     memset(dfn, 0, sizeof(dfn));
     69     memset(low, 0, sizeof(low));
     70 
     71     block = top = 0;
     72     memset(instack, false, sizeof(instack));
     73 }
     74 
     75 int main()
     76 {
     77     int n, m;
     78     while(scanf("%d %d",&n,&m) && (n||m))
     79     {
     80         init();
     81         for(int i = 1; i<=m; i++)
     82         {
     83             int u, v, w;
     84             scanf("%d%d%d",&u,&v,&w);
     85             addedge(u, v, w);
     86             addedge(v, u, w);
     87         }
     88 
     89         int times = 0;
     90         for(int i = 1; i<=n; i++)
     91             if(!dfn[i])
     92                 Tarjan(i, -1), times++;
     93 
     94         if(times>1) //原图不连通,则不需要派人去毁桥。
     95         {
     96             printf("%d
    ", 0);
     97             continue;
     98         }
     99 
    100         if(block==1)    //原图为边双联通图,则不能实现
    101         {
    102             printf("%d
    ", -1);
    103             continue;
    104         }
    105 
    106         int ans = INF;
    107         for(int u = 1; u<=n; u++)
    108         for(int i = head[u]; i!=-1; i = edge[i].next)
    109             if(edge[i].iscut)
    110                 ans = min(ans, edge[i].w);
    111 
    112         printf("%d
    ", ans==0?1:ans);   //需要特判是否为0
    113     }
    114 }
    View Code
  • 相关阅读:
    RPC服务和HTTP服务对比
    常用工具地址
    maven教程
    【springboot】知识点总结
    [JZOJ4272] [NOIP2015模拟10.28B组] 序章-弗兰德的秘密 解题报告(树形DP)
    [NOIP2015模拟10.22] 最大子矩阵 解题报告(单调栈)
    [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)
    [NOIP2015模拟10.27] [JZOJ4270] 魔道研究 解题报告(动态开点+权值线段树上二分)
    [NOIP2015模拟10.22] 最小代价 解题报告 (最小生成树)
    BZOJ4479 [JSOI2013] 吃货jyy 解题报告(三进制状态压缩+欧拉回路)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7701627.html
Copyright © 2011-2022 走看看