POJ 1523 SPF(求割点)

本文介绍了一种算法,该算法通过深度优先搜索(DFS)来确定无向图中每个割点移除后剩余的连通子网数量。利用Tarjan算法进行离线计算,实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:问去掉每一个割点后的联通分支数;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
const int N = 1009;
struct LT{
    int nex,to;
}L[N*N];
int F[N],cnt;
int has[N];
int re[N],rcnt;
void add(int f,int t)
{
    L[cnt].nex = F[f];
    L[cnt].to = t;
    F[f] = cnt++;
}
bool init()
{
    int c=1;
    cnt=1;rcnt=1;
    memset(F,0,sizeof(F));
    memset(has,0,sizeof(has));
    int f,t;
    while(1)
    {
        scanf("%d",&f);
        if(f==0)
        {
            if(c==1) return false;
            return true;
        }
        c=0;
        scanf("%d",&t);
        if(!has[f]) re[rcnt]=f,has[f] = rcnt,rcnt++;
        if(!has[t]) re[rcnt]=t,has[t] = rcnt,rcnt++;
        add(has[f],has[t]);
        add(has[t],has[f]);
    }
}
int dfn[N],low[N],ind,head,son,ans[N];

void tarjan(int k,int fa)
{
    //cout<<k<<" "<<fa<<endl;
    dfn[k] = low[k] = ind++;
    for(int i=F[k];i;i=L[i].nex)
    {
        int to = L[i].to;
        if(to == fa) continue;
        if(!dfn[to])
        {
            tarjan(to,k);
            if(dfn[k]<=low[to])
            ans[k]++;
            else
            low[k] = min(low[k],low[to]);
        }
        else
        low[k] = min(low[k],dfn[to]);
    }
}
struct node{
    int v,ans;
    bool operator<(const node t) const
    {
        return v<t.v;
    }
} vv[N];
void solve(int T)
{
    memset(dfn,0,sizeof(dfn));
    memset(ans,0,sizeof(ans));
    ans[1]=-1;ind = 1;
    tarjan(1,-1);
    int a=0;
    for(int i=1;i<rcnt;i++)
    {
        if(ans[i]>0)
        {
            vv[a].v=re[i],vv[a].ans=ans[i]+1;
            a++;
        }
    }
    printf("Network #%d\n",T);
    if(a==0)
    {
        printf("  No SPF nodes\n\n");
        return ;
    }
    sort(vv,vv+a);
    for(int i=0;i<a;i++)
    {
        printf("  SPF node %d leaves %d subnets\n",vv[i].v,vv[i].ans);
    }
    printf("\n");
}
int main()
{
    freopen("in.txt","r",stdin);
    int T=1;
    while(init()) solve(T),T++;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值