poj1523 SPF

Posted edsheeran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
",k);
        q[1]--;
        for(int i = 1; i <= siz; i++)
            if(q[i] > 0)qq = 1, printf("  SPF node %d leaves %d subnets
",i,q[i]+1);
        if(!qq)printf("  No SPF nodes
");
        puts("");
    }

}
View Code

 



以上是关于poj1523 SPF的主要内容,如果未能解决你的问题,请参考以下文章

poj 1523 SPF

POJ-1523-SPF(求割点)

poj1523 SPF

POJ 1523 SPF 割点 Tarjan

POJ1523SPF[无向图割点]

POJ1523 SPF(割点)