POJ1523(求连用分量数目,tarjan算法原理理解)

SPF

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7406   Accepted: 3363

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题意:问将某个点删除可产生多少个连通分量。思路:考察对tarjan算法原理理解,解释见代码。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=1005;
struct Edge{
    int to,next;
}es[MAXN*2];
int V,E;
int head[MAXN];
inline int max(int u,int v)
{
    return u > v? u: v;
}
inline int min(int a,int b)
{
    return a > b? b: a;
}
void add_edge(int u,int v)
{
    es[E].to=v;
    es[E].next=head[u];
    head[u]=E++;
    V=max(max(u,v),V);
}
bool flag;
int root;
int subnets[MAXN];
int index;
int dfn[MAXN],low[MAXN];
void tarjan(int u,int fa)
{
    int son=0;
    dfn[u]=low[u]=++index;
    for(int i=head[u];i!=-1;i=es[i].next)
    {
        int v=es[i].to;
        if(!dfn[v])
        {
            tarjan(v,u);
            son++;
            low[u]=min(low[u],low[v]);
            if((u==root&&son>1)||(u!=root&&dfn[u]<=low[v]))
            {
                flag=true;
                subnets[u]++;
                //u->v  该边导致u成为割点
                //当dfn[u]==low[v]时u->v为返祖边,u、v处于同一双连通分量中
                //当dfn[u]<low[v]时u->v为割边
                //删除割点u产生的连通数目为:u所在的连通分量数目+与u所连接的割边的数目+1(边:fa->u)
            }
        }
        else if(v!=fa) low[u]=min(low[u],dfn[v]);
    }
}
int main()
{
    int cas=0;
    int u,v;
    while(true)
    {
        v=-1;
        index=0;
        memset(subnets,0,sizeof(subnets));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(head,-1,sizeof(head));
        V=-1,E=0;
        flag=false;
        while(scanf("%d",&u)&&u)
        {
            scanf("%d",&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        if(v==-1)    break;
        root=V;
        tarjan(root,-1);
        printf("Network #%d\n",++cas);
        if(flag)
        {
            for(int i=1;i<=V;i++)
            {
                if(subnets[i]>0)
                {
                    printf("  SPF node %d leaves %d subnets\n",i,subnets[i]+1);//加上fa->u该边所连接的连通分量
                }
            }
        }
        else    printf("  No SPF nodes\n");
        printf("\n");
    }
    return 0;
}
				
时间: 2024-08-26 06:25:03

POJ1523(求连用分量数目,tarjan算法原理理解)的相关文章

poj1236 Network of Schools ,求强连通分量(Tarjan算法),缩点

题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点 顶点数<= 100 求完强连通分量后,缩点,计算每个点的入度,出度. 第一问的答案就是入度为零的点的个数, 第二问就是max(n,m) // 入度为零的个数为n, 出度为零的个数为m. //kuangbin巨巨分析很棒! #include<cstdio> #include<cstring>

对求有向图强连通分量的tarjan算法原理的一点理解

先简单叙述一下tarjan算法的执行过程(其他诸如伪代码之类的相关细节可以自己网上搜索,这里就不重复贴出了): 用到两类数组: dfs[]:DFS过程中给定节点的深度优先数,即该节点在DFS中被访问的次序 low[]:从给定节点回溯时,节点的low值为从节点在DFS树中的子树中的节点可以回溯到的栈中DFS值最小的节点的dfs值 一个数据结构:栈,用于确定强连通分量 执行过程:对有向图进行深度优先搜索,每抵达一个新节点A就把该节点A入栈,并初始化dfs[A],然后将low[A]初始化为dfs[A]

寻找图的强连通分量:tarjan算法简单理解

1.简介tarjan是一种使用深度优先遍历(DFS)来寻找有向图强连通分量的一种算法. 2.知识准备栈.有向图.强连通分量.DFS. 3.快速理解tarjan算法的运行机制提到DFS,能想到的是通过栈来储存沿途的点,可以找到所有的环.环本身就是联通的,所以环对于强连通分量来说环已经很接近最终答案了.要把找环变成找强连通管分量还要考虑:a.在环外是不是有其他环在这个强连通分量内(极大性) (会被认为是2个环) b.一些不能构成环的点无法被考虑到,而他们本身就是强连通分量 (2不被认为是一个强连通分

poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点

题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点.才干做到从这些顶点出发,能够到达所有顶点 2) 至少要加多少条边.才干使得从不论什么一个顶点出发,都能到达所有顶点 顶点数<= 100 求完强连通分量后,缩点,计算每一个点的入度,出度. 第一问的答案就是入度为零的点的个数, 第二问就是max(n,m) // 入度为零的个数为n, 出度为零的个数为m. //kuangbin巨巨分析非常棒! #include<cstdio> #include<cstrin

【转载】有向图强连通分量的Tarjan算法

from byvoid [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 直接根据定义,用双向遍历取交集的方法求强连通分量,时间复杂度为

算法笔记_144:有向图强连通分量的Tarjan算法(Java)

目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.有向图的极大强连通子图,称为强连通分量(strongly connected components). Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量. 定义D

有向图强连通分量的Tarjan算法(转)

原文地址:有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 直接根据定义,用双向遍历取交集的方法求强

有向图强连通分量的Tarjan算法——转自BYVoid

[有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 直接根据定义,用双向遍历取交集的方法求强连通分量,时间复杂度为O(N^2+M).更好的

有向图强连通分量的Tarjan算法

有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 直接根据定义,用双向遍历取交集的方法求强连通分量,