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& ...
随机推荐
- 洛谷U5653 宋荣子的小饼干
题目描述 楼下机房的LYL有n个妹子,分别编号为a1,a2……an,每个妹子都拥有一定数量的小饼干.有一天,saruka没有吃晚饭,饿的不要不要的,这时,他忽然想起了LYL的妹子们有小饼干可以吃.于是 ...
- java中不带package和带package的编译运行方式
Java中不带package的程序和带package的程序编译的方式是不同的. 一.不带package的程序建立个HelloWorld.java的文件,放入C:\,内容如下:public class ...
- DOM-based xss
这个漏洞往往存在于客户端脚本,如果一个Javascript脚本访问需要参数的URL,且需要将该信息用于写入自己的页面,且信息未被编码,那么就有可能存在这个漏洞. (一)DOM—based XSS漏洞的 ...
- linux 定时 svn 代码更新,配置文件不修改
普通参数: 普通参数为正常的传参数: 例子: f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束. 例子: ...
- python集合类型set
set 类型的简单粗暴取出并集合交集 | & li=[11,22,33] n_li=[44,55] b= (list(set(li)&set(n_li))) b2=set(li) ...
- 新浪微博客户端(8)-添加按钮到TabBar
在tabBar的中部添加一个发送微博的按钮. DJTabBar.h #import <UIKit/UIKit.h> @class DJTabBar; @protocol DJTabBarD ...
- Unity3D占用内存太大的解决方法
原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...
- linux shell 字符串操作(长度,查找,替换)详解
linux shell 字符串操作(长度,查找,替换)详解 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系 ...
- 查看现有运行的linux服务器有多少内存条
i161 admin # ssh 192.168.5.209 dmidecode | grep 'Ending Address' -B1 -A2 Starting Address: 0x0000 ...
- Redis使用介绍
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyth ...