POJ 3177 Redundant Paths(Tarjan)
题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 。
思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1。然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 ;
//
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std ; int head[],fb[],low[],dfn[] ,degree[];
int cnt,timee ,ans ;
struct node
{
int u ;
int v ;
int next ;
}p[]; void tarjan(int u)
{
dfn[u] = low[u] = ++timee ;
for(int i = head[u] ; i != - ; i = p[i].next)
{
int v = p[i].v ;
if(fb[i^]) continue ;
fb[i] = ;
if(dfn[v]) low[u] = min(low[u],dfn[v]) ;
else
{
tarjan(v) ;
low[u] = min(low[v],low[u]) ;
//if(low[v] > dfn[u])
// ans /++ ;
}
}
}
void Init()
{
cnt = timee = ans = ;
memset(head,-,sizeof(head)) ;
memset(dfn,,sizeof(dfn)) ;
memset(low,,sizeof(low)) ;
memset(fb,,sizeof(fb)) ;
memset(degree,,sizeof(degree)) ;
}
void addedge(int u,int v)
{
p[cnt].u = u ;
p[cnt].v = v ;
p[cnt].next = head[u] ;
head[u] = cnt ++ ;
p[cnt].u = v ;
p[cnt].v = u ;
p[cnt].next = head[v] ;
head[v] = cnt ++ ;
}
int main()
{
int F,R ;
while(scanf("%d %d",&F,&R) != EOF)
{
Init() ;
int x,y ;
for(int i = ; i < R ; i++)
{
scanf("%d %d",&x,&y) ;
addedge(x-,y-) ;
}
tarjan() ;
for(int i = ; i < F ; i++)
{
for(int j = head[i] ; j != - ; j = p[j].next)
{
if(low[i] != low[p[j].v])
degree[low[i]] ++ ; }
}
for(int i = ; i <= F ; i ++)// 时间戳是从1开始的,所以low的值是可以到达F的,
if(degree[i] == )
ans ++ ;
printf("%d\n",(ans + )/) ;
}
return ;
}
POJ 3177 Redundant Paths(Tarjan)的更多相关文章
- POJ 3177 Redundant Paths (tarjan边双连通分量)
题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...
- poj 3177 Redundant Paths(tarjan边双连通)
题目链接:http://poj.org/problem?id=3177 题意:求最少加几条边使得没对点都有至少两条路互通. 题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
- POJ 3177 Redundant Paths(重边标记法,有重边的边双连通分支)
大致题意: 为了保护放牧环境,避免牲畜过度啃咬同一个地方的草皮,牧场主决定利用不断迁移牲畜进行喂养的方法去保护牧草.然而牲畜在迁移过程中也会啃食路上的牧草,所以如果每次迁移都用同一条道路,那么该条道路 ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
- POJ 3177 Redundant Paths(边双连通分量)
[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...
- Poj 3177 Redundant Paths (双连通分支+节点统计)
题目描述: 给出一个无向的连通图,问最少加入几条边,才能使所给的图变为无桥的双连通图? 解题思路: 可以求出原图中所有的不包含桥的所有最大连通子图,然后对连通子图进行标记缩点,统计度为1的叶子节点le ...
随机推荐
- Android--将字节数转化为B,KB,MB,GB的方法
//将字节数转化为MB private String byteToMB(long size){ long kb = 1024; long mb = kb*1024; long gb = mb*1024 ...
- android开发遇到SDK无法访问谷歌而安装不了的情况
遇到SDK无法访问谷歌而安装不了的情况 1.修改C:\Windows\System32\drivers\etc的HOSTS文件,添加 #google_android更新203.208.46.146 d ...
- golang的goroutine与channel
Golang的goroutine是非抢占式的, 令人相当蛋疼! 有痛不能呻吟...只能配合channel在各goroutine之间传递信号来实现抢占式, 而这形成了golang最灵活与最具性能的核心. ...
- Paragon NTFS for Mac免费获取官方赠送正版.更新获取ntfs for mac 14方法
Paragon NTFS for Mac免费获取官方赠送正版,没有这个软件的朋友赶紧收下.获取地址http://www.paragon-drivers.com/cn/ntfs-mac-free/ntf ...
- shell 基本结构
就像其他的编程语言一样,shell也有三种基本的结构:顺序结构.分支结构.循环结构.顺序结构就是按照命令的出现顺序依次执行,比较简单.如下分别介绍分支结构和循环结构. 分支结构 格式1: if com ...
- online training
https://www.skillfeed.com/browse http://teamtreehouse.com/features http://www.pluralsight.com/ https ...
- 软件工程——四则运算3(C#)
一.设计思想 设计两个窗体,在第一个窗体中选择功能参数,在第二个窗体中显示所出题目. 二.源代码 Form1.cs: using System; using System.Collections.Ge ...
- PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re
我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...
- 阿里云:linux 一键安装web环境
参考地址:http://www.cnblogs.com/ada-zheng/p/3724957.html
- Invalid object name ‘sys.configurations’. (Microsoft SQL Server, Error: 208)
http://blogs.msdn.com/b/ramaprasanna/archive/2009/09/16/invalid-object-name-sys-configurations-micro ...