Road Construction(poj 3352)
题意:求最少天几条边,使这个无向图变成双连通图。
/*
tarjan缩点后,形成一棵树,求出叶子节点数tot,答案是(tot+1)/2
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 1010
using namespace std;
int num[N],low[N],instack[N],vis[N],s[N],in[N],belong[N],top,cnt,indexx;
int head[N],n,m;
struct node
{
int v,pre;
};node e[N*];
void add(int i,int x,int y)
{
e[i].v=y;
e[i].pre=head[x];
head[x]=i;
}
void tarjan(int u,int fa)
{
num[u]=low[u]=++indexx;
vis[u]=instack[u]=;
s[++top]=u;
for(int i=head[u];i!=-;i=e[i].pre)
if((fa^)!=i)
{
int v=e[i].v;
if(!vis[v])
{
tarjan(v,i);
low[u]=min(low[v],num[u]);
}
else if(instack[v])
low[u]=min(low[v],low[u]);
}
int x;
if(num[u]==low[u])
{
++cnt;
do
{
x=s[top--];
instack[x]=;
belong[x]=cnt;
}while(u!=x);
}
}
void work()
{
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
add(i*-,x,y);add(i*-,y,x);
}
for(int i=;i<=n;i++)
if(!vis[i])tarjan(i,-);
for(int i=;i<=n;i++)
for(int j=head[i];j!=-;j=e[j].pre)
if(belong[i]!=belong[e[j].v])
in[belong[e[j].v]]++;
int tot=;
for(int i=;i<=cnt;i++)
if(in[i]==)tot++;
printf("%d\n",(tot+)/);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
top=cnt=indexx=;
memset(num,,sizeof(num));
memset(low,,sizeof(low));
memset(instack,,sizeof(instack));
memset(vis,,sizeof(vis));
memset(s,,sizeof(s));
memset(head,-,sizeof(head));
memset(e,,sizeof(e));
memset(in,,sizeof(in));
memset(belong,,sizeof(belong));
work();
}
return ;
}
Road Construction(poj 3352)的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- POJ - 3352 Road Construction(边双连通分支)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板) 与这道题一模一样.代码就改了下范围,其他都没动 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- AOJ 2249 Road Construction(Dijkstra+优先队列)
[题目大意] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249 [题目大意] 一张无向图,建造每条道路需要的费用已经给出, 现 ...
- HDU 5813 Elegant Construction(优雅建造)
HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65 ...
- POJ3352 Road Construction(边双连通分量)
...
- 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)
Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
随机推荐
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- PRML Chapter 1. Introduction
PRML Chapter 1. Introduction 为了防止忘记,要把每章的重要内容都记下来,从第一章开始 2012@3@28 今天又回去稍微翻了一下第一章内容,发现第一次看的时候没有看透,每次 ...
- 动态插入、添加删除表格行的JS代码
<html> <head> <title>Table对象的方法</title> <script language="JavaScript ...
- WPF 动态布局Grid
//开启线程加载 Action a = () => { ; ; var path = "../../face_img/"; var files = Directory.Get ...
- Gradle用户指南(1)-Gradle安装
前置条件 Gradle 需要 Java JDK 或者 JRE,版本是 6 及以上.Gradle 将会装载自己的 Groovy 库,因此,Groovy 不需要被安装.任何存在的 Groovy 安装都会被 ...
- 修改linux最大文件句柄数
大家知道在linux服务器大并发调优时,往往需要预先调优linux参数,其中修改linux最大文件句柄数是最常修改的参数之一. 在linux中执行ulimit -a 即可查询linux相关的参数,如下 ...
- HTML 标准属性 和 事件属性
HTML的公共属性 HTML 和 XHTML 标签支持的标准属性 核心属性 (Core Attributes) 以下标签不提供下面的属性:base.head.html.meta.param.scrip ...
- 在表单(input)中id和name的区别
但是name在以下用途是不能替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是 ...
- FineUI第八天----下拉列表控件
下拉列表控件 3.模拟树的下拉列表: 其他的控件都跟Asp.net的差不多.
- PHP中通过加号合并数组
通常,我们合并多个数组用的是array_merge()函数,其实,PHP手册中关于数组操作符的介绍给了我们更简单的方法,那就是"+"号,看看下面的例子就明白了(详细了解) 代码: ...