hdu4587 TWO NODES
问一个无向图中去掉任意两点后剩下的连通分量的个数最大值
枚举第一个删去的点,在剩下的子图中求割点
注意,剩下的子图可能不连通,那么就要对每个连通块求割点
计算删去一个点后剩余连通分量个数 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的更多相关文章
- hdu4587 Two Nodes 求图中删除两个结点剩余的连通分量的数量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题目给了12000ms,对于tarjan这种O(|V|+|E|)复杂度的算法来说,暴力是能狗住的 ...
- 备战noip week8
POJ1144 网络 description: 给出一张\(N\)个点的无向图,求其中割点的个数 data range: \(N\le 100\) solution: 一道模板题(但是读入实在是把我恶 ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [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 ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 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 ...
- 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 ...
- No.024:Swap Nodes in Pairs
问题: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
随机推荐
- 五十三 网络编程 TCP/IP简介
虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...
- vue-music 关于playlist (底部播放列表组件)
建立playlist.vue 组件,在player.vue 组件中引用,点击迷你播放器的播放列表按钮由下至上弹出这个层,所以在player.vue 播放器组件中引用 在playlist.vue 组件中 ...
- tp3.23 nginx lnmp填坑
thinkphp3.23在apache上可以轻松实现4个路由模式 但是在nginx上就出现问题 我们的环境是用lnmp包实现(地址:https://lnmp.org/) 安装完成后,ta的lnmp的n ...
- 洛谷P1129 [ZJOI2007] 矩阵游戏
题目传送门 分析:看到这题呢,首先想到的就是搜索,数据范围也不大嘛.但是仔细思考发现这题用搜索很难做,看了大佬们的题解后学到了,这一类题目要用二分图匹配来做.可以知道,如果想要的话,每一个子都可以移动 ...
- Linux系统的目录结构及各目录作用
使用tree命令查看Linux目录结构,这个命令默认是没有安装的,需要手动安装一下. [root@xuexi xf]# mount /dev/sr0 /media/ mount: /dev/sr0 写 ...
- HashMap 不能并发
问题的症状 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多线程的,于是,变成多线程后到了线上,发现 ...
- Arduino可穿戴开发入门教程LilyPad和LilyPad Simple的介绍
Arduino可穿戴开发入门教程LilyPad和LilyPad Simple的介绍 LilyPad和LilyPad Simple的介绍 LilyPad和LilyPad Simple是LilyPad微控 ...
- 【拓展Lucas】模板
求\(C_n^m \mod p\),写得太丑了qwq. 第一次写拓展Lucas竟然是在胡策的时候qwq写了两个半小时啊_(:з」∠)还写挂了一个地方qwq 当然今天胡策我也是第一次写中国剩余定理(ˇˍ ...
- 【并查集】【DFS】搭桥
[codevs1002]搭桥 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着 ...
- nginx和php-fpm的用户权限
启动php-fpm sudo php-fpm -c /etc/php.ini [17-Sep-2018 00:36:59] ERROR: [pool www] please specify user ...