POJ - 3177 Redundant Paths(边双连通分支)(模板)
1、给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
2、
3、
//边双连通分支
/*
去掉桥,其余的连通分支就是边双连通分支了。一个有桥的连通图要变成边双连通图的话,
把双连通子图收缩为一个点,形成一颗树。需要加的边为(leaf+1)/2(leaf为叶子结点的个数)
POJ 3177 给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int MAXN=;//点数
const int MAXM=;//边数,因为是无向图,所以这个值要 *2
struct Edge{
int to,next;
bool cut;//是否是桥标记
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong 数组的值是1~block
int Index,top;
int block;//边双连通块数
bool Instack[MAXN];
int bridge;//桥的数目
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].cut=false;
head[u]=tot++;
}
void Tarjan(int u,int pre){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
for(int i=head[u];i!=-;i=edge[i].next){
v=edge[i].to;
if(v==pre)continue;
if(!DFN[v]){
Tarjan(v,u);
if(Low[u]>Low[v])Low[u]=Low[v];
if(Low[v]>DFN[u]){
bridge++;
edge[i].cut=true;
edge[i^].cut=true;
}
}
else if(Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if(Low[u]==DFN[u]){
block++;
do{
v=Stack[--top];
Instack[v]=false;
Belong[v]=block;
}
while(v!=u);
}
}
void init(){
tot=;
memset(head,-,sizeof(head));
}
int du[MAXN];//缩点后形成树,每个点的度数
void solve(int n){
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index=top=block=;
Tarjan(,);
int ans=;
memset(du,,sizeof(du));
for(int i=;i<=n;i++)
for(int j=head[i];j!=-;j=edge[j].next)
if(edge[j].cut)
du[Belong[i]]++;
for(int i=;i<=block;i++)
if(du[i]==)
ans++;
//找叶子结点的个数 ans,构造边双连通图需要加边(ans+1)/2
printf("%d\n",(ans+)/);
}
int main(){
int n,m;
int u,v;
while(scanf("%d%d",&n,&m)==){
init();
while(m--){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
}
return ;
}
POJ - 3177 Redundant Paths(边双连通分支)(模板)的更多相关文章
- Poj 3177 Redundant Paths (双连通分支+节点统计)
题目描述: 给出一个无向的连通图,问最少加入几条边,才能使所给的图变为无桥的双连通图? 解题思路: 可以求出原图中所有的不包含桥的所有最大连通子图,然后对连通子图进行标记缩点,统计度为1的叶子节点le ...
- 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(边双连通的构造)
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 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
随机推荐
- react native 标签出错.
这种错误为标签错误,没办法,你只能往标签上找了,但不一定是<Text></Text>,我是在<TextInput></TextInput>上出错的,多了 ...
- 大数据学习——yarn集群启动
启动yarn命令: start-yarn.sh 验证是否启动成功 jps查看进程 http://192.168.74.100:8088页面 关闭 stop-yarn.sh
- xtu read problem training 3 B - Gears
Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3789 ...
- Bone Collector II(01背包kth)
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- 【Ajax 2】封装Ajax的核心对象:XMLHttpRequest对象
导读:AJAX利用一个构建到所有现代浏览器内部的对象-XMLHttpRequest-来实现发送和接收HTTP请求与响应信息.那么,XMLHttpRequest对象是怎么创建和封装的呢? 一.简介 1. ...
- nginx1.6.3
Nginx1.6.3安装配置 安装时关闭防火墙和selinuxservice iptables stopsed -i "s/selinux=enabled/selinux=disable/g ...
- Android应用的权限配置和权限列表
权限配置写在Mainifest.xml文件中: <?xml version="1.0" encoding="utf-8"?> <manifes ...
- mySQL windows 服务
https://www.jizhuba.com/kejiyouxi/20171001/6006.html
- poj——3118 Arbiter
Arbiter 题目描述: “仲裁者”是<星际争霸>科幻系列中的一种太空船.仲裁者级太空船是神族的战船,专门提供精神力支援.不像其他战船的人员主要是战士阶级,仲裁者所承载的都 ...
- Permutations(排列问题,DFS回溯)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...