POJ 1523 SPF(割点所割连通分量数)

来源:互联网 发布:网络高清摄像头安装方法 编辑:程序博客网 时间:2024/05/01 06:24

题意给你一个无向图,问你该图中有多少割点.且每个割点能把该图分为几个连通分量

思路:令cut[i]为结点i割了之后生成的儿子数目,如果为根节点,那么如果儿子数大于1的话才算是割点,其他的割点最后分割出来的连通分量数为cut[i]+1


#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 10005#define LL long longint cas=1,T;int n,m;int sum = 0;vector <int> ans;int dfs_clock;            //时钟,每访问一个结点增1vector<int>G[maxn];       //图int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值bool iscut[maxn];         //标记i结点是不是一个割点int cut[maxn];            //切割这个结点后把连通块切成多少份//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥//初始调用dfs(root,-1)int dfs(int u,int fa){int lowu=pre[u]=++dfs_clock;int child = 0;                //子结点数目for (int i = 0;i<G[u].size();i++){int v = G[u][i];if (!pre[v]){child++;              //未访问过的结点才能算是u的孩子int lowv = dfs(v,u);lowu = min(lowu,lowv);if (lowv >=pre[u]){iscut[u]=1;           //u是割点cut[u]++;//if (lowv > pre[u])       //(u,v)边时桥//printf("qiao")}}else if (pre[v] <pre[u] && v!=fa)  //v!=fa确保了(u,v)是从u到v的反向边{lowu = min(lowu,pre[v]);}}if (fa < 0 && child == 1)   //若u是根且孩子数<=1,那么u就不是割点{}       else if (fa<0 && child>1){        ans.push_back(u);}else if (cut[u]>=1)ans.push_back(u),cut[u]++;return low[u]=lowu;}void init(){dfs_clock = 0;sum=0;memset(pre,0,sizeof(pre));memset(iscut,0,sizeof(iscut));memset(cut,0,sizeof(cut));for (int i = 0;i<=maxn;i++)G[i].clear();ans.clear();}int main(){int u,v;while (scanf("%d",&u)&& u){init();        while (1){scanf("%d",&v);G[u].push_back(v);G[v].push_back(u);scanf("%d",&u);if (!u)break;}for (int i = 1;i<=1000;i++){if (!pre[i] && G[i].size()>0){                dfs(i,-1);}}sort(&ans[0],&ans[0]+ans.size());    if (ans.size()>0){printf("Network #%d\n",cas++);for (int i = 0;i<ans.size();i++)printf("  SPF node %d leaves %d subnets\n",ans[i],cut[ans[i]]);}elseprintf("Network #%d\n  No SPF nodes\n",cas++);puts("");}//freopen("in","r",stdin);//scanf("%d",&T);//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}


题目

Description

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 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100

Sample Output

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


0 0