HDU4587 [TWO NODES] tarjan求无向图割顶
来源:互联网 发布:js获取自定义属性 编辑:程序博客网 时间:2024/06/06 11:38
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
题意:求删去无向图中两个节点后连通块的最大数量
solution:枚举第一个点,tarjan判第二个点。
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int N = 5e3 + 7;struct Edge{ int nxt, to;}e[N<<1];int head[N], tot=0;void addeage(int u, int v){ e[++tot].nxt=head[u]; e[tot].to=v; head[u]=tot;} int dfn[N], low[N], iscut[N], dnt=0, root;void dfs(int u, int fa, int del){ dfn[u]=low[u]=++dnt; for ( int i=head[u]; i!=-1; i=e[i].nxt ){ int v=e[i].to; if( v==del || v==fa ) continue; if( !dfn[v] ){ dfs(v, u, del); // 不访问del节点 if( dfn[u]<=low[v] ) iscut[u]++; // 统计删去当前点后增加的连通块的个数 low[u]=min(low[u], low[v]); } else { low[u]=min(low[u], dfn[v]); } }}int n, m;int solve(){ int ret=0; for ( int i=1; i<=n; i++ ){ for ( int j=1; j<=n; j++ ) dfn[j]=low[j]=0; for ( int j=1; j<=n; j++ ) iscut[j]=1; dnt=0; int ans=0; for ( int j=1; j<=n; j++ ) if( !dfn[j] && i!=j ){ iscut[j]=0; //给根赋初值 ans++; dfs(j, 0, i); } for ( int j=1; j<=n; j++ ) if( i!=j ){ ret=max(ret,ans+iscut[j]-1); } } return ret;} int main(){ while( ~scanf("%d%d", &n, &m ) ){ tot=0; memset(head,-1,sizeof(head)); for ( int i=1; i<=m; i++ ){ int x, y; scanf("%d%d", &x, &y ); addeage(x+1,y+1); addeage(y+1,x+1); } printf("%d\n", solve() ); } }
- HDU4587 [TWO NODES] tarjan求无向图割顶
- hdu4587 TWO NODES(无向图割点)
- hdu4587 TWO NODES
- HDU4587 TWO NODES
- hdu4587 TWO NODES 求割点
- HDU 4587 TWO NODES(无向图割点)
- HDU 4587 TWO NODES(无向图割点)
- HDU 4587 TWO NODES 无向图的割点
- POJ 2117 Electricity(无向图割点) && HDU 4587 TWO NODES
- hdu4738 Caocao's Bridges 无向图 tarjan 求桥
- poj3694 Network 无向图tarjan求桥+LCA
- hdu4738Caocao's Bridges tarjan求无向图的桥
- hdoj 4738 tarjan求无向图的桥
- 用Tarjan算法求无向连通图割点&&割边
- hdu3849 Tarjan求无向图的割边+map
- POJ 1523 SPF Tarjan求无向图割点
- UVA 315 Network(Tarjan求无向图割点)
- Tarjan求无向图桥和割点
- Java值传递和引用传递
- 刷题——hdu 6180 Schedule
- 微信小程序修改数组的值
- winform与静态页面中交互总结
- 51nod1073 约瑟夫环
- HDU4587 [TWO NODES] tarjan求无向图割顶
- Linux安装nodejs和npm
- for(:)遍历数组的一种方式
- 剑指offer-对称的二叉树
- 第八章 Testing Concurrent Applications(测试并发应用)【上】
- zookeeper搭建
- java学习笔记之面向接口编程
- Python strip函数和split函数
- java double 和Double的区别 及hashCode的应用