[双连通分量] POJ 3177 Redundant Paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13712 | Accepted: 5821 |
Description
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
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.
Source
#include<stdio.h>
#include<string.h>
struct mp
{
int begin,to,next;
}map[10010];
int frist[10010],num,dfn[10010],low[10010],times,bridgenum,father[10010];
int degree[10010],con[10010],stack[10010],cnt,top,s[10010];
bool count[10010];
void init()
{
memset(father,0,sizeof(father));
memset(count,0,sizeof(count));
memset(con,0,sizeof(con));
memset(degree,0,sizeof(degree));
memset(s,0,sizeof(s));
memset(stack,0,sizeof(stack));
memset(frist,0,sizeof(frist));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(map,0,sizeof(map));
num=times=bridgenum=cnt=top=0;
}
void add(int x,int y)
{
++num;
map[num].begin=x;map[num].to=y;
map[num].next=frist[x];frist[x]=num;
return;
}
void tarjian(int v)
{
int i;bool flag=true;
dfn[v]=low[v]=++times;
stack[top++]=v;
for(i=frist[v];i;i=map[i].next)
{
if(map[i].to==father[v]&&flag)
{
flag=false;
continue;
}
if(!dfn[map[i].to])
{
father[map[i].to]=v;
tarjian(map[i].to);
if(low[map[i].to]<low[v]) low[v]=low[map[i].to];
}
else
if(dfn[map[i].to]<low[v]) low[v]=dfn[map[i].to];
}
if(low[v]==dfn[v])
{
++cnt;
do
{
v=stack[--top];
s[v]=cnt;
}while(dfn[v]!=low[v]);
}
}
int main()
{
int n,m,a,b,i,u,v,k=0,ans=0,j;
init();
scanf("%d%d",&n,&m);
for(i=0;i<m;++i)
{
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
tarjian(1);
for(i=1;i<=n;++i)
{
for(j=frist[i];j;j=map[j].next)
{
v=map[j].to;
if(s[i]!=s[v])
{
++degree[s[i]];
++degree[s[v]];
}
}
}
for(i=1;i<=cnt;++i)
if(degree[i]==2)
++ans;
printf("%d\n",(ans+1)/2);
return 0;
}
[双连通分量] POJ 3177 Redundant Paths的更多相关文章
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ 3177 Redundant Paths(边双连通分量)
[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...
- POJ 3177 Redundant Paths (tarjan边双连通分量)
题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- 获取乌云补天指定关键字的漏洞并输出URL和标题
#coding=utf-8 import urllib2 import re import time def timestamp(): return str(time.strftime ...
- Webstorm的序列号和证书
User Name: ------------name-------------- EMBRACE -------------name-------------- License Key: ===== ...
- MS CRM商机产品等Mapping
商机明细:报价明细 SELECT EntityMapId FROM EntityMapBase WHERE TargetEntityName='quotedetail' AND So ...
- Grunt备忘录
一.安装Grunt 应在全局环境下安装Grunt,以保障在任何目录下都能够正确找到grunt npm install grunt-cli -g 二.对已有Grunt基本目录结构文件进行操作 Grunt ...
- iOS 开发笔记-AFNetWorking https SSL认证
一般来讲如果app用了web service , 我们需要防止数据嗅探来保证数据安全.通常的做法是用ssl来连接以防止数据抓包和嗅探 其实这么做的话还是不够的 . 我们还需要防止中间人攻击(不明白的自 ...
- php常用函数汇总
php常用函数汇总 字符串截取: 1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束'); * 截取英文或者数字 ...
- HTML5的数据自动补齐功能
使用datalist元素,HTML5允许使用一组数据来生成自动补齐功能,现在你不需要使用第三方js代码或者类库啦! <input name="frameworks" list ...
- HashCode
如果一个类的对象要用做hashMap的key,那么一定要注意覆盖该类的equals和hashCode方法. equals()是基类Object的方法,用于判断对象是否有相同地址及是否为同一对象 pub ...
- Deep Learning Papers Reading Roadmap
Deep Learning Papers Reading Roadmap https://github.com/songrotek/Deep-Learning-Papers-Reading-Roadm ...
- JPA入门案例详解(附源码)
1.新建JavaEE Persistence项目