poj1523 SPF

SPF
链接:http://poj.org/problem?id=1523
Time Limit: 1000MS Memory Limit: 10000K
   

题目大意:

有一个网络,在这个网络里,电脑之间的通信只能是两台电脑间(点对点)双向通信。如下面左图

所示:如果3号电脑出故障了,那么1号和2号之间、4号和5号之间还可以通信,不过1、2和3、4

号电脑之间就不能通信了,那么3号电脑就是一个SPF节点,且3号电脑故障后,整个网络被分为

了2个子网络。那么问题来了:给你一些边。问删除某个SPF节点后,可以将图分为几个连通分量。

 

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

题解:

其实就是给你一个连通图,求出这个连通图的所有割点编号,并求出若删去其中一个割点后,原网

络被分成几个子网络。这里我们使用的思路和Tarjan算法类似。

判断一个点是不是割点,如果一个节点v是割点,满足下面两种情况。

1.v为树根,且v有多于一个子树;2.v不为树根,且满足存在边(v,u),使得dfn[v] <= low[u]。

我开始设了child都没有用,一个模板题一直WA

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
// 0--n-1 1   n -- 2n-1 0
const int maxn = 1005, maxm = 50005;
int tot,h[maxn],dfn[maxn],low[maxn],idx,scc;
int n,q[maxn];
bool ins[maxn],iscut[maxn];
stack <int> t;
struct edge{
    int u,to,nxt;
}G[maxm];
void add(int u,int v){G[++tot].nxt = h[u];G[tot].u = u;G[tot].to = v;h[u] = tot;}

void tarjan(int u, int f){
    int child = 0;
    dfn[u] = low[u] = ++idx;
    t.push(u); ins[u] = 1;
    for(int i = h[u]; i; i = G[i].nxt){
        int v = G[i].to;
        if(v == f)continue;
        child++;
        if(!dfn[v]){
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(dfn[u] <= low[v]){
                scc++;
                q[u]++;
            }
        }
        else if(ins[v]) low[u] = min(low[u], dfn[v]);

    }
    if(f < 0 && child < 2)q[u] = 0;

}
void init(){
    idx = tot = scc = 0;
    memset(G, 0, sizeof(G));
    memset(h, 0, sizeof(h));
    memset(dfn, 0, sizeof(dfn));
    memset(ins, 0, sizeof(ins));
    memset(q, 0, sizeof(q));
    while(!t.empty())t.pop();
}
int main(){
    int f = 0, k = 0, u, v;
    while(scanf("%d",&u)&&u){
        k++;
        init();
        int qq = 0, siz = u;
        scanf("%d",&v);
        add(u, v);add(v, u);
        siz = max(siz, v);
        while(scanf("%d",&u)&&u){
            scanf("%d",&v);
            siz = max(u, max(siz, v));
            add(u, v);add(v, u);
        }

        tarjan(1, -1);
        printf("Network #%d\n",k);
        q[1]--;
        for(int i = 1; i <= siz; i++)
            if(q[i] > 0)qq = 1, printf("  SPF node %d leaves %d subnets\n",i,q[i]+1);
        if(!qq)printf("  No SPF nodes\n");
        puts("");
    }

}
View Code

 

转载于:https://www.cnblogs.com/EdSheeran/p/9396275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值