原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587

TWO NODES

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1448    Accepted Submission(s): 441

Problem Description
Suppose that G is an undirected graph, and the value of stab is defined as follows:

Among the expression,G-i, -j is the remainder after removing node i, node j and all edges that are directly relevant to the previous two nodes. cntCompent is the number of connected components of X independently.
Thus, given a certain undirected graph G, you are supposed to calculating the value of stab.
 
Input
The input will contain the description of several graphs. For each graph, the description consist of an integer N for the number of nodes, an integer M for the number of edges, and M pairs of integers for edges (3<=N,M<=5000).
Please note that the endpoints of edge is marked in the range of [0,N-1], and input cases ends with EOF.
 
Output
For each graph in the input, you should output the value of stab.
 
Sample Input
4 5
0 1
1 2
2 3
3 0
0 2
 
Sample Output
2
 
Source
 
Recommend
zhuyuanchen520

题意

给你个图,问你去掉两个点之后能有最多多少连通块。

题解

先枚举其中一个点,然后在剩下的点中求割点,Tarjan的时候统计一下每个割点分割几个连通块,取个最大的割点,然后再dfs一次求连通块个数。

代码

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<vector>
  4. #include<cstring>
  5. #include<algorithm>
  6. #define MAX_N 5555
  7. using namespace std;
  8.  
  9. vector<int> G[MAX_N];
  10. bool vis[MAX_N];
  11. int dfn[MAX_N],low[MAX_N],ind=;
  12.  
  13. int cut[MAX_N];
  14.  
  15. int node;
  16.  
  17. void Tarjan(int u,int p){
  18. int child=;
  19. dfn[u]=low[u]=++ind;
  20. vis[u]=;
  21. for(int i=;i<G[u].size();i++){
  22. int v=G[u][i];
  23. if(v==p||v==node)continue;
  24. if(!vis[v]){
  25. Tarjan(v,u);
  26. low[u]=min(low[v],low[u]);
  27. child++;
  28. if((p==-&&child>)||(p!=-&&low[v]>=dfn[u]))
  29. cut[u]++;
  30. }
  31. else
  32. low[u]=min(dfn[v],low[u]);
  33. }
  34. }
  35.  
  36. int n,m;
  37.  
  38. void init(){
  39. for(int i=;i<=n;i++)G[i].clear();
  40. ind=;
  41. memset(vis,,sizeof(vis));
  42. memset(cut,,sizeof(cut));
  43. }
  44.  
  45. bool used[MAX_N];
  46. int cu;
  47. void dfs(int u,int p){
  48. if(u==p||used[u]||u==node||u==cu)return;
  49. used[u]=;
  50. for(int i=;i<G[u].size();i++)dfs(G[u][i],u);
  51. }
  52.  
  53. int main(){
  54. while(scanf("%d%d",&n,&m)==){
  55. int stab=;
  56. init();
  57. int u,v;
  58. for(int i=;i<m;i++) {
  59. scanf("%d%d", &u, &v);
  60. G[u].push_back(v);
  61. G[v].push_back(u);
  62. }
  63. for(int i=;i<n;i++){
  64. node=i;
  65. memset(vis,,sizeof(vis));
  66. ind=;
  67. memset(cut,,sizeof(cut));
  68. for(int j=;j<n;j++)
  69. if((!vis[j])&&j!=node)
  70. Tarjan(j,-);
  71. int maxC=;
  72. for(int j=;j<n;j++)
  73. if(j!=node&&cut[j]>=maxC){
  74. cu=j;
  75. maxC=cut[j];
  76. }
  77. int ans=;
  78. memset(used,,sizeof(used));
  79. for(int j=;j<n;j++)
  80. if((!used[j])&&j!=node&&j!=cu){
  81. dfs(j,-);
  82. ans++;
  83. }
  84. stab=max(stab,ans);
  85. }
  86. printf("%d\n",stab);
  87. }
  88.  
  89. return ;
  90. }

HDU 4587 TWO NODES 枚举+割点的更多相关文章

  1. HDU 4587 TWO NODES(割点)(2013 ACM-ICPC南京赛区全国邀请赛)

    Description Suppose that G is an undirected graph, and the value of stab is defined as follows: Amon ...

  2. HDU 4587 TWO NODES 割点

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 删除两个点,使连通块的数目最大化 题解: 枚举删除第一个点,然后对删除了第一个点的图跑 ...

  3. HDU - 4587 TWO NODES (图的割点)

    Suppose that G is an undirected graph, and the value of stab is defined as follows: Among the expres ...

  4. HDU 4587 TWO NODES(割两个点的最大连通分支数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 给一图,求割去两个点后所能形成的最大连通分支数. 思路: 对于这种情况,第一个只能枚举,然后在删除 ...

  5. hdu 4587 推断孤立点+割点+ 删除点之后,剩下多少连通分量

    做了非常久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点.没有割点当然也有答案 学到 ...

  6. hdu 4587(割点的应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 思路:题目的意思很简单,就是删除任意2个节点以及关联的边,求图的最大连通分量数.我们知道删除割点 ...

  7. HDU 4587 B - TWO NODES tarjan

    B - TWO NODESTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  8. hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

    题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...

  9. hdu 4587(枚举+割顶)

    TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

随机推荐

  1. Python学习笔记:configparser(INI格式配置文件解析)

    在平时的开发中感觉INI格式的配置文件使用还是挺需要的,有时会使用一个单独的py来存放一些常量或者配置项,大多时候这样倒是挺好用的,但是如果某些配置项需要在运行时由用户来修改指定,比如很多app在关闭 ...

  2. Python9-事件及队列-day37

    信号量 from multiprocessing import Process from multiprocessing import Semaphore import time import ran ...

  3. stm32之PWM学习

    下图是一个STM32普通PWM形成的图形原理说明 自动重装载寄存器(ARR)用于确定波形的频率(即周期).捕获比较寄存器(CCRx)(用于确定占空比的) PWM的工作过程如下:首先ARR寄存器里面的值 ...

  4. Nastya Studies Informatics CodeForces - 992B (大整数)

    B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...

  5. iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结

    我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...

  6. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...

  7. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  8. 天气API接口的使用

    最近项目中使用到了天气预报的功能,需要从网上获取天气数据,然后显示在公司系统的页面上. 在这里和大家分享下我的做法,希望能对大家有所帮助,如果有错误,欢迎大家指正. 先给大家看看效果: 下面开始进行讲 ...

  9. 电商平台API接口

  10. 对python的想法

    作为计算机专业的学生,在编程语言之余,我认为掌握一门脚本语言是很必要的.尤其是现在在数据分析,AI,机器学习等各个方面都大放异彩的python.相比于之前接触过的Java,C,C++乃至于php等语言 ...