#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 模板求的的更多相关文章

  1. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  2. 大白书中无向图的点双联通分量(BCC)模板的分析与理解

    对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...

  3. HDU4738 Caocao's Bridges —— 边双联通分量 + 重边

    题目链接:https://vjudge.net/problem/HDU-4738 A network administrator manages a large network. The networ ...

  4. HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...

  5. POJ 3177 Redundant Paths (桥,边双连通分量,有重边)

    题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...

  6. 【POJ 3177】Redundant Paths(边双连通分量)

    求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...

  7. 边双连通分量 jarjan (poj 3177)

    大意:给定一个无向连通图,判断至少加多少的边,才能使任意两点之间至少有两条的独立的路(没有公共的边,但可以经过同一个中间的顶点). 思路:在同一个双连通分量里的所有的点可以看做一个点,收缩后,新图是一 ...

  8. 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)

    题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...

  9. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

随机推荐

  1. HDU - 3622 Bomb Game(二分+2-SAT)

    题目大意:玩一个放炸弹游戏,有N次放炸弹的机会,每次放炸弹时,你都有两个位置能够选择.问怎样放炸弹,能使爆炸的炸弹的半径的最小值最大(炸弹爆炸半径能够控制,可是爆炸形成的圈不能有重叠部分) 解题思路: ...

  2. oc30--id

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (void)sleep; @ ...

  3. 【POJ 2230】 Watchcow

    [题目链接] http://poj.org/problem?id=2230 [算法] 欧拉回路 [代码] #include <algorithm> #include <bitset& ...

  4. linux更换阿里云的源的shell脚本

    #!/bin/bash##########################################Function: update source#Usage: bash update_sour ...

  5. APP加固反调试(Anti-debugging)技术点汇总

    0x00 时间相关反调试 通过计算某部分代码的执行时间差来判断是否被调试,在Linux内核下可以通过time.gettimeofday,或者直接通过sys call来获取当前时间.另外,还可以通过自定 ...

  6. [转]RDLC报表格式化format表达式

    本文转自:http://www.cnblogs.com/samlin/archive/2012/04/17/FormatDateTime.html 刚开始接触RDLC报表,觉得RDLC报表提供的格式化 ...

  7. for 循环的中的i

    for循环中的i,如果倒过来判断从某数一直到0,一定不能用unsigned int类型的i,因为unsigned int不可能小于0,当i=0后,i--将达到最大的unsigned int,依旧> ...

  8. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  9. Django中关于MySQL的bug总结

    bug one: You are trying to add a non-nullable field 'height' to person without a default; we can't d ...

  10. angularJS之ng-bind与{{}}取值的区别

    1:{{ }} 是等页面加载完后,再取值. 2:ng-bind 它是在页面加载的时候,是不会显示{{name}}这种变量出来. 3:ng-bind 可以解决 ng 页面闪烁加载问题. 4:ng-bin ...