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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<vector>
  8. using namespace std;
  9. const int maxn = ;
  10. const int INF = 0x3f3f3f;
  11.  
  12. int pre[maxn],low[maxn],dfs_clock;
  13.  
  14. int bccnum[maxn],bcc_cnt; //记录每个点属于哪个边联通分量;
  15. bool isbridge[maxn*]; //isbridge[i],边i是不是桥;
  16. int n,m;
  17. int deg[maxn];
  18.  
  19. struct Edge{
  20. int u,v;
  21. int next;
  22. Edge(int u=,int v=,int next=): u(u),v(v),next(next) {}
  23. }edges[maxn*];
  24. int head[maxn],cnt ;
  25.  
  26. void addedge(int u,int v){
  27. edges[cnt] = Edge(u,v,head[u]);
  28. head[u] = cnt++;
  29. }
  30.  
  31. void init(){
  32. memset(head,-,sizeof(head));
  33. cnt = ;
  34. }
  35.  
  36. void tarjan(int u,int fa){
  37. pre[u] = low[u] = dfs_clock++;
  38. for(int i=head[u];i!=-;i=edges[i].next){
  39. int v = edges[i].v;
  40. if(v == fa) continue;
  41. if(!pre[v]){
  42. tarjan(v,u);
  43. low[u] = min(low[u],low[v]);
  44. if(low[v] > pre[u]) {isbridge[i] = true; isbridge[i^] = true;}
  45. }
  46. else
  47. low[u] = min(low[u],pre[v]);
  48. }
  49. }
  50. void dfs(int u){
  51. bccnum[u] = bcc_cnt;
  52. for(int i=head[u];i!=-;i=edges[i].next){
  53. int v = edges[i].v;
  54. if(bccnum[v] || isbridge[i]) continue;
  55. dfs(v);
  56. }
  57. }
  58.  
  59. void find_bcc(){
  60. bcc_cnt = ;
  61. memset(bccnum,,sizeof(bccnum));
  62. for(int i=;i<n;i++){
  63. if(!bccnum[i]){
  64. bcc_cnt++;
  65. dfs(i);
  66. }
  67. }
  68. }
  69.  
  70. void BuildnewG(){
  71. memset(deg,,sizeof(deg));
  72. /** for(int u=0;u<n;u++){
  73. for(int i=head[u];i!=-1;i=edges[i].next){
  74. int v = edges[i].v;
  75. if(bccnum[u] != bccnum[v]){
  76. deg[bccnum[u]]++;
  77. deg[bccnum[v]]++;
  78. }
  79. }
  80. } **/ //两种方法都可以;但下面这种要快些;
  81. for(int i=;i<cnt;i+=){
  82. if(isbridge[i]){
  83. deg[bccnum[edges[i].u]]++;
  84. deg[bccnum[edges[i].v]]++;
  85. }
  86. }
  87. }
  88.  
  89. int main()
  90. {
  91. // freopen("E:\\acm\\input.txt","r",stdin);
  92. int T;
  93. cin>>T;
  94. for(int t=;t<=T;t++){
  95. cin>>n>>m;
  96. init();
  97. for(int i=;i<=m;i++){
  98. int a,b;
  99. scanf("%d %d",&a,&b);
  100. addedge(a,b);
  101. addedge(b,a);
  102. }
  103. dfs_clock = ;
  104. memset(pre,,sizeof(pre));
  105. memset(isbridge,,sizeof(isbridge));
  106. tarjan(,-); //找桥;
  107.  
  108. find_bcc();
  109. BuildnewG();
  110. int ans = ;
  111. for(int i=;i<=bcc_cnt;i++)
  112. if(deg[i] == ) ans++; //如果/**...**/中找法,则deg[i] == 2,因为统计了两次。
  113. if(ans%) ans = ans/+;
  114. else ans = ans/;
  115. printf("Case %d: %d\n",t,ans);
  116. }
  117. }

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. eclipse-自动注释

    在eclipse中自动添加'注释'的快捷键是'Alt+Shift+J',可以在 MyEclipse中的 Java->Code Style->Code Template->Commen ...

  2. SQL SERVER 查看死锁的存储过程

    end

  3. html 5的localstorag

    随着我们硬件技术的发展,浏览器本身的功能也愈发的完善,从之前的cookie到现在的本地缓存机制,再到web storage,在之前html4 的时候使用cookie具有一些明显的局限,如大小限制,co ...

  4. throw 导致 Error C2220, wraning C4702错误

    今天在程序加了一个语句,发现报 Error C2220, Wraning C4702错误 查询Wraning C4702 ,[无法访问的代码] 由于为 Visual Studio .NET 2003 ...

  5. php定时执行任务的几个方法

    PHP的实现决定了它没有Java和.Net这种AppServer的概念, 而http协议是一个无状态的协议, php只能被用户触发, 被调用, 调用后会自动退出内存, 没有常驻内存, 就没有办法准确的 ...

  6. T-SQL语言基础

    1.T-SQL语言 CREATE:创建新对象,包括数据库.表.视图.过程.触发器和函数等常见数据库对象. ALTER:修改已有对象的结构. DROP:用来删除已有的对象.有些对象是无法删除的,因为它们 ...

  7. 用Python高亮org-mode代码块

    文章同时可在我的github blog上阅读:http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.h ...

  8. Jquery 全选、反选

    jQuery 1.9以后用 prop(); 不用attr 等 $(function() { $('#inputCheck').click(function() { $("input[name ...

  9. TatukGIS - GisDefs - CanonicalSQLName 函数

    函数名称  CanonicalSQLName 所在单元  GisDefs 函数原型  function CanonicalSQLName(const _name: String; const _tem ...

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