zoukankan      html  css  js  c++  java
  • HDU 3844 Mining Your Own Business

    Mining Your Own Business

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3844
    64-bit integer IO format: %I64d      Java class name: Main
     
    John Digger is the owner of a large illudium phosdex mine. The mine is made up of a series of tunnels that meet at various large junctions. Unlike some owners, Digger actually cares about the welfare of his workers and has a concern about the layout of the mine. Specifically, he worries that there may a junction which, in case of collapse, will cut off workers in one section of the mine from other workers (illudium phosdex, as you know, is highly unstable). To counter this, he wants to install special escape shafts from the junctions to the surface. He could install one escape shaft at each junction, but Digger doesn’t care about his workers that much. Instead, he wants to install the minimum number of escape shafts so that if any of the junctions collapses, all the workers who survive the junction collapse will have a path to the surface.

    Write a program to calculate the minimum number of escape shafts and the total number of ways in which this minimum number of escape shafts can be installed.
     

    Input

    The input consists of several test cases. The first line of each case contains a positive integer N (N <= 5×10^4) indicating the number of mine tunnels. Following this are N lines each containing two distinct integers s and t, where s and t are junction numbers. Junctions are numbered consecutively starting at 1. Each pair of junctions is joined by at most a single tunnel. Each set of mine tunnels forms one connected unit (that is, you can get from any one junction to any other).

    The last test case is followed by a line containing a single zero.
     

    Output

    For each test case, display its case number followed by the minimum number of escape shafts needed for the system of mine tunnels and the total number of ways these escape shafts can be installed. You may assume that the result fits in a signed 64-bit integer.

    Follow the format of the sample output.
     

    Sample Input

    9
    1 3
    4 1
    3 5
    1 2
    2 6
    1 5
    6 3
    1 6
    3 2
    6
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    0

    Sample Output

    Case 1: 2 4
    Case 2: 4 1

    Source

     
    解题:点双连通分量,当然我不是直接求点双连通分量的,我是求割点。先找出割点,然后从非割点开始dfs,在dfs的过程中,不能经过割点。
     
    当一个点双连通分量中有且仅有一个割点的时候才选择一个位置进行安装,安装的可选的位置不包括割点。
     
    当整个图不存在割点的时候,需要两个位置,因为如果一个,恰好放梯子的位置挂了,全挂了。所以随便选两个位置就好了。
     
    此题目需要扩栈。。。。WF的题目满是坑
     
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 #pragma comment(linker, "/STACK:102400000,102400000")
     6 using namespace std;
     7 typedef long long LL;
     8 const int maxn = 50010;
     9 struct arc {
    10     int to,next;
    11     arc(int x = 0,int y = -1) {
    12         to = x;
    13         next = y;
    14     }
    15 } e[1000010];
    16 int head[maxn],dfn[maxn],low[maxn],idx,tot,cn;
    17 bool cut[maxn];
    18 void add(int u,int v) {
    19     e[tot] = arc(v,head[u]);
    20     head[u] = tot++;
    21     e[tot] = arc(u,head[v]);
    22     head[v] = tot++;
    23 }
    24 void tarjan(int u,int fa) {
    25     dfn[u] = low[u] = ++idx;
    26     int son = 0;
    27     for(int i = head[u]; ~i; i = e[i].next) {
    28         if(e[i].to == fa) continue;
    29         if(!dfn[e[i].to]) {
    30             tarjan(e[i].to,u);
    31             son++;
    32             low[u] = min(low[u],low[e[i].to]);
    33             if(fa != -1 && low[e[i].to] >= dfn[u] || fa == -1 && son > 1) {
    34                 cut[u] = true;
    35                 cn++;
    36             }
    37         } else low[u] = min(low[u],dfn[e[i].to]);
    38     }
    39 }
    40 bool vis[maxn];
    41 int cnt,n,m,cao;
    42 bool fk[maxn];
    43 void dfs(int u,int fa) {
    44     vis[u] = true;
    45     cnt++;
    46     for(int i = head[u]; ~i; i = e[i].next) {
    47         if(e[i].to == fa) continue;
    48         if(cut[e[i].to]) {
    49             if(!fk[e[i].to]) {
    50                 cao++;
    51                 fk[e[i].to] = true;
    52             }
    53             continue;
    54         }
    55         if(vis[e[i].to]) continue;
    56         dfs(e[i].to,fa);
    57     }
    58 }
    59 int main() {
    60     int u,v,cs = 1;
    61     while(scanf("%d",&m),m) {
    62         memset(head,-1,sizeof head);
    63         for(int i = tot = n = idx = cn = 0; i < m; ++i) {
    64             scanf("%d%d",&u,&v);
    65             add(u,v);
    66             n = max(n,max(u,v));
    67         }
    68         memset(dfn,0,sizeof dfn);
    69         memset(cut,false,sizeof cut);
    70         for(int i = 1; i <= n; ++i)
    71             if(!dfn[i]) tarjan(i,-1);
    72         LL ret = 1;
    73         if(!cn) printf("Case %d: %d %I64d
    ",cs++,2,(LL)n*(n-1)/2);
    74         else {
    75             memset(vis,false,sizeof vis);
    76             int ri = 0;
    77             for(int i = 1; i <= n; ++i) {
    78                 if(cut[i] || vis[i]) continue;
    79                 cnt = cao = 0;
    80                 memset(fk,false,sizeof fk);
    81                 dfs(i,-1);
    82                 if(cao < 2 && cnt) {ri++; ret *= cnt;}
    83             }
    84             printf("Case %d: %d %I64d
    ",cs++,ri,ret);
    85         }
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    MySQL 中 truncate 和 delete 的坑
    理解 Linux 中的 inodes
    Linux 下date命令的常见用法
    Linux 下du命令详解
    nc
    启动Tomcat时报错:错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1101; nested exception is: java.net.BindException: Address already in use: JVM_Bind
    ssm调用webservice接口并进行文件传输
    mybatis 中 执行INSERT操作后获取自增主键
    mysql时间差8小时
    生成验证码工具类
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4692769.html
Copyright © 2011-2022 走看看