lightoj 1291 无向图边双联通+缩点统计叶节点
题目链接: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 无向图边双联通+缩点统计叶节点的更多相关文章
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- poj 3694双联通缩点+LCA
题意:给你一个无向连通图,每次加一条边后,问图中桥的数目. 思路:先将图进行双联通缩点,则缩点后图的边就是桥,然后dfs记录节点深度,给出(u,v)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...
- hdu 4612 双联通缩点+树形dp
#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...
- 『Tarjan算法 无向图的双联通分量』
无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...
- POJ 3177 Redundant Paths 无向图边双联通基础题
题意: 给一个无向图,保证任意两个点之间有两条完全不相同的路径 求至少加多少边才能实现 题解: 得先学会一波tarjan无向图 桥的定义是:删除这条边之后该图不联通 一条无向边(u,v)是桥,当且仅当 ...
- Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std ...
- 无向图边双联通分量 tarjan 模板
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXM = 500005 ...
- 边的双联通+缩点+LCA(HDU3686)
Traffic Real Time Query System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- BZOJ1123 [POI2008]BLO(割点判断 + 点双联通缩点size)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...
随机推荐
- eclipse-自动注释
在eclipse中自动添加'注释'的快捷键是'Alt+Shift+J',可以在 MyEclipse中的 Java->Code Style->Code Template->Commen ...
- SQL SERVER 查看死锁的存储过程
end
- html 5的localstorag
随着我们硬件技术的发展,浏览器本身的功能也愈发的完善,从之前的cookie到现在的本地缓存机制,再到web storage,在之前html4 的时候使用cookie具有一些明显的局限,如大小限制,co ...
- throw 导致 Error C2220, wraning C4702错误
今天在程序加了一个语句,发现报 Error C2220, Wraning C4702错误 查询Wraning C4702 ,[无法访问的代码] 由于为 Visual Studio .NET 2003 ...
- php定时执行任务的几个方法
PHP的实现决定了它没有Java和.Net这种AppServer的概念, 而http协议是一个无状态的协议, php只能被用户触发, 被调用, 调用后会自动退出内存, 没有常驻内存, 就没有办法准确的 ...
- T-SQL语言基础
1.T-SQL语言 CREATE:创建新对象,包括数据库.表.视图.过程.触发器和函数等常见数据库对象. ALTER:修改已有对象的结构. DROP:用来删除已有的对象.有些对象是无法删除的,因为它们 ...
- 用Python高亮org-mode代码块
文章同时可在我的github blog上阅读:http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.h ...
- Jquery 全选、反选
jQuery 1.9以后用 prop(); 不用attr 等 $(function() { $('#inputCheck').click(function() { $("input[name ...
- TatukGIS - GisDefs - CanonicalSQLName 函数
函数名称 CanonicalSQLName 所在单元 GisDefs 函数原型 function CanonicalSQLName(const _name: String; const _tem ...
- Library:python-memcached on Windows
1 install memcached 1.4.4 Windows 32-bit 2 cd into the base file and type memcached.exe -d install ...