题目链接:传送门

题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图

题目思路:tarjan算法+缩点(如果已经是双连通图就直接输出0)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
#define Min(x,y) (x<y?x:y)
#define Max(x,y) (x>y?x:y)
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 11005
#define maxn 5005
typedef long long LL;
typedef pair<int,int> PII; stack<int>sk;
int n,m,hcnt,deep,block,leaf;
int d[maxn],dfn[maxn],low[maxn],head[maxn];
int color[maxn],vis[maxn];
struct Node{
int to,next;
Node(){}
Node(int a,int b):to(a),next(b){}
}node[N<<]; inline void add(int x,int y){
node[hcnt]=Node(y,head[x]);
head[x]=hcnt++;
} inline void init(){
while(!sk.empty())sk.pop();
mst(vis,);
mst(head,-);
mst(d,);
mst(dfn,);
hcnt=deep=;
block=leaf=;
} void dfs(int x,int fa){
int flag=; ///这个不是必要的,有重边时有效
sk.push(x);
vis[x]=;
low[x]=dfn[x]=++deep;
for(int i=head[x];~i;i=node[i].next){
int e=node[i].to;
if(e==fa&&flag){flag=;continue;}
if(vis[e])low[x]=min(low[x],dfn[e]);
else if(!dfn[e]){
dfs(e,x);
low[x]=min(low[x],low[e]);
}
}
if(low[x]==dfn[x]){
++block; ///缩点
int co;
do{
co=sk.top();sk.pop();
vis[co]=;
color[co]=block;
}while(co!=x);
}
} int main(){
int i,j,group,Case=,x,y;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(i=;i<m;++i){
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(i=;i<=n;++i){
if(!dfn[i])dfs(i,-);
}
if(block==){printf("0\n");continue;}
for(i=;i<=n;++i)
for(j=head[i];~j;j=node[j].next){
int e=node[j].to;
if(color[i]!=color[e])
++d[color[i]]; ///d数组保留的是缩点后每个点的度数
}
for(i=;i<=block;++i){
if(d[i]==)leaf+=; ///如果有孤立的点,叶子节点加2
else if(d[i]==)++leaf;
}
printf("%d\n",(leaf+)/);
}
return ;
}

最新文章

  1. sublime text常用快捷键
  2. [原创]Matlab生成随机数
  3. KO+bootstrap 模态窗全选绑定
  4. Android事件分发机制(二)30分钟弄明白Touch事件分发机制
  5. Git之分支创建策略
  6. java基础之 泛型
  7. 用户自定义结构数据与VARIANT转换 .
  8. [Introduction to programming in Java 笔记] 1.3.8 Gambler&#39;s ruin simulation 赌徒破产模拟
  9. [MCM]2014年美赛MCM题目原文及翻译
  10. bzoj1146
  11. 触发器内insert,delete,update判断执行不同的内容
  12. HDU 1090 A+B for Input-Output Practice (II)
  13. Java NIO的探究
  14. JVM线程安全
  15. input框内的单引号,双引号转译
  16. Ubuntu系统下解决“YourUserName不在sudoers文件中。此事将被报告”的问题
  17. 解决js的 Math取正弦值 余弦值不准确的问题
  18. WordPress主题开发:按分类调用文章
  19. SpringBoot2.0小程序支付功能实现weixin-java-pay
  20. 前端学习 -- Html&amp;Css -- 框架集

热门文章

  1. Unity多媒体展示项目经验分享-ImageEffect+动态绑定
  2. C++——动态分配内存问题
  3. Redis 3.2.8 集群模式+Sentinel多Master部署
  4. loadrunner使用sitescope监测监控mysql数据库
  5. redislive
  6. PLSQL中scott账户登录不上,报错ORA-01017: invalid username/password; logon denied
  7. mongoDB id 导出,dump,sed,count,mysql import等用法示例
  8. 网易2016年研发project师编程题(2)
  9. 基于Linux的智能家居的设计(5)
  10. Git使用笔记三