原题链接: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一次求连通块个数。

代码

#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#define MAX_N 5555
using namespace std; vector<int> G[MAX_N];
bool vis[MAX_N];
int dfn[MAX_N],low[MAX_N],ind=; int cut[MAX_N]; int node; void Tarjan(int u,int p){
int child=;
dfn[u]=low[u]=++ind;
vis[u]=;
for(int i=;i<G[u].size();i++){
int v=G[u][i];
if(v==p||v==node)continue;
if(!vis[v]){
Tarjan(v,u);
low[u]=min(low[v],low[u]);
child++;
if((p==-&&child>)||(p!=-&&low[v]>=dfn[u]))
cut[u]++;
}
else
low[u]=min(dfn[v],low[u]);
}
} int n,m; void init(){
for(int i=;i<=n;i++)G[i].clear();
ind=;
memset(vis,,sizeof(vis));
memset(cut,,sizeof(cut));
} bool used[MAX_N];
int cu;
void dfs(int u,int p){
if(u==p||used[u]||u==node||u==cu)return;
used[u]=;
for(int i=;i<G[u].size();i++)dfs(G[u][i],u);
} int main(){
while(scanf("%d%d",&n,&m)==){
int stab=;
init();
int u,v;
for(int i=;i<m;i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i=;i<n;i++){
node=i;
memset(vis,,sizeof(vis));
ind=;
memset(cut,,sizeof(cut));
for(int j=;j<n;j++)
if((!vis[j])&&j!=node)
Tarjan(j,-);
int maxC=;
for(int j=;j<n;j++)
if(j!=node&&cut[j]>=maxC){
cu=j;
maxC=cut[j];
}
int ans=;
memset(used,,sizeof(used));
for(int j=;j<n;j++)
if((!used[j])&&j!=node&&j!=cu){
dfs(j,-);
ans++;
}
stab=max(stab,ans);
}
printf("%d\n",stab);
} return ;
}

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. LeetCode(292) Nim Game

    题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...

  2. Cacti安装脚本Server端+客户端

    #!/bin/bash #auto make install LAMP+Cacti #by authors zhang #RRDtool define path variable R_FILES=rr ...

  3. py文件转exe时包含paramiko模块出错解决方法

    问题描述:python代码中包含paramiko模块的远程登录ssh,在用pyInstaller转为exe时报错, 报错提示为“No handlers could be found for logge ...

  4. 异常 ndroid.view.InflateException: Binary XML file line #8: Error inflating class com.ouyang.test.MyView

    发现自定义view时出现ndroid.view.InflateException: Binary XML file line #8: Error inflating class com.ouyang. ...

  5. body标签相关

    03-body标签中相关标签   今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一 ...

  6. 带有命名空间的xml解析,C#

    前一段时间做花旗的接口,返回的xml格式是带有命名空间的,可是难倒了我,找了好久才找到解决办法,给大家分享下,少走弯路. 1,直接进入正题,先看一段带有命名空间的xml,这段xml大概的意思是,前面是 ...

  7. Policy-Based Reinforcement Learning

    Policy-based Approach policy-based 强化学习通常是要学习一个actor, actor可以用\(\pi_\theta (S)\) 来确定.如果我们用actor来玩游戏, ...

  8. 【转】[译]深入理解JVM

    http://www.cnblogs.com/enjiex/p/5079338.html 深入理解JVM 原文链接:http://www.cubrid.org/blog/dev-platform/un ...

  9. 【Luogu】P2486染色(树链剖分)

    题目链接 线段树维护左端颜色,右端颜色,颜色段数量. 合并的时候看左子树的右端颜色是不是等于右子树的左端颜色,如果等于那么颜色段数量要-1S 然后在树剖跳链的时候搞同样的操作qwq 然后就没有然后了 ...

  10. 关于 lambda expression 返回值的类型转换

    lambda expression(lambda 表达式,$\lambda$ 表达式) 是 C++ 11 引入的特性. 一般而言,lambda 表达式的返回值类型可不指定,而由返回值推断. 需要注意的 ...