题目链接:http://lightoj.com/volume_showproblem.php?problem=1291

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f; int pre[maxn],low[maxn],dfs_clock; int bccnum[maxn],bcc_cnt; //记录每个点属于哪个边联通分量;
bool isbridge[maxn*]; //isbridge[i],边i是不是桥;
int n,m;
int deg[maxn]; struct Edge{
int u,v;
int next;
Edge(int u=,int v=,int next=): u(u),v(v),next(next) {}
}edges[maxn*];
int head[maxn],cnt ; void addedge(int u,int v){
edges[cnt] = Edge(u,v,head[u]);
head[u] = cnt++;
} void init(){
memset(head,-,sizeof(head));
cnt = ;
} void tarjan(int u,int fa){
pre[u] = low[u] = dfs_clock++;
for(int i=head[u];i!=-;i=edges[i].next){
int v = edges[i].v;
if(v == fa) continue;
if(!pre[v]){
tarjan(v,u);
low[u] = min(low[u],low[v]);
if(low[v] > pre[u]) {isbridge[i] = true; isbridge[i^] = true;}
}
else
low[u] = min(low[u],pre[v]);
}
}
void dfs(int u){
bccnum[u] = bcc_cnt;
for(int i=head[u];i!=-;i=edges[i].next){
int v = edges[i].v;
if(bccnum[v] || isbridge[i]) continue;
dfs(v);
}
} void find_bcc(){
bcc_cnt = ;
memset(bccnum,,sizeof(bccnum));
for(int i=;i<n;i++){
if(!bccnum[i]){
bcc_cnt++;
dfs(i);
}
}
} void BuildnewG(){
memset(deg,,sizeof(deg));
/** for(int u=0;u<n;u++){
for(int i=head[u];i!=-1;i=edges[i].next){
int v = edges[i].v;
if(bccnum[u] != bccnum[v]){
deg[bccnum[u]]++;
deg[bccnum[v]]++;
}
}
} **/ //两种方法都可以;但下面这种要快些;
for(int i=;i<cnt;i+=){
if(isbridge[i]){
deg[bccnum[edges[i].u]]++;
deg[bccnum[edges[i].v]]++;
}
}
} int main()
{
// freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int t=;t<=T;t++){
cin>>n>>m;
init();
for(int i=;i<=m;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs_clock = ;
memset(pre,,sizeof(pre));
memset(isbridge,,sizeof(isbridge));
tarjan(,-); //找桥; find_bcc();
BuildnewG();
int ans = ;
for(int i=;i<=bcc_cnt;i++)
if(deg[i] == ) ans++; //如果/**...**/中找法,则deg[i] == 2,因为统计了两次。
if(ans%) ans = ans/+;
else ans = ans/;
printf("Case %d: %d\n",t,ans);
}
}

lightoj 1291 无向图边双联通+缩点统计叶节点的更多相关文章

  1. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

  2. poj 3694双联通缩点+LCA

    题意:给你一个无向连通图,每次加一条边后,问图中桥的数目. 思路:先将图进行双联通缩点,则缩点后图的边就是桥,然后dfs记录节点深度,给出(u,v)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...

  3. hdu 4612 双联通缩点+树形dp

    #pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...

  4. 『Tarjan算法 无向图的双联通分量』

    无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...

  5. POJ 3177 Redundant Paths 无向图边双联通基础题

    题意: 给一个无向图,保证任意两个点之间有两条完全不相同的路径 求至少加多少边才能实现 题解: 得先学会一波tarjan无向图 桥的定义是:删除这条边之后该图不联通 一条无向边(u,v)是桥,当且仅当 ...

  6. Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std ...

  7. 无向图边双联通分量 tarjan 模板

    #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXM = 500005 ...

  8. 边的双联通+缩点+LCA(HDU3686)

    Traffic Real Time Query System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. BZOJ1123 [POI2008]BLO(割点判断 + 点双联通缩点size)

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...

随机推荐

  1. C# 如何创建接口以及使用接口的简单Demo(转载!)

    //No:1  首先,我们要封装一个接口,接口中不要实现具体的方法(说白了这就是一个架子而已!) using System;using System.Collections.Generic;using ...

  2. C# 数字证书微信API调用使用参考事例

    X.509 v.3 证书的方法.一个比较完整的调用  微信  API的示例: private stringGetResponseResult()         { string strRespons ...

  3. office2010怎么激活

    软件都是不断更新换代的,像我们使用最多的Microsoft Office软件,从最初的98,2000,2003,2007,到现在的2010.但是在最初安装Office软件时,都是未激活的.下面介绍的就 ...

  4. Asp.Net 注册 邮箱激活

    数据库 表的设计 State为用户状态  0为禁用  1为可用  默认为0,下面有个UserGUID,这个字段将来用于激活账户 首先你要写一个表单,验证码神马的,这个我就不写了..直接写处理的 代码在 ...

  5. WPF 自定义滚动条样式

    先看一下效果: 先分析一下滚动条有哪儿几部分组成: 滚动条总共有五部分组成: 两端的箭头按钮,实际类型为RepeatButton Thumb 两端的空白,实际也是RepeatButton 最后就是Th ...

  6. Java设计模式(学习整理)----装饰模式

    1.概念: (在我看来,模式就像是是一种思想,在这种思想的指引下,对代码和结构的一番加工和整合而已!都是套路!) 装饰模式又称包装(Wrapper)模式,是以对客户端透明的方式扩展对象的功能,是继承关 ...

  7. 对数据预处理的一点理解[ZZ]

    数据预处理没有统一的标准,只能说是根据不同类型的分析数据和业务需求,在对数据特性做了充分的理解之后,再选择相关的数据预处理技术,一般会用到多种预处理技术,而且对每种处理之后的效果做些分析对比,这里面经 ...

  8. mahout的安装、配置及运行java程序

    一.下载安装包: http://mahout.apache.org/general/downloads.html 二.解压: 将下载的安装包解压到需要的目录下 三.配置环境变量: export MAH ...

  9. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

  10. SGU 138.Games of Chess

    时间限制:0.25s 空间限制:4M 题目: n个朋友在一起按照下面的规则依次下棋:在第一局游戏,n个人中的两个开始下棋.在第二局,第一局胜利的人将跟其他人下棋(也可能还是输了第一局人), 在第三局第 ...