无向图求割点

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxx = 1005;
int dfn[maxx],low[maxx],flag[maxx],sign[maxx];
vector<int> v[maxx];
int cnt = 0,ans = 0,root;
void tarjan(int x,int pre)
{
    int child = 0;
    flag[x] = 1;
    low[x] = dfn[x] = ++cnt;
    for(int i=0;i<v[x].size();i++){
        int now = v[x][i];
        if(!flag[now]){
            child++;
            tarjan(now , x);
            low[x] = min(low[x] , low[now]);

            if(x != root && low[x] >= dfn[x]){
                sign[x] = 1,ans++;
            }
            if(x == root && child >= 2){
                sign[x] = 1,ans++;
            }
        }
        else if(pre != now){
            low[x] = min(low[x],dfn[now]);
        }
    }
}
int main()
{
    int n,m,x,y;
    cin>>n>>m;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(flag,0,sizeof(flag));
    for(int i=1;i<=m;i++){
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1;i<=n;i++){
        root = i;
        if(!dfn[i])
            tarjan(i , -1);
    }
    printf("割点的个数是:%d\n",ans);

    for(int i=1;i<=n;i++){
        if(sign[i] == 1){
            cout<<i<<" ";
        }
    }
    return 0;
}
全部评论

相关推荐

xdm怎么说&nbsp;要被拷打了&nbsp;担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务