题意:求最少天几条边,使这个无向图变成双连通图。

/*
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)的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. POJ - 3352 Road Construction(边双连通分支)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板)  与这道题一模一样.代码就改了下范围,其他都没动 ...

  3. 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 ...

  4. AOJ 2249 Road Construction(Dijkstra+优先队列)

    [题目大意] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2249 [题目大意] 一张无向图,建造每条道路需要的费用已经给出, 现 ...

  5. HDU 5813 Elegant Construction(优雅建造)

    HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65 ...

  6. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  7. 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)

    Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...

  8. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  9. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

随机推荐

  1. NOI2002 洛谷 P1196 银河英雄传说

    神奇的并查集问题 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩 ...

  2. codeforce626C.Block Towers(二分)

    C. Block Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Servlet------(声明式)异常处理

    Test.java 其他方法不变,重写 protected void service()方法 public void init(ServletConfig config) throws Servlet ...

  4. JS小记

    好记性不如烂笔头. 1.document.ElementFromPoint:根据坐标获得元素 2.有时候要操作DOM页面,但是得不到预期结果,很可能是因为页面还没加载完成,在console控制台可以看 ...

  5. 裴波那契数列 JavaScript 尾递归实现

    一般递归实现 : //经典递归 function fibonacci(n) { return (function(n) { ) ; ); })(n); } 或者: function fibonacci ...

  6. mysql gb2312与lanti1

    1.如果数据库编码为lanti1,页面编码utf-8和gb2312均可,并且不用set names,设置就会乱码: 2.如果数据库编码为utf8,页面编码utf-8和gb2312均可,一定要设置好se ...

  7. Java设计原则:面向接口的设计

    前言:在一个面向对象的系统中,系统的各种功能是由许许多多的不同对象协作完成的.在这种情况下,各个对象内部是如何实现对系统设计人员来说就不那么重要:而各个对象之间的协作关系则成为系统设计的关键.小到不同 ...

  8. Xcode6中添加pch全局引用文件

    前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6添加pch文件方法 1. 右键Supporting File,选择“New File” 2. 选择Other ...

  9. CSS2-3常见的demo列子总结

    CSS2-3常见的demo列子总结 阅读目录 1. css超过一行或者多行后显示省略号. 2. css图片未知高度垂直居中完美解决方案. 3. 学习使用 :before和 :after伪元素 回到顶部 ...

  10. 自定义继承于Page的基类

    自定义继承于Page的基类:MyBasePage[校验用户是否登录,如果登录则获取用户信息,否则跳转到登录页面]============================================ ...