POJ 3177 Redundant Paths 边双(重边)缩点
分析:边双缩点后,消环变树,然后答案就是所有叶子结点(即度为1的点)相连,为(sum+1)/2;
注:此题有坑,踩踩更健康,普通边双缩短默认没有无向图没有重边,但是这道题是有的
我们看,low数组是我们缩点的关键,记录最早的前驱,但是不能由父边过来,这是因为没有重边
当有重边时,父边也可以更新low数组,所以我们只需要忽略父边第一次出现,这样第二次出现时,就可以用了
这样就完美解决了重边问题
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <stack>
using namespace std;
typedef long long LL;
const int N = 5e3+;
int head[N],tot,n,cnt,m;
struct Edge{
int u,v,next;
}edge[N<<];
void add(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int dfn[N],low[N],clk,bel[N],d[N];
stack<int>s;
void targin(int u,int f){
dfn[u]=low[u]=++clk;
s.push(u);
bool flag=;//判断重边
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(v==f&&!flag){
flag=;//记录重边出现次数,只跳过父边第一次出现
continue;
}
if(!dfn[v]){
targin(v,u);
low[u]=min(low[u],low[v]);
}
else if(dfn[v]<low[u])low[u]=dfn[v];
}
if(dfn[u]==low[u]){
++cnt;
int k;
do{
k=s.top();
s.pop();
bel[k]=cnt;
}while(k!=u);
}
}
int main(){ memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
while(m--){
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
targin(,-);
for(int i=;i<tot;i+=){
int k1=bel[edge[i].u],k2=bel[edge[i].v];
if(k1==k2)continue;
++d[k1],++d[k2];
}
int sum=;
for(int i=;i<=n;++i)if(d[i]==)++sum;
printf("%d\n",++sum/);
return ;
}
POJ 3177 Redundant Paths 边双(重边)缩点的更多相关文章
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
- POJ 3177 Redundant Paths (边双连通+缩点)
<题目链接> <转载于 >>> > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...
- POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)
这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...
- 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【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
- 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 ...
随机推荐
- Qt播放mp3
.pro项目文件中加入 QT += phonon 包含头 #include <phonon/Phonon> 播放文件 Phonon::MediaObject *media = ...
- man手册使用
1.是普通的命令 2.是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件) 3.是库函数,如printf,fread 4.是特殊文件,也就是/dev ...
- 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
http://blog.sina.com.cn/s/blog_458f871201017i06.html 这篇文章写的不错,从比较宏观的角度说了下作者在OGRE中实现的延迟渲染
- 宏 #,##,_ _VA_ARGS_ _
宏里面使用: 一.# 转为字符串 #define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x)) ...
- 12 求1+2+...+n
参考 http://www.cppblog.com/zengwei0771/archive/2012/04/28/173014.html 和 http://blog.csdn.net/shiren_b ...
- 6大排序算法,c#实现
using System; using System.Text; using System.Collections.Generic; namespace ArithmeticPractice { st ...
- codeforces #309 div1 A
先说我的解法吧 首先设f(i,j)表示选了前i个球且j种颜色都已经选完了的方案数 这显然是可以随便转移的 #include<cstdio> #include<cstring> ...
- Qt: 多线程, 就是这么简单(确实非常简洁明了)
#include <iostream>#include <QApplication>#include <QThread>#include <QString&g ...
- 万网空间如何安装wordpress
万网空间如何安装wordpress建站教程 _ 学做网站论坛 http://www.xuewangzhan.com/wpbbs/1643.html 1.先在本地下载一个最新版本的wordpress ...
- SQL盲注修订建议
一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...