zoukankan      html  css  js  c++  java
  • POJ 2117 Electricity

    Electricity

     

    Description

    Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them supplying a small area that surrounds it. This organization brings a lot of problems - it often happens that there is not enough power in one area, while there is a large surplus in the rest of the country. 

    ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places - i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun. 

    One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points. 

    Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining points (not counting the removed joining point itself). 

    Input

    The input consists of several instances. 

    The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection between every two plants. 

    The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros. 

    Output

    The output consists of several lines. The i-th line of the output corresponds to the i-th input instance. Each line of the output consists of a single integer C. C is the maximum number of the connected parts of the network that can be obtained by removing one of the joining points at power plants in the instance.

    Sample Input

    3 3
    0 1
    0 2
    2 1
    4 2
    0 1
    2 3
    3 1
    1 0
    0 0
    

    Sample Output

    1
    2
    2

     1 #include<cstdio>
     2 #include<vector>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=10005;
     8 int clock,son,ans;
     9 vector<int>G[maxn];
    10 int low[maxn],pre[maxn];
    11 
    12 int tarjan(int u,int fa)
    13 {
    14     int lowu=pre[u]=++clock;
    15     int child=0;
    16     for(int i=0;i<G[u].size();i++)
    17     {
    18         int v=G[u][i];
    19         if(!pre[v])
    20         {
    21             int lowv=tarjan(v,u);
    22             lowu=min(lowu,lowv);
    23             if(u==fa)
    24                 son++;
    25             else if(lowv>=pre[u])
    26                 child+=1;
    27         }
    28         else
    29             lowu=min(lowu,pre[v]);
    30     }
    31     ans=max(ans,child+1);
    32     low[u]=lowu;
    33     return lowu;
    34 }
    35 
    36 int main()
    37 {
    38     int P,C,num,ma;
    39     while(scanf("%d%d",&P,&C),C|P)
    40     {
    41         for(int i=0;i<P;i++)
    42         {
    43             low[i]=pre[i]=0;
    44             G[i].clear();
    45         }
    46         int ma=ans=num=clock=son=0;
    47         for(int i=0;i<C;i++)
    48         {
    49             int u,v;
    50             scanf("%d%d",&u,&v);
    51             G[u].push_back(v);
    52             G[v].push_back(u);
    53         }
    54         for(int i=0;i<P;i++)
    55         {
    56             if(!pre[i])
    57             {
    58                 num++;
    59                 son=0;
    60                 tarjan(i,i);
    61                 if(son>1)
    62                     ans=max(ans,son);
    63                 ma=max(ans,ma);
    64             }
    65         }
    66         if(C==0)
    67             ma=0;
    68         printf("%d
    ",num+ma-1);
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Azure 存储简介
    Databricks 第6篇:Spark SQL 维护数据库和表
    Databricks 第5篇:Databricks文件系统(DBFS)
    Databricks 第4篇:pyspark.sql 分组统计和窗口
    IDEA分析JAVA内存溢出和内存泄漏
    Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Connection is broken: "session closed" [90067-200] 解决
    Java 中初始化 List 集合的 8 种方式!
    java list算法问题(给定一 int 数组返回倒序的最大连续递增的区间(至少大于等于2)数组倒序)
    uni-app知识点:页面滚动到指定位置(即锚点实现)、设置背景颜色backgroundColor无效的问题、导航栏设置角标及动态控制修改角标数字
    @Transactional注解为什么不生效
  • 原文地址:https://www.cnblogs.com/homura/p/4823794.html
Copyright © 2011-2022 走看看