TOJ 2018 SPF(连通图割点和分成的连通块)
Posted taozi1115402474
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOJ 2018 SPF(连通图割点和分成的连通块)相关的知识,希望对你有一定的参考价值。
描述
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.
输入
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.
输出
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.
样例输入
1 2
5 4
3 1
3 2
3 4
3 5
0
1 2
2 3
3 4
4 5
5 1
0
1 2
2 3
3 4
4 6
6 3
2 5
5 1
0
0
样例输出
Network #1
SPF node 3 leaves 2 subnets
Network #2
No SPF nodes
Network #3
SPF node 2 leaves 2 subnets
SPF node 3 leaves 2 subnets
题意
给你一个连通图,求所有割点,和去掉割点共分成几个连通图
题解
割点直接targin,求分成的连通图可以dfs整个图
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int N=1e4+5; 5 6 vector<int>G[N]; 7 int dfn[N],low[N],tot; 8 bool cut[N],vis[N]; 9 void tarjan(int u,int fa) 10 { 11 int child=0; 12 dfn[u]=low[u]=++tot; 13 for(int i=0;i<G[u].size();i++) 14 { 15 int v=G[u][i]; 16 if(!dfn[v]) 17 { 18 tarjan(v,u); 19 low[u]=min(low[u],low[v]); 20 if(dfn[u]<=low[v]&&u!=fa)cut[u]=true; 21 if(u==fa)child++; 22 } 23 low[u]=min(low[u],dfn[v]); 24 } 25 if(u==fa&&child>=2)cut[u]=true; 26 } 27 void init(int n) 28 { 29 tot=0; 30 for(int i=0;i<=n;i++) 31 { 32 G[i].clear(); 33 dfn[i]=low[i]=0; 34 cut[i]=false; 35 } 36 } 37 void dfs(int u,int no) 38 { 39 for(int i=0;i<(int)G[u].size();i++) 40 { 41 int v=G[u][i]; 42 if(!vis[v]&&v!=no) 43 vis[v]=true,dfs(v,no); 44 } 45 } 46 int main() 47 { 48 int u[N],v[N],TOT=0,o=1; 49 while(scanf("%d",&u[TOT])!=EOF,u[TOT]) 50 { 51 scanf("%d",&v[TOT]); 52 TOT=1; 53 while(scanf("%d",&u[TOT])!=EOF,u[TOT]) 54 { 55 scanf("%d",&v[TOT]); 56 TOT++; 57 } 58 int n=1; 59 for(int i=0;i<TOT;i++) 60 n=max(n,max(u[i],v[i])); 61 init(n); 62 for(int i=0;i<TOT;i++) 63 G[u[i]].push_back(v[i]), 64 G[v[i]].push_back(u[i]); 65 tarjan(1,1); 66 if(o-1)printf(" "); 67 printf("Network #%d ",o++); 68 vector<int>CUT; 69 for(int i=1;i<=n;i++)if(cut[i])CUT.push_back(i); 70 if((int)CUT.size()==0) 71 printf(" No SPF nodes "); 72 else 73 { 74 for(int i=0;i<(int)CUT.size();i++) 75 { 76 memset(vis,false,sizeof vis); 77 int cnt=0; 78 for(int j=1;j<=n;j++) 79 { 80 if(j==CUT[i])continue; 81 if(!vis[j]) 82 cnt++,vis[j]=true,dfs(j,CUT[i]); 83 } 84 printf(" SPF node %d leaves %d subnets ",CUT[i],cnt); 85 } 86 } 87 } 88 return 0; 89 }
以上是关于TOJ 2018 SPF(连通图割点和分成的连通块)的主要内容,如果未能解决你的问题,请参考以下文章