题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34902

思路:首先是双连通缩点,然后就是搜索一下,搜索时要跳过连通分量的点的个数>=2的点,最后的答案是n*(n-1)/2.

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stack>
  6. #include<vector>
  7. using namespace std;
  8. #define MAXN 11111
  9. #define MAXM 444444
  10.  
  11. struct Edge{
  12. int v,next;
  13. }edge[MAXM];
  14.  
  15. int n,m,NE,cnt,_count;
  16. int head[MAXN];
  17.  
  18. void Insert(int u,int v)
  19. {
  20. edge[NE].v=v;
  21. edge[NE].next=head[u];
  22. head[u]=NE++;
  23. }
  24.  
  25. int low[MAXN],dfn[MAXN];
  26. int color[MAXN];
  27. bool mark[MAXN];
  28. stack<int>S;
  29. void Tarjan(int u,int father)
  30. {
  31. int flag=;
  32. low[u]=dfn[u]=++cnt;
  33. mark[u]=true;
  34. S.push(u);
  35. for(int i=head[u];i!=-;i=edge[i].next){
  36. int v=edge[i].v;
  37. if(v==father&&!flag){ flag=;continue; }
  38. if(dfn[v]==){
  39. Tarjan(v,u);
  40. low[u]=min(low[u],low[v]);
  41. }else if(mark[v]){
  42. low[u]=min(low[u],dfn[v]);
  43. }
  44. }
  45. if(low[u]==dfn[u]){
  46. _count++;
  47. int x=S.top();
  48. if(x==u)S.pop();
  49. else {
  50. do{
  51. x=S.top();
  52. S.pop();
  53. mark[x]=false;
  54. color[x]=_count;
  55. }while(x!=u);
  56. }
  57. }
  58. }
  59.  
  60. int ans;
  61. void dfs(int u,int father)
  62. {
  63. color[u]=;
  64. _count++;
  65. for(int i=head[u];i!=-;i=edge[i].next){
  66. int v=edge[i].v;
  67. if(v==father)continue;
  68. if(color[v])continue;
  69. dfs(v,u);
  70. }
  71. }
  72.  
  73. int main()
  74. {
  75. int _case,u,v,t=;
  76. scanf("%d",&_case);
  77. while(_case--){
  78. scanf("%d%d",&n,&m);
  79. NE=;
  80. memset(head,-,sizeof(head));
  81. while(m--){
  82. scanf("%d%d",&u,&v);
  83. Insert(u,v);
  84. Insert(v,u);
  85. }
  86. cnt=_count=;
  87. memset(dfn,,sizeof(dfn));
  88. memset(color,,sizeof(color));
  89. for(int i=;i<=n;i++){
  90. if(dfn[i]==)Tarjan(i,-);
  91. }
  92. ans=;
  93. for(int i=;i<=n;i++){
  94. if(color[i]==){
  95. _count=;
  96. dfs(i,-);
  97. ans+=_count*(_count-)/;
  98. }
  99. }
  100. printf("Case #%d: %d\n",t++,ans);
  101. }
  102. return ;
  103. }

UVALive 6044(双连通分量的应用)的更多相关文章

  1. UVALive - 5135 - Mining Your Own Business(双连通分量+思维)

    Problem   UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...

  2. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  3. UVALive 5135 Mining Your Own Business 双连通分量 2011final

    题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太平井逃脱, ...

  4. UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)

    由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...

  5. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  6. 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分

    E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...

  7. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 751[Submit][Statu ...

  8. hihoCoder 1184 连通性二·边的双连通分量

    #1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...

  9. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

随机推荐

  1. Native App、Web App 还是Hybrid App

    Native App.Web App 还是Hybrid App? 技术 标点符 1年前 (2014-05-09) 3036℃ 0评论 一.什么是Native App? Native App即原生应用, ...

  2. [精]Oracle APEX 5.0 入门教程(一) Form表单

    Oracle APEX Tutorial for Beginners (APEX 5.0) 1- Introduction 2- Create Workspace 3- Work with Works ...

  3. css background-position结合disaply:inline-block使用

    $(".icon-a").on('click', function (e) { if ($(this).next().css('display') == "none&qu ...

  4. MySQL中group_concat函数

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) .MySQL中group_concat函数完整的语法如下:group_c ...

  5. ERROR org.apache.zookeeper.ClientCnxn:532 - Error while calling watcher

    一.背景 使用zookeeper操作时提示这个错误信息 ERROR org.apache.zookeeper.ClientCnxn: - Error while calling watcher jav ...

  6. 如何利用dex2jar反编译APK

    工具/原料 电脑 dex2jar JD-GUI 方法/步骤 1 下载dex2jar和JD-GUI,在参考资料中添加了这两个工具的百度网盘下载地址供读者下载使用(笔者亲测) 2 找到我们准备测试用的ap ...

  7. 每日英语:Yahoo's Rally: Made in China

    The typical honeymoon doesn't last too long before the hard work of marriage begins. And so it norma ...

  8. django model 多对多保存

  9. Centos7+PHP5.6+MySQL5.7+Zabbix4.0部署

    Centos7+PHP5.6+MySQL5.7+Zabbix4.0部署 系统版本:CentOS Linux release 7.4.1708 (Core) 最小化安装 内核版本:3.10.0-693. ...

  10. Android——Android Bundle类(转)

    今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下. 根据google官方的文档(http://developer.android.com/reference/android/os/Bun ...