POJ 1523 SPF 割点 Tarjan

SPF

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9317   Accepted: 4218

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

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 1009
#define N 100
/*
求割点的个数 和割点被去掉之后联通快的个数
//删除割点u产生的连通数目为:u所在的连通分量数目+与u所连接的割边的数目+1(边:fa->u)
*/
struct edge
{
    //edge(int _t,int _next):to(_t),next(_next){}
    int to, next;
};
edge E[MAXN * MAXN];
int index, dfn[MAXN], low[MAXN], s[MAXN], head[MAXN], cnt, Max, cas = 1, root;
bool flag;
void Init()
{
    index = 0;
    Max = -1;
    cnt = 0;
    flag = false;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(s, 0, sizeof(s));
    memset(low, 0, sizeof(low));
}
void add_edge(int u, int v)
{
    E[cnt].to = v;
    E[cnt].next = head[u];
    head[u] = cnt++;
    Max = max(max(u, v), Max);
}

void tarjan(int pre, int u)
{
    int son = 0;
    low[u] = dfn[u] = ++index;
    for (int i = head[u]; i != -1; i = E[i].next)
    {
        int v = E[i].to;
        if (!dfn[v])
        {
            tarjan(u, v);
            son++;
            low[u] = min(low[v], low[u]);
            if ((u == root&&son > 1) || (u != root&&low[v] >= dfn[u]))
            {
                flag = true;
                s[u]++;
            }
        }
        else if (v != pre)
            low[u] = min(low[u], dfn[v]);
    }
}

int main()
{
    int f, t;
    while (1)
    {
        Init();
        while (scanf("%d",&f), f)
        {
            scanf("%d", &t);
            add_edge(f, t);
            add_edge(t, f);
        }
        if (Max == -1)
            break;
        root = Max;
        tarjan(-1, Max);
        printf("Network #%d\n",cas++);
        if (!flag)
        {
            printf("  No SPF nodes\n");
        }
        else
        {
            for (int i = 1; i <= Max; i++)
                if (s[i] > 0)
                    printf("  SPF node %d leaves %d subnets\n", i, s[i] + 1);
        }
        printf("\n");
    }
}
时间: 2024-08-28 10:17:03

POJ 1523 SPF 割点 Tarjan的相关文章

POJ 1523 SPF 割点与桥的判断算法-Tarjan

题目链接: POJ1523 题意: 问一个连通的网络中有多少个关节点,这些关节点分别能把网络分成几部分 题解: Tarjan 算法模板题 顺序遍历整个图,可以得到一棵生成树: 树边:可理解为在DFS过程中访问未访问节点时所经过的边,也称为父子边 回边:可理解为在DFS过程中遇到已访问节点时所经过的边,也称为返祖边.后向边 对根节点u,若其有两棵或两棵以上的子树,则该根结点u为割点: 对非叶子节点u(非根节点),若其子树的节点均没有指向u的祖先节点的回边,说明删除u之后,根结点与u的子树的节点不再

POJ - 1523 SPF(割点)

题目大意:给出一张图,问去割点后,连通分量的个数有多少 解题思路:割点的水题,套模版就可以 不得不吐槽一下输入.. #include <cstdio> #include <cstring> #define min(a,b) ((a)<(b)?(a):(b)) #define N 1010 #define M 2000010 struct Edge{ int to, next; }E[M]; int head[N], num[N], pre[N], lowlink[N]; in

POJ 1523 SPF(强连通分量求割点)

题目地址:POJ 1523 这题猛的一看..貌似有点难的样子.不过仔细一想,那个每个割点所分成一次子图不就都能找到这个割点一次吗,那么只要记录下它作为割点的次数再+1不就行了.也算是求割点的裸题吧.这个题的输出很坑...需要注意一下.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #incl

POJ 1523 SPF(割点)

大致题意: 给出一个连通的无向图,求哪些点是割点,对于每个割点,求出去掉这个点后连通分量的个数.如果没有割点的话输出" No SPF nodes". 思路: 求割点用tarjan即可,然后要求删除割点后连通分量的个数,每次找到割点,再在从割点dfs即可,也可以直接在tarjan算法中记录child个数.那么如果割点是根,那么答案就是child,不是根的割点答案是child+1 //188K 0MS C++ 2121B #include<cstdio> #include<

poj 1523 SPF 求割点以及删除该割点后联通块的数量

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7136   Accepted: 3255 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

POJ 1523 SPF 求割点的好(板子)题!

题意: 给个无向图,问有多少个割点,对于每个割点求删除这个点之后会产生多少新的点双联通分量 题还是很果的 怎么求割点请参考tarjan无向图 关于能产生几个新的双联通分量,对于每个节点u来说,我们判断他是否是割点,即判断是否满足他的儿子v的low[v]>dfn[u] 而这个时候割掉这个点就会让双联通分量增加,所以搞一个数组记录一下这个操作的次数就行 请注意在是否是根节点的问题上特判 !!注意输出格式!! 1 #include<cstdio> 2 #include<algorithm

poj 1523 SPF 无向图求割点

SPF 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 s

poj 1523 SPF

SPF http://poj.org/problem?id=1523 Time Limit: 1000MS   Memory Limit: 10000K       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 fail

POJ 1523 SPF 解题报告

思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一下讨论: 如果这个割点是根节点,在tarjan算法中搜到几个孩子结点(low[v] >= dfn[u]),他就能割出几个联通块,如果这个割点是孩子结点,那么他所形成的联通块的个数+1,因为他还有一条与父亲结点间接或直接相连的边. 代码如下: #include<map> #include<