zoukankan      html  css  js  c++  java
  • Redundant Paths(poj 3177 边双连通分量)

    Redundant Paths
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 21252   Accepted: 8773

    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.
     
    解题思路: 题目意思求解最少加几条边使得每一个点到另外任意一个点至少存在两条边
    边双连通分量 缩点后把出度为0的点相连即可
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn=5005;
     8 int n,m;
     9 int cnt,head[maxn];
    10 int dfn[maxn],low[maxn],scc[maxn],vis[maxn],stk[maxn],index=0,sccnum=0,top=0;
    11 struct Edge{
    12     int v,nxt;
    13 }G[maxn*2];
    14 void init(){ cnt=0; memset(head,-1,sizeof(head)); }
    15 void add(int u,int v){
    16     G[cnt].v=v;
    17     G[cnt].nxt=head[u];
    18     head[u]=cnt++;
    19 }
    20 
    21 void tarjan(int root){   //tarjan 模板
    22     if(dfn[root]) return;
    23     dfn[root]=low[root]=++index;
    24     stk[++top]=root;
    25 
    26     for(int i=head[root];~i;i=G[i].nxt){
    27         int v=G[i].v;
    28         if(vis[i]) continue;
    29         vis[i]=vis[i^1]=1;  //边和反边标记为1
    30         if(!dfn[v]){
    31             tarjan(v);
    32             low[root]=min(low[root],low[v]);
    33         }
    34         else{   //如果还在站内
    35             low[root]=min(low[root],dfn[v]);
    36         }
    37     }
    38     if(low[root]==dfn[root]){
    39         sccnum++;
    40         for(;;){
    41             int x=stk[top--];
    42             scc[x]=sccnum;
    43             if(x==root) break;
    44         }
    45     }
    46 }
    47 
    48 int degree[maxn];
    49 void solve(){
    50     index=0,sccnum=0,top=0;
    51     memset(dfn,0,sizeof(dfn));
    52     memset(low,0,sizeof(low));
    53     memset(scc,0,sizeof(scc));
    54     memset(stk,0,sizeof(stk));
    55     for(int i=1;i<=n;i++){
    56         if(!dfn[i]){
    57             tarjan(i);
    58         }
    59     }
    60     for(int i=1;i<=sccnum;i++){
    61         degree[i]=0;
    62     }
    63     for(int i=1;i<=n;i++){   //重新建图
    64         for(int j=head[i];~j;j=G[j].nxt){
    65             int v=G[j].v;
    66             if(scc[i]!=scc[v]){
    67                 degree[scc[i]]++;
    68                 degree[scc[v]]++;
    69             }
    70         }
    71     }
    72     int res=0;
    73     for(int i=1;i<=sccnum;i++){
    74         if(degree[i]==2) res++;   //无向图  或者前面记录自己搞搞一个方向
    75     }
    76     printf("%d
    ",(res+1)/2);
    77 }
    78 
    79 int main(){
    80     while(scanf("%d%d",&n,&m)==2){
    81         init();
    82         for(int i=1,d1,d2;i<=m;i++){
    83             scanf("%d%d",&d1,&d2);
    84             add(d1,d2);
    85             add(d2,d1);
    86         }
    87         solve();
    88     }
    89     return 0;
    90 
    91 }
    View Code
  • 相关阅读:
    python 字符串替换
    python 字符串截取
    python 字符串连接
    PHP 做群发短信(短信接口连接问题)
    Yii dropDownList 下拉菜单 联动菜单
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    SQL Server 2008 R2——分组取前几名
    SQL Server 分组后取Top N
    大型网站架构系列:20本技术书籍推荐
    较主流的消息队列的比较与选型
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11963022.html
Copyright © 2011-2022 走看看