[双连通分量] 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 ...
随机推荐
- IO/ACM中来自浮点数的陷阱(收集向)
OI/ACM中经常要用到小数来解决问题(概率.计算几何等),但是小数在计算机中的存储方式是浮点数而不是我们在作数学运算中的数,有精度的限制. 以下以GUN C++为准,其他语言(或编译器)也差不了多少 ...
- Spring 文章推荐
spring mvc 异常统一处理方式:http://www.cnblogs.com/xd502djj/archive/2012/09/24/2700490.html 在springmvc中使用hib ...
- Python—I/O多路复用
一.I/O多路复用概念: 监听多个描述符的状态,如果描述符状态改变,则会被内核修改标志位,从而被进程获取进而进行读写操作 二.select,poll,epoll select模块,提供了:select ...
- samba的rpm包,只有tar.gz文件安装
tar.gz包是源代码的包,不能直接运行,需要安装tar xvf samba-3.5.1.tar.gz cd samba-3.5.1 &&cd source3 &&./ ...
- python:HTML + CSS 优先级 返回顶部
优先级 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- python中的正则表达式(re模块)
一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎 ...
- codeforces VK cup 2016-round 1 D.Bear and Contribution
题意大概就是有n个数字,要使至少有k个相同,可以花费b使一个数+5,可以花费c使一个数+1,求最小花费. 要对齐的数肯定是在[v,v+4]之间,所以分别枚举模为0~4的情况就可以了. 排序一下,然后化 ...
- c++11中的for简化用法
1.序列for循环 map<string,int> m{{"a",1},{"b",2},{"c",3}} for(auto p: ...
- spring netty-socket.io
spring-nettysocketio.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?>< ...
- Message Queue vs. Web Services?
From stackoverflow.com When you use a web service you have a client and a server: If the server fail ...