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

/*
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. 配置 Apache+php多端口多站点(转载)

    配置httpd.conf监听多个端口 #增加监听端口 等以下内容都设置以后,可以通过 netstat -n -a查看端口是否开启 开启虚拟站点 # Virtual hosts#Include conf ...

  2. jgestures 一款jquery手势插件

    jgestures插件允许你如同原生的jQuery事件一样监听以下事件:'pinch'(缩放手势), 'rotate'(旋转手势), 'swipe'(滑动手势), 'tap'(轻触) 以及 'orie ...

  3. CentOS创建免密码SSH(密钥)

    1.输入以下命令:ssh-keygen -t rsa 2.输入命令ls:产生两个文件:id_rsa id_rsa.pub 3.复制id_rsa.pub,并命名为authorized_key cp ~/ ...

  4. dedecms升级后报错

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  5. shell脚本监控Flume输出到HDFS上文件合法性

    在使用flume中发现由于网络.HDFS等其它原因,使得经过Flume收集到HDFS上得日志有一些异常,表现为: 1.有未关闭的文件:以tmp(默认)结尾的文件.加入存到HDFS上得文件应该是gz压缩 ...

  6. cf.295.B Two Buttons (bfs)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. xcode6以后, 使用.pch

    http://blog.csdn.net/lihuiqwertyuiop/article/details/39268101 总结: . 创建.pch文件 . Apple LLVM 6.1 - Lang ...

  8. Awvs如何扫描需要登录的部分

    一个小技巧,可能部分的人习惯了按下一步.所以不大知道.给大家说说哈. 到LOGIN步骤的时候,在Login sequen 新建.然后你懂的了.会新出来一个浏览器,直接登录后一直下一步即可

  9. nyoj 8

    http://acm.nyist.net/JudgeOnline/problem.php?pid=8 #include<stdio.h> #include<iostream> ...

  10. snoopy 强大的PHP采集类使用实例代码

    下载地址: http://www.jb51.net/codes/33397.html Snoopy的一些特点: 1抓取网页的内容 fetch 2 抓取网页的文本内容 (去除HTML标签) fetcht ...