hdu4587TWO NODES【割点】
来源:互联网 发布:编码器编程程序接线图 编辑:程序博客网 时间:2024/05/22 01:47
Description
Suppose that G is an undirected graph, and the value ofstab 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 ofstab.
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 ofstab.
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.
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 ofstab.
Sample Input
4 50 11 22 33 00 2
Sample Output
2
做法:很容易想到应该从个点入手,然而就算枚举割点,要怎么算呢?(下午试试这种枚举所有割点的方法)
本题的解法是第一个点枚举所有点,第二个点由dfs求出最大值。具体来说就是iscut原来表示是否是割点,现在让他作为“遍历时成为割点的次数”即为子树的个数。
那么对于非根节点,删去后剩余个数为iscut[i] + 1(子树个数加上父节点),根节点为iscut[i] (没有父节点)
那么全题答案便是 max(iscut[i] + 1) + left - 1
#pragma comment(linker, "/STACK:102400000000,102400000000")#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define maxn 5009vector<int>G[maxn];int pre[maxn],dfs_cnt,iscut[maxn];int n,m,none;void init(){ for(int i=0;i<n;i++)G[i].clear(); none=n;}int dfs(int u,int fa){ int lowu=pre[u]=++dfs_cnt; int child=0; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(v==none) continue; if(!pre[v]) { child++; int lowv=dfs(v,u); lowu=min(lowu,lowv); if(lowv>=pre[u]) iscut[u]++; } else if(pre[v]<pre[u]&&v!=fa) lowu=min(lowu,pre[v]); } if(fa<0&&child==1)iscut[u]=0; return lowu;}int solve(int x){ int ans=0,left=0; memset(iscut,0,sizeof(iscut)); dfs_cnt=0; memset(pre,0,sizeof(pre)); none=x; for(int i=0;i<n;i++) if(i!=x&&!pre[i]) iscut[i]--,left++,dfs(i,-1); for(int i=0;i<n;i++) if(i!=x) ans=max(ans,iscut[i]+1); ans+=left-1; return ans;}int main(){ // freopen("cin.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { init(); while(m--) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } int ans=0; for(int i=0;i<n;i++) ans=max(ans,solve(i)); printf("%d\n",ans); } return 0;}
0 0
- hdu4587TWO NODES【割点】
- hdu4587TWO NODES【割点】
- Hdu 4587 TWO NODES(割点)
- 【割点】 HDU 4587 TWO NODES
- HDOJ 4587 TWO NODES【割点】
- hdu 4587 TWO NODES (枚举+割点,5级)
- HDU 4587TWO NODES(割点,连通块,枚举)
- HDU 4587TWO NODES(割点,连通块,枚举)
- HDU-4587 TWO NODES (割点[Tarjan])
- [HDU 4587] TWO NODES (Tarjan+割点)
- HDU 4587 TWO NODES 无向图的割点
- hdu 4587 TWO NODES (割点的应用)
- HDU 4587 TWO NODES(割点 模板题)
- HDU-4587 TWO NODES(割点变形或者求点双连通分量)
- HDOJ 题目4587 TWO NODES(双联通,割点,枚举)
- 割点、割边
- 割点、割边
- 割点 割边
- extjs echarts highcharts 饼图
- Android recovery UI实现分析
- 如何解释“三门问题”
- ubuntu软件源更新及其中会遇到的各种问题(以下ID的密钥没有可用的公钥)
- 用PHP开发购物车网站(第三篇):零碎知识(1)
- hdu4587TWO NODES【割点】
- Dockerizing a Node.js web app
- easyui combotree加载静态前台json问题
- IOS之基础动画
- 2014年最热门的国人开发开源软件TOP100
- MS SQL Server:分区表、分区索引 详解
- android环境搭建
- 防刷新,只有关闭浏览器才会增加点击量
- Caffe研究实践 四 -------lenet_train_test.prototxt分析