Network UVA - 315(tarjan求割点)

Network UVA - 315

题意&&思路:

就是求割点。

AC

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <sstream>
#define mst(x,a) memset(x,a,sizeof(x))
#define fzhead EDGE(int _to, int _next)
#define fzbody to(_to), next(_next)
#define For(i,x,y) for(int i=(x); i<=(y); i++)
using namespace std;
const string lim="0";
const int maxm=1e4+10;
const int maxn=100+10;
int n;
struct EDGE{
    int to,next;
    EDGE(){}
    fzhead:fzbody{}
}e[maxm];
int dfs_clock,bcc_cnt,root,tot;
int head[maxn],vis[maxn],low[maxn],dfn[maxn],cut[maxn];
stack<int>s;
void init(){
    root = tot = dfs_clock = bcc_cnt = 0;
    mst(head,-1);
    mst(vis,0);
    mst(low,0);
    mst(dfn,0);
    mst(cut,0);
    while(!s.empty())s.pop();
}
void add(int bg, int to){
    e[tot]=EDGE(to,head[bg]);
    head[bg]=tot++;
}
void tarjan(int u){
    vis[u]=1;
    low[u]=dfn[u]=++dfs_clock;
    int flag=0;
    s.push(u);
    for(int i=head[u]; i!=-1; i=e[i].next){
        int v=e[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]&&(u!=root||++flag>1))cut[u]=1;
        }else low[u]=min(low[u],dfn[v]);
    }
    ///这里就不用求强联通块了
}
int main()
{
    while(cin>>n,n){
        getchar();
        init(); tot=2; root=1;
        string line;
        while(getline(cin,line)&&line!=lim){
            stringstream ss(line);
            int u,v;
            ss>>u;
            ///cout<<"u----"<<u<<endl;
            while(ss>>v){
                ///cout<<"v----"<<v<<endl;
                ///cout<<v<<endl;//add()
                add(u,v);
                add(v,u);
            }
        }
        For(i,1,n){
            if(!dfn[i])dfs_clock=0,root=i,tarjan(i);
        }
        int ans=0;
        For(i,1,n)if(cut[i])ans++;
      //  cout<<"ans-------";
        cout<<ans<<endl;
    }
    return 0;
}
/*
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值