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<<];
  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-^)+]=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=; i<=m; ++i)
  49. scanf("%d%d",&u,&v),dis[u][v]=dis[v][u]=true;
  50. for (int i=; i<=n; ++i)
  51. for (int j=i+; j<=n; ++j)
  52. if (dis[i][j])
  53. add(i,j),add(j,i);
  54. for (int i=; i<=n; ++i)
  55. if (!Dfn[i])
  56. Tarjan(i,);
  57. for (int i=; i<=n; ++i)
  58. if (!vis[i])
  59. {
  60. bridge_num=;
  61. Dfs(i);
  62. if (bridge_num==)
  63. ans++;
  64. }
  65. printf("%d",(ans+)/);
  66. }

BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)的更多相关文章

  1. 【bzoj1718】Redundant Paths 分离的路径

    1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 964  Solve ...

  2. Redundant Paths 分离的路径【边双连通分量】

    Redundant Paths 分离的路径 题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  3. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  4. [Usaco2006 Jan] Redundant Paths 分离的路径

    1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1132  Solv ...

  5. Redundant Paths 分离的路径

    Redundant Paths 分离的路径 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她 ...

  6. [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)

    题目传送门 题目描述 为了从F个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分 ...

  7. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...

  8. 【BZOJ】1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    [题意]给定无向连通图,要求添加最少的边使全图变成边双连通分量. [算法]Tarjan缩点 [题解]首先边双缩点,得到一棵树(无向无环图). 入度为1的点就是叶子,两个LCA为根的叶子间合并最高效,直 ...

  9. bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径【tarjan】

    首先来分析一下,这是一张无向图,要求没有两条路联通的点对个数 有两条路连通,无向图,也就是说,问题转化为不在一个点双连通分量里的点对个数 tarjan即可,和求scc还不太一样-- #include& ...

随机推荐

  1. 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp

    「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...

  2. 6.ConcurrentHashMap jdk1.7

    6.1 hash算法 就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所 ...

  3. GreenPlum 大数据平台--运维(一)

    .最后分析或真空或创建表或等... Select * from pg_stat_operations where schemaname='SCHEMA NAME ' and actionname in ...

  4. 使用windows的BitLocker+VHD加密“文件夹”

    进入磁盘管理 创建VHD,选定位置 初始化创建的虚拟盘,新建简单卷 给新的盘启用BitLocker 其他: 快速锁定:manage-bde.exe D: -lock  -fd

  5. git命令(转载学习)

    Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是 ...

  6. 移动端本地 H5 秒开方案探索与实现

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 企业微信移动端项目中有需求要展示数据趋势的可视化图表,经过调研,最终决定以单页面 H5 来完成,对 APP 里的一些使用 H5 实现的功能模 ...

  7. C#基础知识-引用类型和值类型的区别(六)

    在第一篇中我们介绍了C#中基本的15种数据类型,这15种数据类型中又分为两大类,一种是值类型,一种是引用类型.值类型有sbyte.short.long.int.byte.ushort.uint.ulo ...

  8. python 中函数

    函数   def 函数名(形参):形参不用在前面定义,局部变量   参数      必须参数            必须以正确的顺序传参      关键字参数        加入关键字后可以不需要正确 ...

  9. springmvc 全局的异常拦截处理 @ControllerAdvice注解 @ExceptionHandler

    第一步: Dispatcher前端控制器的源码中 默认的 private boolean throwExceptionIfNoHandlerFound = false;说明如果没有找到匹配的执行器,不 ...

  10. 今日头条极速版邀请码以及其它APP邀请码大全

    现在大多手机新闻APP都需要输入码,在网上找了很久,最终找到一个比较全的文章,本人试过,都是可以使用的! 第1个比较好,可边看新闻,边收益!嘻嘻!平时写代码累了,休息刷一下!或者在睡觉前刷新一下,每天 ...