【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)
題干:
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 00Sample Output
Network #1SPF node 3 leaves 2 subnetsNetwork #2No SPF nodesNetwork #3SPF node 2 leaves 2 subnetsSPF node 3 leaves 2 subnets題目大意:
給你一個聯通網路(應該是無自環無回路的),求出這個網絡所有割點的編號,以及如果刪除這個割點之后所對應的聯通分量數。
解題報告:
? 就是個板子題,,就是輸出格式比較坑。。
? 再就是注意幾個地方,一個是if(dfn[x]==0)的時候的low[x]別忘更新,第二是son++要在if(dfn[x]==0)這個判斷內。(不然的話就算下面的if(x == rt && son > 1)改成son>2也不行。)
再就是一開始寫的時候把init函數放到n=max(a,b)后面了,這樣肯定不對啊你都add了兩條邊了然后又給人家清空了,,還好樣例比較良心直接就能發現,不然這個小錯又得找半天錯。
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這個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 ; }總結
以上是生活随笔為你收集整理的【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自研6nm芯片下放!OPPO Reno8
- 下一篇: 【HDU - 5455】Fang Fan