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

 #include<iostream>
#include<cstring>
#include<cstdio>
#define N (5000+100)
using namespace std; struct Edge{int to,next;} edge[N<<];
int n,m,u,v,head[N],num_edge;
int Dfn[N],Low[N],dfs_num;
int bridge_num,ans;
bool Bridge[N],vis[N],dis[N][N]; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Tarjan(int x,int fa)
{
Dfn[x]=Low[x]=++dfs_num;
for (int i=head[x]; i; i=edge[i].next)
if (!Dfn[edge[i].to])
{
Tarjan(edge[i].to,x);
Low[x]=min(Low[x],Low[edge[i].to]);
if (Low[edge[i].to]>Dfn[x])
Bridge[i]=Bridge[(i-^)+]=true;
}
else if (Dfn[edge[i].to]<Dfn[x] && edge[i].to!=fa)
Low[x]=min(Low[x],Dfn[edge[i].to]);
} void Dfs(int x)
{
vis[x]=true;
for (int i=head[x]; i; i=edge[i].next)
{
if(Bridge[i]){bridge_num++; continue;}
if (!vis[edge[i].to]) Dfs(edge[i].to);
}
} int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<=m; ++i)
scanf("%d%d",&u,&v),dis[u][v]=dis[v][u]=true;
for (int i=; i<=n; ++i)
for (int j=i+; j<=n; ++j)
if (dis[i][j])
add(i,j),add(j,i);
for (int i=; i<=n; ++i)
if (!Dfn[i])
Tarjan(i,);
for (int i=; i<=n; ++i)
if (!vis[i])
{
bridge_num=;
Dfs(i);
if (bridge_num==)
ans++;
}
printf("%d",(ans+)/);
}

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. linux 拓展之linux纯命令行界面变为图形界面

    使用版本为6.x 1, 连接网络  没网络就下载不了哦 2,设置下yum源,我本机原来的yum源是网易的但是我用不了,我设置阿里云可以下载, 你们有这问题的可以试试    3, yum groupin ...

  2. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  3. webservice 介绍

    Web service 即web服务,它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术. l 采用标准SOAP(Simple Object Access Protocol)  协议 ...

  4. TOJ 3635 过山车

    Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找 个个男生做partne ...

  5. linux_api之进程环境(二)

      本篇索引: 1.引言 2.终端登录 3.进程组 4.会话期 1.引言 通过上一篇的学习,我们已经知道了如何控制一个进程,fork函数从父进程中复制出子进程,我们可以通过exec函数让子进程运行新的 ...

  6. poi excel 常用api

    http://www.cnblogs.com/huajiezh/p/5467821.html

  7. 为什么一段时间后网站后台自动退出 php中session过期时间设置

    修改php配置文件中的session.gc_maxlifetime.如果想了解更多session回收机制,继续阅读.(本文环境php5.2) 概述:每一次php请求,会有1/100的概率(默认值)触发 ...

  8. 3..net可以做什么

    .net可以做什么呢? (1)桌面应用程序  Winform(.net开发的桌面应用程序叫winform应用程序) (2)internet应用程序 ASP.net(.net开发的internet应用程 ...

  9. 二、MVC3+EF单表增删改查

    document 表为例 写入静态类 NorthwindDataProvider: Controller可直接调用:如 //获取document表全部数据 NorthwindDataProvider. ...

  10. python搭建本地服务器

    python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...