poj3177重修道路——边双连通分量缩点
题目:http://poj.org/problem?id=3177
找桥,缩点,总之都是板子;
对于每个叶子,互相连一条边即可;若最后剩下一个,则去和根节点连边;
所以叶子节点数+1再/2即答案。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,ct=,head[],reg[],c[],dc,dfn[],low[],tim,ans;
bool bri[];
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[],dcc[];
void tarjan(int x,int e)
{
tim++;
dfn[x]=tim;
low[x]=tim;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
// if(i==(e^1))continue;
if(!dfn[u])
{
tarjan(u,i);
low[x]=min(low[x],low[u]);
if(low[u]>dfn[x])bri[i]=,bri[i^]=;//!!!
}
else if(i!=(e^))
low[x]=min(low[x],dfn[u]);
}
}
void dfs(int x)
{
c[x]=dc;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(!c[u]&&!bri[i])
dfs(u);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
memset(head,,sizeof head);
memset(reg,,sizeof reg);
memset(c,,sizeof c);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(bri,,sizeof bri);
ct=;ans=;dc=;tim=;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
tarjan(,);
for(int i=;i<=n;i++)
{
if(c[i])continue;
dc++;
dfs(i);
}
for(int i=;i<=ct;i+=)
{
int u=edge[i].to,v=edge[i^].to;
if(c[u]==c[v])continue;
reg[c[u]]++;reg[c[v]]++;
}
for(int i=;i<=dc;i++)
if(reg[i]==)ans++;
printf("%d\n",(ans+)/);
}
return ;
}
poj3177重修道路——边双连通分量缩点的更多相关文章
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- POJ3694 Network(边双连通分量+缩点+LCA)
题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
随机推荐
- 从epoll构建muduo-12 多线程入场
mini-muduo版本号传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ve ...
- 关于erlang的-run 的启动参数
在github上,关于erlang的一致性hash,有erlang-ryng和 hash_ring .在这里先聊下erlang-ryng这个. 在erlang-ryng的启动方式上,github上提供 ...
- Android 开发小工具之:Tools 属性 (转)
Android 开发小工具之:Tools 属性 http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi 今天来介绍一些 Android 开发过程中比较有用但 ...
- Android OkHttp的Cookie自己主动化管理
Android中在使用OkHttp这个库的时候.有时候须要持久化Cookie,那么怎么实现呢.OkHttp的内部源代码过于复杂,不进行深究.这里仅仅看当中的HttpEngineer里面的部分源代码,在 ...
- 【转】安卓apk反编译、修改、重新打包、签名全过程
首先明确,反编译别人apk是一件不厚道的事情.代码是程序员辛苦工作的成果,想通过这种手段不劳而获,是不对的.这也说明,代码混淆是非常重要的.本文抱着学习的态度,研究在一些特殊的情况下如果有需要,该怎么 ...
- struts2 环境建立(1)
说明:以下操作都是以本机例 在java web 开发之前,应该具备开发环境.要搭建开发环境应该具备以下工作: 1 JDK,jdk是java开发不可缺少的开发工具包. 2. 开发工具本例使用Eclips ...
- 【BZOJ2666】[cqoi2012]组装 贪心
[BZOJ2666][cqoi2012]组装 Description 数轴上有m个生产车间可以生产零件.一共有n种零件,编号为1~n.第i个车间的坐标为xi,生产第pi种零件(1<=pi< ...
- CDH使用Solr实现HBase二级索引
一.为什么要使用Solr做二级索引二.实时查询方案三.部署流程3.1 安装HBase.Solr3.2 增加HBase复制功能3.3创建相应的 SolrCloud 集合3.4 创建 Lily HBa ...
- PECL的安装和使用
下载并安装pear脚本 cd /usr/local/php/bin/ curl -o go-pear.php http://pear.php.net/go-pear.phar ./php go-pea ...
- Grunt学习笔记【8】---- grunt-angular-templates插件详解
本文主要讲如何用Grunt打包AngularJS的模板HTML. 一 说明 AngularJS中使用单独HTML模板文件的地方非常多,例如:自定义指令.ng-include.templateUrl等. ...