poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
#include<stdio.h>/*
求边双联通分量和求强连通差不多,先缩点求出叶子节点的个数
*/
#include<string.h>
#define N 5100
struct node {
int u,v,next;
}bian[N*4];
int dfn[N],low[N],head[N],index,cnt,yong,stac[N],suo[N],vis[N],top,degree[N];
void init() {
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
index=0;cnt=0;yong=0;top=0;
memset(degree,0,sizeof(degree));
}
int Min(int a,int b) {
return a>b?b:a;
}
void addedge(int u,int v) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
void tarjan(int u,int fa) {
dfn[u]=low[u]=++index;
vis[u]=1;
stac[++top]=u;
int i;
for(i=head[u];i!=-1;i=bian[i].next) {
int v=bian[i].v;
if(i==(fa^1))continue;//注意优先级
if(!dfn[v]) {
tarjan(v,i);
low[u]=Min(low[u],low[v]);
}
else if(vis[v])
low[u]=Min(low[u],dfn[v]);
}
if(low[u]==dfn[u]) {
cnt++;
int t;
do{
t=stac[top--];
vis[t]=0;
suo[t]=cnt;
}while(u!=t);
}
}
int main() {
int n,m,i,a,b;
while(scanf("%d%d",&n,&m)!=EOF) {
init();
while(m--) {
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
for(i=1;i<=n;i++)
if(!dfn[i])
tarjan(i,-1);
for(i=0;i<yong;i++) {
int u,v;
u=bian[i].u;
v=bian[i].v;
if(suo[u]!=suo[v]) {
degree[suo[u]]++;
degree[suo[v]]++;
}
}
int leave=0;
for(i=1;i<=cnt;i++)
if(degree[i]==2)
leave++;
printf("%d\n",(leave+1)/2);
}
return 0;
}
poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的的更多相关文章
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- 大白书中无向图的点双联通分量(BCC)模板的分析与理解
对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...
- HDU4738 Caocao's Bridges —— 边双联通分量 + 重边
题目链接:https://vjudge.net/problem/HDU-4738 A network administrator manages a large network. The networ ...
- HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
- 【POJ 3177】Redundant Paths(边双连通分量)
求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...
- 边双连通分量 jarjan (poj 3177)
大意:给定一个无向连通图,判断至少加多少的边,才能使任意两点之间至少有两条的独立的路(没有公共的边,但可以经过同一个中间的顶点). 思路:在同一个双连通分量里的所有的点可以看做一个点,收缩后,新图是一 ...
- 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
随机推荐
- CMDBuild安装
近日来,老板要在内部部署一套IT资产管理系统,要笔者去调研一下,测试了GLPI.OCSNG(没记错吧)和CMDBuild之后,发现还是CMDBuild的功能较为强大,虽然暂时不具备SNMP之类的工具, ...
- 怎样动态设置GridView的宽和高(Android)
LinearLayout.LayoutParams linearParams2 = (LinearLayout.LayoutParams)liveGrid.getLayoutParams(); lin ...
- Android系统升级那些事儿【转】
本文转载自:http://blog.csdn.net/chenyufei1013/article/details/12705719 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?) ...
- 使用tortoisegit修改日志
http://stackoverflow.com/questions/17647936/how-do-i-edit-an-incorrect-commit-message-with-tortoiseg ...
- C语言实现字符串拼接
#include <stdio.h>#include <stdlib.h>#include <string.h> char* str_contact(const c ...
- Constructing Roads In JGShining's Kingdom(LIS)
http://acm.hdu.edu.cn/showproblem.php?pid=1025 题意:富人路与穷人路都分别有从1到n的n个点,现在要在富人点与穷人点之间修路,但是要求路不能交叉,问最多能 ...
- 常用的 Maven 命令
maven 命令的格式为 mvn [plugin-name]:[goal-name],可以接受的参数如下. -D 指定参数,如 -Dmaven.test.skip=true 跳过单元测试: -P 指定 ...
- SQL Server 行转列,列转行
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...
- RabbitMQ .NET消息队列使用入门(二)【多个队列间消息传输】
孤独将会是人生中遇见的最大困难. 实体类: DocumentType.cs public enum DocumentType { //日志 Journal = 1, //论文 Thesis = 2, ...
- Maven项目pom.xml配置详解
maven项目pom.xml文件配置详解,需要时可以用作参考: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...