• POJ 1144 Network


    Network

    Time Limit: 1000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1144
    64-bit integer IO format: %lld      Java class name: Main
     
     
    A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is 
    possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure 
    occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
     

    Input

    The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated 
    by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
     

    Output

    The output contains for each block except the last in the input file one line containing the number of critical places.
     

    Sample Input

    5
    5 1 2 3 4
    0
    6
    2 1 3
    5 4 6 2
    0
    0

    Sample Output

    1
    2

    Hint

    You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.
     

    Source

     
    解题:求割点个数。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 110;
    18 int n,cnt,dfn[maxn],low[maxn];
    19 vector<int>g[maxn];
    20 bool iscut[maxn];
    21 void tarjan(int u,int fa) {
    22     dfn[u] = low[u] = ++cnt;
    23     int v,son = 0;
    24     for(v = 0; v < g[u].size(); v++) {
    25         if(!dfn[g[u][v]]) {
    26             son++;
    27             tarjan(g[u][v],u);
    28             if(low[u] > low[g[u][v]]) low[u] = low[g[u][v]];
    29             if(low[g[u][v]] >= dfn[u]) iscut[u] = true;
    30         } else if(g[u][v] != fa && low[u] > dfn[g[u][v]])
    31             low[u] = dfn[g[u][v]];
    32     }
    33     if(fa < 0 && son == 1) iscut[u] = false;
    34 }
    35 int main() {
    36     int u,v,ans;
    37     while(scanf("%d",&n),n) {
    38         for(int i = 0; i <= n; i++){
    39             g[i].clear();
    40             dfn[i] = low[i] = 0;
    41             iscut[i] = false;
    42         }
    43         while(scanf("%d",&u),u) {
    44             while(scanf("%d",&v)) {
    45                 g[u].push_back(v);
    46                 g[v].push_back(u);
    47                 if(getchar() == '
    ') break;
    48             }
    49         }
    50         ans = cnt = 0;
    51         tarjan(1,-1);
    52         for(int i = 1; i <= n; i++)
    53             ans += iscut[i];
    54         printf("%d
    ",ans);
    55     }
    56     return 0;
    57 }
    View Code

    。。再写一次。。。复习下

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn = 200;
     6 struct arc{
     7     int to,next;
     8     arc(int x = 0,int y = -1){
     9         to = x;
    10         next = y;
    11     }
    12 }e[maxn*maxn];
    13 int head[maxn],dfn[maxn],low[maxn],tot,idx,ans;
    14 void add(int u,int v){
    15     e[tot] = arc(v,head[u]);
    16     head[u] = tot++;
    17 }
    18 void tarjan(int u,int fa){
    19     dfn[u] = low[u] = idx++;
    20     bool flag = true,iscut = false;
    21     int son = 0;
    22     for(int i = head[u]; ~i; i = e[i].next){
    23         if(flag && e[i].to == fa){
    24             flag = false;
    25             continue;
    26         }
    27         if(dfn[e[i].to] == -1){
    28             tarjan(e[i].to,u);
    29             son++;
    30             low[u] = min(low[u],low[e[i].to]);
    31             if(fa == -1 && son >= 2 || fa != -1 && low[e[i].to] >= dfn[u]) iscut = true;
    32         }else if(fa != e[i].to) low[u] = min(low[u],dfn[e[i].to]);
    33      }
    34      ans += iscut;
    35 }
    36 int main(){
    37     int n,tmp,u,v;
    38     while(scanf("%d",&n),n){
    39         idx = tot = ans = 0;
    40         memset(dfn,-1,sizeof(dfn));
    41         memset(low,-1,sizeof(low));
    42         memset(head,-1,sizeof(head));
    43         bool flag = false;
    44         while(scanf("%d",&u),u){
    45             while(~scanf("%d",&v)){
    46                 add(u,v);
    47                 add(v,u);
    48                 if(getchar() == '
    ') break;
    49             }
    50         }
    51         for(int i = 1; i <= n; ++i)
    52             if(dfn[i] == -1) tarjan(i,-1);
    53         printf("%d
    ",ans);
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    Hibernate学习笔记
    Oracle12c 在windonServer2012中安装的步骤
    提升tomcat服务器性能的七条经验
    zyUpload---照片上传并显示效果
    js的隐含参数(arguments,callee,caller)使用方法
    js中callback.call()和callback()的区别
    理解javascript中的回调函数(callback)
    Spring MVC 流程图
    JDBC在springMvc等框架中使用的方式
    为IE8添加EventListener系列方法支持
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3935937.html
Copyright © 2020-2023  润新知