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

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

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

计算删去一个点后剩余连通分量个数 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. 【原创】IIS7.5优化,支持同时10万个请求

    背景 IIS7.5是微软推出的最新平台IIS,性能也较以前有很大的提升,但是默认的设置配不适合很大的请求.但是我们可以根据实际的需要进行IIS调整,使其性能更佳,支持同时10万个请求. 以下方案,通过 ...

  2. Vue 2.0 生命周期-钩子函数理解

    Vue 2.0 + 生命周期钩子在项目过程中经常用到,所以闲下来整理了下,直接复制下面的实例运行: <!DOCTYPE html> <html lang="en" ...

  3. Laravel通过Swoole提升性能

    1.安装配置laravel 1.1.composer下载laravel composer create-project --prefer-dist laravel/laravel blog " ...

  4. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

    简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...

  5. python 字符集转换-灰常慢

    代码 def toUni (text): str = text try: charstyle = chardet.detect(text) # print 'confidence: ', charst ...

  6. 【转载】文件下载FileDownloader

    原文地址:https://github.com/lingochamp/FileDownloader 特点 简单易用 高并发 灵活 可选择性支持: 独立/非独立进程 自动断点续传 需要注意 当下载的文件 ...

  7. ECNU 2018 10月月赛 E 盖房子 (bitset + 倍增)

    题目链接  ECNU Monthly 2018.10 Problem E 从开场写到结束…… 显然要把三角形分成上下两部分. 把每一部分分成三部分,以上部分为例. 上面和右边,以及左下角的正方形. 也 ...

  8. 木材加工(LintCode)

    木材加工 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 样例 有3根木头[232 ...

  9. 第2天:Ansible-Inventory管理

    在Ansible中,将可管理的服务器集合成为Inventory.因此,Inventory管理便是服务器管理. hosts文件位置 我们知道,Ansible在执行操作时,首先需要确定对哪些服务器执行操作 ...

  10. 【POJ 2186】Popular Cows

    http://poj.org/problem?id=2186 tarjan求强连通分量. 因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了. 写手动栈时思路清晰一点应该是不会 ...