zoukankan      html  css  js  c++  java
  • [COGS311] Redundant Paths

    ★★☆   输入文件:rpaths.in   输出文件:rpaths.out   简单对比
    时间限制:1 s   内存限制:128 MB

    Description

    In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

    Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

    There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

    Input

    Line 1: Two space-separated integers: F and R

    Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

    Output

    Line 1: A single integer that is the number of new paths that must be built.

    Sample Input

    7 7

    1 2

    2 3

    3 4

    2 5

    4 5

    5 6

    5 7

    Sample Output

    2

    Hint

    Explanation of the sample:

    One visualization of the paths is:
       1   2   3
       +---+---+  
           |   |
           |   |
     6 +---+---+ 4
          / 5
         / 
        / 
     7 +
    Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
       1   2   3
       +---+---+  
       :   |   |
       :   |   |
     6 +---+---+ 4
          / 5  :
         /     :
        /      :
     7 + - - - - 
    Check some of the routes:
    1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
    1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
    3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
    Every pair of fields is, in fact, connected by two routes.

    It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

    思路

    此题的图很良心,都是连通图;
    所以。。。Tarjan缩点,ans=(新图中度为一的点的个数+1)/2;

    代码实现

    先试了个贪心;
     1 #include<cstdio>
     2 const int maxn=5e3+10;
     3 const int maxm=2e4+10;
     4 int f,r,ans;
     5 int a,b;
     6 int d[maxn];
     7 int main(){
     8     freopen("rpaths.in","r",stdin);
     9     freopen("rpaths.out","w",stdout);
    10     scanf("%d%d",&f,&r);
    11     for(int i=1;i<=r;i++){
    12         scanf("%d%d",&a,&b);
    13         ++d[a],++d[b];
    14     }
    15     for(int i=1;i<=f;i++) if(d[i]<2) ans++;
    16     ans=ans+1>>1;
    17     printf("%d
    ",ans);
    18     return 0;
    19 }
    6/11

    然后,正解;

     1 #include<cstdio>
     2 const int maxn=5e3+10;
     3 const int maxm=2e4+10;
     4 inline int min_(int x,int y){return x<y?x:y;}
     5 int f,r,ans;
     6 int a,b;
     7 int e[maxm][2];
     8 int h[maxn],hs=1,et[maxm],en[maxm];
     9 void add(int u,int v){
    10     ++hs,et[hs]=v,en[hs]=h[u],h[u]=hs;
    11     ++hs,et[hs]=u,en[hs]=h[v],h[v]=hs;
    12 }
    13 int q[maxn],top;
    14 int dfn[maxn],dfs,low[maxn];
    15 int d[maxn],t[maxn],ts;
    16 bool v[maxm];
    17 void tarjan(int k){
    18     dfn[k]=low[k]=++dfs;
    19     q[++top]=k;
    20     for(int i=h[k];i;i=en[i])
    21     if(!v[i]){
    22         v[i]=v[i^1]=1;
    23         if(dfn[et[i]]) low[k]=min_(low[k],dfn[et[i]]);
    24         else tarjan(et[i]),low[k]=min_(low[k],low[et[i]]);
    25     }
    26     if(!t[k]){
    27         ++ts;
    28         while(low[k]<=dfn[q[top]]) t[q[top--]]=ts;
    29     }
    30 }
    31 int main(){
    32     freopen("rpaths.in","r",stdin);
    33     freopen("rpaths.out","w",stdout);
    34     scanf("%d%d",&f,&r);
    35     for(int i=1;i<=r;i++){
    36         scanf("%d%d",&a,&b);
    37         e[i][0]=a,e[i][1]=b;
    38         add(a,b);
    39     }
    40     for(int i=1;i<=f;i++) if(!dfn[i]) tarjan(i);
    41     for(int i=1;i<=r;i++) if(t[e[i][0]]!=t[e[i][1]]) ++d[t[e[i][0]]],++d[t[e[i][1]]];
    42     for(int i=1;i<=ts;i++) if(d[i]==1) ans++;
    43     printf("%d
    ",ans+1>>1);
    44     return 0;
    45 }
  • 相关阅读:
    考试心得 模拟18
    模拟17 题解
    模拟16 题解
    考试心得 模拟17
    模拟15 题解(waiting)
    BZOJ2653 middle 【主席树】【二分】*
    BZOJ3932 CQOI2015 任务查询系统 【主席树】
    与或 【线段树】 *
    BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
    网络流--最大权闭合子图 *
  • 原文地址:https://www.cnblogs.com/J-william/p/7074361.html
Copyright © 2011-2022 走看看