poj1144Network (求割點模板題)

來源:互聯網
上載者:User

標籤:

題目串連

題意:給出一個無向圖,求出割點的個數

code:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#define maxn 100005using 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() != ‘\n‘)            {                cin >> b;                G[a].push_back(b);                G[b].push_back(a);            }        }        root = 1;        dfs(1,0);        cout << ans << endl;    }    return 0;}

 

poj1144Network (求割點模板題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.