您的位置:首页 > 理论基础 > 计算机网络

HLG 1494网络 (求的割点数目)可做模板

2015-08-30 21:24 375 查看
网络
Time Limit: 1000 MSMemory Limit: 65535 K
Total Submit: 103(32 users)Total Accepted: 54(31 users)Rating:Special Judge: No
Description
一个电话公司建立了一个新的电话网,他们用1——N的整数标记了一些地方,每个地方有唯一的编号,网络中的每条电缆双向连接了两个地方,从每个地方可以通过电缆连接到任意其他的地方,因为它们之间不一定要有直接相连的电缆,可以通过其他的电缆间接连接,有的时候某个地方的电力可能会中断,从而导致这个地方无法被连接,电话公司意识到了某些地方的电力中断可能会导致其他地方也无法互相连接,把这些地方叫做关键点,所以他们决定写一个程序去找到这些地方。
Input
每组输入是一个电话网络,每组的第一行是一个整数N<100,表示电话网络连接了N个地点,接下来是不超过N行整数,每行表示第一个整数与后面的整数表示的地点之间有电缆直接相连,每组数据以0结束,输入也以0结束。
Output
输出关键点的数量
Sample Input
5

5 1 2 3 4

0

6

2 1 3

5 4 6 2

0

0
Sample Output
1
2

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 110
using namespace std;
int dfn
, low
;
bool mark
, ans
;
bool edge

;
int cnt, son, n;
int min(int a, int b)
{
return a < b ? a : b;
}
void unit()
{
memset(edge, 0, sizeof(edge));
memset(mark, 0, sizeof(mark));
memset(ans, 0, sizeof(ans));
cnt=1; son=0;
mark[1]=low[1]=dfn[1]=1;
}

void dfs(int u)
{
for(int v=1;v<=n;v++)
{
if(edge[u][v])
{
if(!mark[v])
{
mark[v]=1;
dfn[v]=low[v]=++cnt;
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
if(u!=1) ans[u]=1;
else son++;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
return ;
}

int main()
{
int i;
char str[300];
while(cin >> n)
{
if(n==0) break;
getchar();
unit();
while(true)
{
gets(str);
if(strcmp(str, "0")==0) break;
int len=strlen(str);
int num=0;
for(i=0; str[i]>='0'&&str[i]<='9'; i++)
num=num*10+str[i]-'0';
for(;i<len; i++)
{
int m=0;
for(;str[i]>='0'&&str[i]<='9'&&i<len; i++)
{
m=m*10+str[i]-'0';
}
if(m!=0) edge[num][m]=edge[m][num]=1;
}
}
dfs(1);
int js=0;
for(int j=1; j<=n; j++)
js+=ans[j];
if(son>=2) cout << js+1 << endl;
else cout << js << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: