• BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)


    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.

        为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分离的路径,这样她们就有多一些选择.
        每对草场之间已经有至少一条路径.给出所有R(F-1≤R≤10000)条双向路的描述,每条路连接了两个不同的草场,请计算最少的新建道路的数量, 路径由若干道路首尾相连而成.两条路径相互分离,是指两条路径没有一条重合的道路.但是,两条分离的路径上可以有一些相同的草场. 对于同一对草场之间,可能已经有两条不同的道路,你也可以在它们之间再建一条道路,作为另一条不同的道路.

    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.

        第1行输入F和R,接下来R行,每行输入两个整数,表示两个草场,它们之间有一条道路.

    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

    Solution

    首先可以发现,对于一个双连通分量,我们是不用处理它的
    那么如果将所有边双缩成一个点的话,很显然我们可以得到一颗树
    那么我们只需要处理叶子节点,在叶子节点间两两连边就好了
    答案是(叶子节点数+1)/2

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define N (5000+100)
     5 using namespace std;
     6 
     7 struct Edge{int to,next;} edge[N<<3];
     8 int n,m,u,v,head[N],num_edge;
     9 int Dfn[N],Low[N],dfs_num;
    10 int bridge_num,ans;
    11 bool Bridge[N],vis[N],dis[N][N];
    12 
    13 void add(int u,int v)
    14 {
    15     edge[++num_edge].to=v;
    16     edge[num_edge].next=head[u];
    17     head[u]=num_edge;
    18 }
    19 
    20 void Tarjan(int x,int fa)
    21 {
    22     Dfn[x]=Low[x]=++dfs_num;
    23     for (int i=head[x]; i; i=edge[i].next)
    24         if (!Dfn[edge[i].to])
    25         {
    26             Tarjan(edge[i].to,x);
    27             Low[x]=min(Low[x],Low[edge[i].to]);
    28             if (Low[edge[i].to]>Dfn[x])
    29                 Bridge[i]=Bridge[(i-1^1)+1]=true;
    30         }
    31         else if (Dfn[edge[i].to]<Dfn[x] && edge[i].to!=fa)
    32             Low[x]=min(Low[x],Dfn[edge[i].to]);
    33 }
    34 
    35 void Dfs(int x)
    36 {
    37     vis[x]=true;
    38     for (int i=head[x]; i; i=edge[i].next)
    39     {
    40         if(Bridge[i]){bridge_num++; continue;}
    41         if (!vis[edge[i].to]) Dfs(edge[i].to);
    42     }
    43 }
    44 
    45 int main()
    46 {
    47     scanf("%d%d",&n,&m);
    48     for (int i=1; i<=m; ++i)
    49         scanf("%d%d",&u,&v),dis[u][v]=dis[v][u]=true;
    50     for (int i=1; i<=n; ++i)
    51         for (int j=i+1; j<=n; ++j)
    52             if (dis[i][j])
    53                 add(i,j),add(j,i);
    54     for (int i=1; i<=n; ++i)
    55         if (!Dfn[i])
    56             Tarjan(i,0);
    57     for (int i=1; i<=n; ++i)
    58         if (!vis[i])
    59         {
    60             bridge_num=0;
    61             Dfs(i);
    62             if (bridge_num==1)
    63                 ans++;
    64         }
    65     printf("%d",(ans+1)/2);
    66 }
  • 相关阅读:
    2020年目标检测大盘点 | ECCV大盘点(附论文&代码下载)
    Transformer再下一城!low-level多个任务榜首被占领,北大华为等联合提出预训练模型IPT
    opencv------->>>>>>打印点
    生信工具
    生物信息学练习1-综合使用软件-2
    生物信息学练习1-综合使用软件
    操作指南之下载数据
    安装生物信息学软件-HUMAnN2
    多样性指数介绍
    统计学基础知识-欧式距离与其他
  • 原文地址:https://www.cnblogs.com/refun/p/9293079.html
Copyright © 2020-2023  润新知