题目链接:poj1523
SPF
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9893 | Accepted: 4447 |
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.
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.
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
题意:在一个无向图里求割点,并且要求出去掉该点后图的联通分量有几个。
题解:tarjan求割点算法稍微改一下,或者直接dfs暴力枚举。
tarjan:
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <ostream>
#include <istream>
using namespace std;
const int maxn = 2e5 + 7;
int n,tot,Size,root,head[maxn];
int dfn[maxn],low[maxn];
//dfn[u]为结点u在搜索树中的次序号
//low[u]为节点u或者结点u的子孙中能通过非父子边追溯到的dfn最小的结点
int iscut[maxn];//去掉割点i后的联通分量个数
bool flag;
bool vis[maxn];
int index;
struct node{
int to,nxt;
node(){}
node(int _to,int _nxt):to(_to),nxt(_nxt){}
}e[maxn<<2];
void addedge(int u,int v){//链式前向星加边操作
e[tot]=node(v,head[u]);
head[u]=tot++;
}
void tarjan(int u,int fa){
dfn[u]=low[u]=++index;
int son=0;
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]){
tarjan(v,u);
son++;
low[u]=min(low[u],low[v]);
if((u==root&&son>1)||(u!=root&&low[v]>=dfn[u])){
//如果u是根节点并且u有多于一颗子树,说明u是割点
//若u不是根节点,并且存在u的孩子v,使得dfn[u]<=low[v]
//也就是说去掉结点u后v并不能到达u的祖先,则u是割点
iscut[u]++;//联通分量++
flag=true;
}
}else if(v!=fa){
low[u]=min(low[u],dfn[v]);
}
}
}
int main(){
int u,v;
int cas=0;
while(1){
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(vis,false,sizeof(vis));
memset(iscut,0,sizeof(iscut));
memset(head,-1,sizeof(head));
n=0;
flag=false;
while(1){
cin >> u;
if(u==0) break;
cin >> v;
addedge(u,v); addedge(v,u);
n=max(n,max(v,u));
}
if(!n) break;
if(cas) cout << endl;
cout << "Network #" << ++cas << endl;
int ans=0;
for(int i=1;i<=n;i++){
if(!dfn[i]){
root=i;
tarjan(i,-1);
}
}
if(flag){
for(int i=1;i<=n;i++){
if(iscut[i]>0){
cout << " SPF node " << i << " leaves " << iscut[i]+1 << " subnets" << endl;//加上fa->u该边所连接的连通分量
}
}
}else cout << " No SPF nodes" << endl;
}
return 0;
}
dfs暴力:
这里需要注意的是所给的点的序号有可能不是连续的,所以在输入边的时候要标记一下,否则就会出错。
AC代码:
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e4 + 7;
int tot,head[maxn];
bool connect[maxn];//标记点是否出现
bool vis[maxn];
int n;
struct node{
int to,nxt;
node(){}
node(int _to,int _nxt):to(_to),nxt(_nxt){}
}e[maxn<<1];
void addedge(int u,int v){
e[tot]=node(v,head[u]);
head[u]=tot++;
}
void dfs(int u){//暴力dfs
vis[u]=true;
for(int i=head[u];~i;i=e[i].nxt){
int to=e[i].to;
if(!vis[to]){
dfs(to);
}
}
}
int main(){
int cas=0;
// freopen("E:\\233.txt","w",stdout);
while(1){
memset(connect,false,sizeof(connect));
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
n=0;
while(1){
int u,v;
cin >> u;
if(u==0) break;
cin >> v;
addedge(u,v);
addedge(v,u);
connect[u]=true;
connect[v]=true;
n=max(n,max(u,v));
}
if(n==0) break;
bool flag=false;
if(cas) cout << endl;
cout << "Network #" << ++cas << endl;
for(int i=1;i<=n;i++){
int cnt=0;
memset(vis,false,sizeof(vis));
vis[i]=true;
//head[i]=-1;
for(int j=1;j<=n;j++){
if(!vis[j]&&j!=i&&connect[j]){//注意判断条件
dfs(j);
cnt++;
}
}
if(cnt>=2){
flag=true;
cout << " SPF node " << i << " leaves " << cnt << " subnets" << endl;
}
}
if(!flag) cout << " No SPF nodes" << endl;
}
return 0;
}