日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Network UVA - 315(Tarjan+连通性问题:求割点)

發布時間:2023/12/4 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Network UVA - 315(Tarjan+连通性问题:求割点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

存在n個電話公司的網絡連接站點,每個站點之間相互連通,是一個連通圖,問,如果去掉一個站點,能夠將整個網絡體系變得不再連通,那么這樣的點有幾個,即求它的割點個數。

題目:

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting
several places numbered by integers from 1 to N. No two places have the same number. The lines
are bidirectional and always connect together two places and in each place the lines end in a telephone
exchange. There is one telephone exchange in each place. From each place it is possible to reach
through lines every other place, however it need not be a direct connection, it can go through several
exchanges. From time to time the power supply fails at a place and then the exchange does not operate.
The officials from TLC realized that in such a case it can happen that besides the fact that the place
with the failure is unreachable, this can also cause that some other places cannot connect to each other.
In such a case we will say the place (where the failure occured) is critical. Now the officials are trying
to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line
of each block there is the number of places N < 100. Each of the next at most N lines contains the
number of a place followed by the numbers of some places to which there is a direct line from this place.
These at most N lines completely describe the network, i.e., each direct connection of two places in
the network is contained at least in one row. All numbers in one line are separated by one space. Each
block ends with a line containing just ‘0’. The last block has only one line with N = 0.

Output

The output contains for each block except the last in the input file one line containing the number of
critical places.

Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output

1
2

分析:

無向圖中,如果有一個頂點,刪除這個頂點以及這個頂點相關聯的邊以后,圖的連通分量增多,就稱這個點集為割點。
判斷一個頂點是不是割點除了從定義,還可以從DFS(深度優先遍歷)的角度出發。我們先通過DFS定義兩個概念。
假設DFS中我們從頂點U訪問到了頂點V(此時頂點V還未被訪問過),那么我們稱頂點U為頂點V的父頂點,V為U的孩子頂點。
在頂點U之前被訪問過的頂點,我們就稱之為U的祖先頂點。
顯然如果頂點U的所有孩子頂點可以不通過父頂點U而訪問到U的祖先頂點,那么說明此時去掉頂點U不影響圖的連通性,U就不是割點。
相反,如果頂點U至少存在一個孩子頂點,必須通過父頂點U才能訪問到U的祖先頂點,那么去掉頂點U后,頂點U的祖先頂點和孩子頂點就不連通了,說明U是一個割點。
情況1:1.u為當前搜索樹的根結點且u的子樹數量大于1 即 (x==f&&son>=2)
情況2:2.u不為根且存在樹邊(u,v)使dfn(u)<=low(v) 對應(low[v] >= dfn[x] && x != f)

AC代碼

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 1e3+10; const int M = 1e6+10; struct node {int v,next;int u; } s[M]; int ans ; int n,a,b; int dp[N]; int top,num; int dfn[N],low[N]; int cut[N];//標記是否為割點 int id; void tarjan(int x,int f) //當前結點以及父節點 {low[x] = dfn[x] = ++id; //(1)結點u初值int son = 0; //定義根結點孩子個數for(int i = dp[x]; ~i; i= s[i].next)//(2)枚舉每一條邊{int v = s[i].v;//(3)入過結點u為強連通分量的根節點時if(!dfn[v]) //(4)沒有被訪問時候遞歸搜索{tarjan(v,x);low[x] = min(low[x],low[v]);if(low[v] >= dfn[x] && x != f) //(5)如果該子節點不能到達祖先節點 (標記)cut[x] = 1;if(x == f)//每從根結點往孩子走就加1son++;}low[x] = min(low[x],dfn[v]);}if(x == f && son >= 2) //(6)根節點且孩子個數大于2即為割點cut[x] = 1; } void add(int u,int v) {s[top].v = v;s[top].next = dp[u];dp[u] = top++; } void init() {memset(dp,-1,sizeof(dp));memset(cut,0,sizeof(cut));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));id = ans = top = 0; } int main() {while(~scanf("%d",&n) &&n){init();while(scanf("%d",&a) &&a){char ch;while(scanf("%d%c",&b,&ch)){add(a,b),add(b,a);if(ch == '\n')break;}}for(int i = 1; i<=n; i++){if(!dfn[i])tarjan(i,i);}for(int i = 1; i<=n; i++){if(cut[i])ans++;}printf("%d\n",ans);}return 0; }

總結

以上是生活随笔為你收集整理的Network UVA - 315(Tarjan+连通性问题:求割点)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。