Poj 1523 SPF 关节点

简介:

 经典的关节点例题,要注意只有1个点的情况

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int org[1001][1001],n;
bool ok;
int dph[1001],low[1001],vis[1001],Ans[1001];
void dfs(int u,int d)//Tarjan
{
    dph[u]=low[u]=d;
    vis[u]=1;
    int ans=0;
    for(int v=1;v<=n;v++)
    {
        if(!org[u][v])continue;
        if(vis[v])
        {
            low[u]=min(low[u],dph[v]);
        }
        else
        {
            dfs(v,d+1);
            if(low[v]>=dph[u])ans++;
            low[u]=min(low[u],low[v]);
        }
    }
    if(u==1&&ans>=1)Ans[u]=ans-1;
    else Ans[u]=ans;
}
int main()
{
    int a,b,T=0;
    while(~scanf("%d",&a)&&a)
    {
        n=0;
        scanf("%d",&b);
        memset(org,0,sizeof(org));
        if(T)puts("");
        T++;
        org[a][b]=org[b][a]=1;
        n=max(n,max(a,b));
        while(~scanf("%d",&a)&&a)
        {
            scanf("%d",&b);
            org[a][b]=org[b][a]=1;
            n=max(n,max(a,b));
        }
        ok=0;
        memset(Ans,0,sizeof(Ans));
        memset(vis,0,sizeof(vis));
        printf("Network #%d\n",T);
        dfs(1,1);
        for(int i=1;i<=n;i++)
        {
            if(Ans[i])
                ok=1,
                printf("  SPF node %d leaves %d subnets\n",i,Ans[i]+1);
        }
        if(!ok)printf("  No SPF nodes\n");
    }
}


目录
相关文章
|
6月前
最短路径问题(HDU3790)
最短路径问题(HDU3790)
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
56 0
codeforces731——C. Socks(并查集)
codeforces731——C. Socks(并查集)
80 0
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
164 0
codeforces-1242-B 0-1 MST
|
存储 机器学习/深度学习 算法
SPF单源最短路径算法
SPF(shortest path first)算法也叫Dijkstra(迪杰斯特拉)算法,由上个世纪的计算机科学家狄克斯特拉提出,是离散数学中一种经典高效的网络(连通图)最短路径寻路算法.指定一个源点,求出到其余各个顶点的最短路径,也叫”单源最短路径”.
492 1
SPF单源最短路径算法
|
算法 定位技术 C语言
|
算法 网络协议