POJ 1523 SPF 求割點

來源:互聯網
上載者:User

這道題的大意就是求割點,並且求出假如去掉這個割點後,整個圖被分為了幾部分

看的北大那本圖論書,無向圖的點連通性的求解。

然後就把代碼打上來了。

書上是用鄰接矩陣儲存,我改用了vector

並且書上的代碼也有一些問題,就是沒有處理好邊和回邊的問題

因為low數組的定義,就是low[u] = min(dfn[u], low[w], dfn[v])

其中w是u的子女,而(u,v)是一條回邊

所以在DFS的過程中,加入一個變數father,用來判斷是否是回邊,不是回邊的話,顯然是不能進行處理的

#include <iostream>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cmath>#include <ctime>#define INF 1000000000using namespace std;vector<int>edge[1005];int visited[1005];int nodes;int tmpdfn;int dfn[1005];int low[1005];int son;int subnets[1005];void dfs(int u, int father){    int x = edge[u].size();    for(int i = 0; i < x; i++)    {        int v = edge[u][i];        if(!visited[v])        {            visited[v] = 1;            tmpdfn++;            dfn[v] = low[v] = tmpdfn;            dfs(v, u);            low[u] = min(low[u], low[v]);            if(low[v] >= dfn[u])            {                if(u != 1)                subnets[u]++;                if(u == 1)                son++;            }        }        else if(v != father) low[u] = min(low[u], dfn[v]);    }}void init(){    low[1] = dfn[1] = 1;    tmpdfn = 1;    son = 0;    memset(visited, 0, sizeof(visited));    visited[1] = 1;    memset(subnets, 0, sizeof(subnets));}int main(){    int u, v, flag, cas = 0;    while(scanf("%d", &u) != EOF && u)    {        for(int i = 0; i <= 1000; i++)            edge[i].clear();        nodes = 0;        scanf("%d", &v);        if(u > nodes) nodes = u;        if(v > nodes) nodes = v;        edge[u].push_back(v);        edge[v].push_back(u);        while(scanf("%d", &u) != EOF && u)        {            scanf("%d", &v);            if(u > nodes) nodes = u;            if(v > nodes) nodes = v;            edge[u].push_back(v);            edge[v].push_back(u);        }        if(cas) printf("\n");        printf("Network #%d\n", ++cas);        init();        dfs(1, 0);        if(son > 1) subnets[1] = son - 1;        flag = 0;        for(int i = 1; i <= nodes; i++)        {            if(subnets[i])            {                flag = 1;                printf("  SPF node %d leaves %d subnets\n", i, subnets[i] + 1);            }        }        if(!flag)        printf("  No SPF nodes\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.