• poj1144Network (求割点模板题)


    题目连接

    题意:给出一个无向图,求出割点的个数

    code:

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #define maxn 100005
    using namespace std;
    vector<int> G[maxn];
    int dfn[maxn],vis[maxn],low[maxn];     
    bool isGedian[maxn];                  //标记该点是否是割点 
    int fa[maxn];                         //父亲节点 
    int depth;                         
    int root;
    int ans;
    void init(){
        memset(isGedian,0,sizeof(isGedian));
        memset(fa,0,sizeof(fa));
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < maxn;i ++){
            G[i].clear();
        }
    }
    void dfs(int cur,int depth){
         int cnt = 0;                       //记录当前节点的孩子个数 
         low[cur] = dfn[cur] = depth;
         vis[cur] = true;
         for(int i = 0;i < G[cur].size();i ++){
             int v = G[cur][i];
            if(!vis[v] && v != fa[cur]){
                fa[v] = cur;
                dfs(v,depth+1);
                cnt ++;
                low[cur] = min(low[cur],low[v]);
                if(low[v] >= dfn[cur] && root!= cur){
                    if(!isGedian[cur]){
                        ans ++;
                    }
                    isGedian[cur] = true;
                }
            }
            else{
                low[cur] = min(low[cur],dfn[v]);
            }
         }
         if(root == cur && cnt > 1){
            if(!isGedian[cur]){
                ans ++;
            }
            isGedian[cur] = true;
         }
    }
    int main()
    {
        int n,a,b;
        while(cin >> n && n){
            ans = 0;
            init();
            while(cin >> a && a){
                while(getchar() != '
    ')
                {
                    cin >> b;
                    G[a].push_back(b);
                    G[b].push_back(a);
                }
            }
            root = 1;
            dfs(1,0);
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    显示器接口
    常用英语-持续更新
    Web Service
    单元测试--Moq
    单元测试--Xunit
    Asp.Net WebApi 跨域问题
    VS中常用的快捷键
    单元测试--最佳实践
    设计模式--建造者模式
    windows10搭建GitBucket服务器(1)
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5721414.html
Copyright © 2020-2023  润新知