您的位置:首页 > 其它

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为根节点的时候要特判

SPF

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 4366Accepted: 2009
Description

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: