问一个无向图中去掉任意两点后剩下的连通分量的个数最大值

枚举第一个删去的点,在剩下的子图中求割点

注意,剩下的子图可能不连通,那么就要对每个连通块求割点

计算删去一个点后剩余连通分量个数 left 的方法为:tarjan算法中的时间戳数组dfn[]若为0说明是新的连通分量

求删去割点后剩余连通分量个数:

tarjan算法中将判断是否为割点的bool 数组改为int类型,并将iscut[i] = 1 改为 iscut[i]++ 即可

那么对于非根节点,删去后剩余个数为iscut[i] + 1(子树个数加上父节点),根节点为iscut[i] (没有父节点)

那么全题答案便是 max(iscut[i] + 1) + left - 1

细节见代码

#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
//LOOP
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//OTHER
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1000000000;
const int MAXN = 5050;
const int MOD = 1000000; vector<int> G[MAXN];
int dfn[MAXN], low[MAXN], iscut[MAXN]; //时间戳数组,所能访问的最早祖先,删去此点后所能得到的连通分量个数
int dfs_c, ans;
int n, m, none; void add(int u, int v)
{
G[u].push_back(v);
G[v].push_back(u);
} int tarjan(int u, int fa)
{
bool f = false; ///判断重边用
int lowu = dfn[u] = ++dfs_c;
int child = 0, sz = G[u].size();
REP(i, sz)
{
int v = G[u][i];
if (v == none) continue; ///若为枚举的第一个删除的节点,忽略
if (v == fa && !f)
{
f = 1;
continue;
}
if (!dfn[v])
{
int lowv = tarjan(v, u);
lowu = min(lowu, lowv);
if (lowv >= dfn[u])
iscut[u]++;
}
else
lowu = min(lowu, dfn[v]);
}
if (fa < 0 && child == 1) ///若为此连通分量的根节点且只有一个子树,那么删去后连通分量为 1
iscut[u] = 1;
low[u] = lowu;
return lowu;
} void init()
{
REP(i, n + 1)
G[i].clear();
ans = 0;
} int solve(int x)
{
int ret = 0;
none = x;
CLR(dfn, 0), CLR(low, 0);
CLR(iscut, 0);
dfs_c = 0;
int left = 0;
REP(i, n)
if (i != x && !dfn[i])
iscut[i]--, left++, tarjan(i, -1); ///dfn为0 说明是根节点,先-1,后面统计时便全是iscut + 1
REP(i, n)
if (i != x)
ret = max(ret, iscut[i] + 1);
ret += left - 1; ///剩下连通分量加上最大iscut值
return ret;
} int main()
{
int x, y;
while (~RII(n, m))
{
init();
REP(i, m)
{
RII(x, y);
add(x, y);
}
REP(i, n)
ans = max(ans, solve(i));
printf("%d\n", ans);
}
}
/* 5 5
0 3
3 4
3 2
1 2
2 4
*/

hdu4587 TWO NODES的更多相关文章

  1. hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...

  2. 备战noip week8

    POJ1144 网络 description: 给出一张\(N\)个点的无向图,求其中割点的个数 data range: \(N\le 100\) solution: 一道模板题(但是读入实在是把我恶 ...

  3. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  4. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  5. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  6. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  7. Leetcode-24 Swap Nodes in Pairs

    #24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  8. No.025:Reverse Nodes in k-Group

    问题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  9. No.024:Swap Nodes in Pairs

    问题: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

随机推荐

  1. Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))

    C. Postcard time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  3. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. CodeForces 738A Interview with Oleg

    模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  5. webpack配置点记录

    jsx语法需要用到babel-preset-react: 通用babel-loader相关如下: 以上配置还只能针对ES6情况,static defaultProps是ES7的特性,需要引入新的配置:

  6. 【bzoj1594】猜数游戏

    1594: [Usaco2008 Jan]猜数游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 556  Solved: 225 Descripti ...

  7. 用flask开发个人博客(26)—— 利用config.py配置文件动态的创建不同的Flask对象

    原文:https://blog.csdn.net/hyman_c/article/details/52877704 对配置进行封装的目的是根据不同的使用场景,给flask的app赋予不同的config ...

  8. python学习第九十天:vue补习2

    Vue 八.重要指令 v-bind <!-- 值a --> <div v-bind:class='"a"'></div> <!-- 变量a ...

  9. Problem Z: 百鸡问题

    #include <stdio.h> int main() { int i, j, k; ; i <= ; i++ ) ; j <= ; j++ ) ; k <= ; k ...

  10. 修改ORACLE实例名

    修改数据库的SID  举例说明,我的数据库的SID叫testdb,现在要改成oral.更改ORACLE数据库的sid,涉及到的用东西比较多,但是大概来说就以下六步. 1.停止所有的Oracle服务.  ...