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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)

發(fā)布時(shí)間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.?

Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.?

Input

The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

Output

For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist.?

The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.

Sample Input

1 2 5 4 3 1 3 2 3 4 3 5 01 2 2 3 3 4 4 5 5 1 01 2 2 3 3 4 4 6 6 3 2 5 5 1 00

Sample Output

Network #1SPF node 3 leaves 2 subnetsNetwork #2No SPF nodesNetwork #3SPF node 2 leaves 2 subnetsSPF node 3 leaves 2 subnets

題目大意:

給你一個(gè)聯(lián)通網(wǎng)路(應(yīng)該是無自環(huán)無回路的),求出這個(gè)網(wǎng)絡(luò)所有割點(diǎn)的編號(hào),以及如果刪除這個(gè)割點(diǎn)之后所對(duì)應(yīng)的聯(lián)通分量數(shù)。

解題報(bào)告:

? 就是個(gè)板子題,,就是輸出格式比較坑。。

? 再就是注意幾個(gè)地方,一個(gè)是if(dfn[x]==0)的時(shí)候的low[x]別忘更新,第二是son++要在if(dfn[x]==0)這個(gè)判斷內(nèi)。(不然的話就算下面的if(x == rt && son > 1)改成son>2也不行。)

再就是一開始寫的時(shí)候把init函數(shù)放到n=max(a,b)后面了,這樣肯定不對(duì)啊你都add了兩條邊了然后又給人家清空了,,還好樣例比較良心直接就能發(fā)現(xiàn),不然這個(gè)小錯(cuò)又得找半天錯(cuò)。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; struct Edge {int u,v;int ne; } e[MAX]; int dfn[MAX],low[MAX],clk; int head[MAX],tot; int gd[MAX]; int n; void init() {for(int i = 1; i<=1000; i++) {dfn[i]=low[i]=gd[i]=0;head[i] = -1;}tot = 0;clk = 0; } void add(int u,int v) {e[++tot].u = u;e[tot].v = v;e[tot].ne = head[u];head[u] = tot; } void tarjan(int x,int fa,int rt) {//注意這里fa和rt是不一樣的含義!! dfn[x] = low[x] = ++clk;int son = 0;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].v;if(v == fa) continue;if(dfn[v] == 0) {son++;//放到dfn[v]==0這個(gè)if外面也可以??這兩種都可以?? 答:不可以 tarjan(v,x,rt);low[x] = min(low[x],low[v]);//別忘這一步啊傻不傻、、if(x != rt && low[v] >= dfn[x]) {gd[x]++;}}else low[x] = min(low[x],dfn[v]);}if(x == rt && son > 1) {gd[x] = son-1;} } int main() {int a,b,iCase=0;while(~scanf("%d",&a) && a) {scanf("%d",&b);init();add(a,b);add(b,a);n=max(a,b); while(~scanf("%d",&a) && a) {scanf("%d",&b);add(a,b);add(b,a);n=max(n,a);n=max(n,b);}tarjan(1,-1,1);printf("Network #%d\n",++iCase);int flag = 0;for(int i = 1; i<=n; i++) {if(gd[i] != 0) printf(" SPF node %d leaves %d subnets\n",i,gd[i]+1),flag = 1;}if(flag == 0) printf(" No SPF nodes\n");printf("\n");}return 0 ; }

總結(jié)

以上是生活随笔為你收集整理的【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。