poj 1523(无向联通图的割点)
2013-05-13 14:08
274 查看
结合tarjan算法思想,这题终于写了出来。
同样用dfs将图变成为一颗树,这样可以提供许多有用的性质。
对于一个无向连通图,dfs后的树为只有回边(回边Euv,v是u的祖先)和生成树的边的图。 那么在遍历到一个点u的时候,可以知道如果不考虑这个点,如果与u相邻的点连通那么u不是割点,否则是割点。 那么只需要判断与u相邻的点是否连通就行了,于是借鉴tarjan求强连通的办法,在dfs时,对每个点标记一个深度low
也就是从根到这个点最短路径(经过的最小结点数), 然后在遍历到u点的时候,看看与u相邻的点v的low[v], 如果low[U] >= low[u]那么说明u就是割点. 因为v点无法到达u相邻的某些点。 当然当u为根节点的时候要特判
同样用dfs将图变成为一颗树,这样可以提供许多有用的性质。
对于一个无向连通图,dfs后的树为只有回边(回边Euv,v是u的祖先)和生成树的边的图。 那么在遍历到一个点u的时候,可以知道如果不考虑这个点,如果与u相邻的点连通那么u不是割点,否则是割点。 那么只需要判断与u相邻的点是否连通就行了,于是借鉴tarjan求强连通的办法,在dfs时,对每个点标记一个深度low
也就是从根到这个点最短路径(经过的最小结点数), 然后在遍历到u点的时候,看看与u相邻的点v的low[v], 如果low[U] >= low[u]那么说明u就是割点. 因为v点无法到达u相邻的某些点。 当然当u为根节点的时候要特判
SPF
Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible. Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate. Input The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored. Output For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist. The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes. Sample Input 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 1 2 2 3 3 4 4 6 6 3 2 5 5 1 0 0 Sample Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes Network #3 SPF node 2 leaves 2 subnets SPF node 3 leaves 2 subnets Source Greater New York 2000 |
// start time 12:44 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; #define N 1010 #define INF 0x3ffffff int g ; int mark ; int low ; int save ; int n; void dfs(int s,int cnt) { low[s]=cnt; int mi=INF; int k=0; for(int i=1;i<=n;i++) { if(g[s][i]==0||s==i) continue; if(low[i] == -1) { dfs(i,cnt+1); if( low[i] >= cnt ) k++; } mi=min(low[i],mi); } low[s]=mi; if(cnt==0) k--; if(k!=0) save[s]=k; } int main() { int tt=1; int x,y; while(scanf("%d",&x)&&x) { memset(mark,0,sizeof(mark)); mark[x]=1; n=0; if(x>n) n=x; memset(g,0,sizeof(g)); memset(save,-1,sizeof(save)); memset(low,-1,sizeof(low)); scanf("%d",&y); mark[y]=1; if(y>n) n=y; g[x][y]=g[y][x]=1; while(scanf("%d",&x)&&x) { mark[x]=1; if(x>n) n=x; scanf("%d",&y); mark[y]=1; if(y>n) n=y; g[x][y]=g[y][x]=1; } for(int i=1;i<=n;i++) if(mark[i]==1) { dfs(i,0); break; } printf("Network #%d\n",tt++); int flag=0; for(int i=1;i<=n;i++) { if(save[i]!=-1) { flag=1; printf(" SPF node %d leaves %d subnets\n",i,save[i]+1); } } if(flag==0) printf(" No SPF nodes\n"); printf("\n"); } return 0; }
相关文章推荐
- poj 2117 Electricity 求无向图中去掉一个点后最大的联通分支数 无向图有可能不联通 tarjan求割点模板
- poj 1523 求图的割点
- poj 1815 Friendship 最小割+邻接表 求最小点割集(求无向图中最少删除几个顶点使得s和t不联通) 输出顶点是要枚举
- poj 1523 SPF (无向图 求割项)
- POJ 2117 求无向图的割点
- poj 1523 求割点把一个图分成几个联通部分
- poj 1144 Network 【求无向图中割点总数】【点双联通入门题目】
- poj 1523 (统计割点,及割点所分割的块数)
- 基于DFS求无向图的割点及桥(割边)算法总结 POJ_1144题解
- poj 1523 求割点把一个图分成几个联通部分
- 【tarjan双连通求割点&连通分量】POJ 1523
- POJ 1144 Network(求无向图中的割点)
- POJ 1523——Tarjan + 计算删除割点后的连通分支数
- poj 1523 SPF 图论基础 图的割点
- poj1523—SPF(tarjan算法求无向图中所有的割点)
- (求割点和除去割点后的联通块数目) poj 1523
- POJ 1523 无向图的割点
- poj1523割点&&块
- poj1523割点模板
- POJ 1144 - Network 用tarjan求无向图的割点