SPF
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7678   Accepted: 3489

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. 1 2
  2. 5 4
  3. 3 1
  4. 3 2
  5. 3 4
  6. 3 5
  7. 0
  8.  
  9. 1 2
  10. 2 3
  11. 3 4
  12. 4 5
  13. 5 1
  14. 0
  15.  
  16. 1 2
  17. 2 3
  18. 3 4
  19. 4 6
  20. 6 3
  21. 2 5
  22. 5 1
  23. 0
  24.  
  25. 0

Sample Output

  1. Network #1
  2. SPF node 3 leaves 2 subnets
  3.  
  4. Network #2
  5. No SPF nodes
  6.  
  7. Network #3
  8. SPF node 2 leaves 2 subnets
  9. SPF node 3 leaves 2 subnets

题意: 求割点 同时求出删除当前点后,分成几个连通分量。

思路:

tarjan算法求割点。 tarjan处理强连通 其实都是基于dfs的。同时维护2个数组。 dfn[] 和 low[] 分别表示第i点时的深度,和通过能够到达的祖先的深度。

求强连通的时候,如果low[i] == dfn[i] 说明栈内当前点以上的点 都是强连通块里面的。也就是说i的子树中不能到达i的祖先。

在求割点的时候,如果low[v] > dfn[u](v是u的子节点)说明v不能到达u的祖先 说明删除u后 v的子树从原图中分离。

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<string>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<algorithm>
  12. #define INF 1000000001
  13. #define MOD 1000000007
  14. #define ll long long
  15. #define lson l,m,rt<<1
  16. #define rson m+1,r,rt<<1|1
  17. #define pi acos(-1.0)
  18. using namespace std;
  19. const int MAXN = ;
  20. struct node
  21. {
  22. int to;
  23. int next;
  24. }edge[MAXN*];
  25. int ind,pre[MAXN],dfn[MAXN],low[MAXN],num[MAXN],n,vis[MAXN];
  26. void add(int x,int y)
  27. {
  28. edge[ind].to = y;
  29. edge[ind].next = pre[x];
  30. pre[x] = ind ++;
  31. }
  32. void dfs(int rt,int d)
  33. {
  34. vis[rt] = ;
  35. dfn[rt] = low[rt] = d;
  36. for(int i = pre[rt]; i != -; i = edge[i].next){
  37. int t = edge[i].to;
  38. if(!vis[t]){
  39. dfs(t,d + );
  40. low[rt] = min(low[rt],low[t]);
  41. if(low[t] >= dfn[rt]){
  42. num[rt] ++;
  43. }
  44. }
  45. else {
  46. low[rt] = min(low[rt],dfn[t]);
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. int x,y,ff = ;
  53. while(){
  54. n = ;
  55. scanf("%d",&x);
  56. if(!x)break;
  57. scanf("%d",&y);
  58. ind = , memset(pre,-,sizeof(pre));
  59. add(x,y), add(y,x);
  60. n = max(x,y);
  61. while(){
  62. scanf("%d",&x);
  63. if(!x)break;
  64. scanf("%d",&y);
  65. n = max(n,x);
  66. n = max(n,y);
  67. add(x,y), add(y,x);
  68. }
  69. memset(vis,,sizeof(vis));
  70. memset(dfn,,sizeof(dfn));
  71. memset(low,,sizeof(low));
  72. memset(num,,sizeof(num));
  73. dfs(,);
  74. int flag = ;
  75. if(ff >= )printf("\n");
  76. printf("Network #%d\n",++ff);
  77. num[] = num[] ? num[] - : ;
  78. for(int i = ; i <= n; i++){
  79. if(num[i]){
  80. flag = ;
  81. printf(" SPF node %d leaves %d subnets\n",i,num[i] + );
  82. }
  83. }
  84. if(!flag){
  85. printf(" No SPF nodes\n");
  86. }
  87. }
  88. return ;
  89. }

poj1523 求割点 tarjan的更多相关文章

  1. poj1523求割点以及割后连通分量数tarjan算法应用

    无向图,双向通道即可,tarjan算法简单应用.点u是割点,条件1:u是dfs树根,则u至少有2个孩子结点.||条件2:u不是根,dfn[u]=<low[v],v是u的孩子结点,而且每个这样的v ...

  2. zoj 1119 / poj 1523 SPF (典型例题 求割点 Tarjan 算法)

    poj : http://poj.org/problem?id=1523 如果无向图中一个点 u 为割点 则u 或者是具有两个及以上子女的深度优先生成树的根,或者虽然不是一个根,但是它有一个子女 w, ...

  3. uva 315 Network(无向图求割点)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. poj 1523 SPF(tarjan求割点)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  5. Tarjan求割点和桥

    by szTom 前置知识 邻接表存储及遍历图 tarjan求强连通分量 割点 割点的定义 在一个无向图中,如果有一个顶点集合,删除这个顶点集合以及这个集合中所有顶点相关联的边以后,图的连通分量增多, ...

  6. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  7. [学习笔记]tarjan求割点

    都口胡了求割边,就顺便口胡求割点好了QAQ 的定义同求有向图强连通分量. 枚举当前点的所有邻接点: 1.如果某个邻接点未被访问过,则访问,并在回溯后更新 2.如果某个邻接点已被访问过,则更新 对于当前 ...

  8. tarjan算法求割点cojs 8

    tarjan求割点:cojs 8. 备用交换机 ★★   输入文件:gd.in   输出文件:gd.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] n个城市之间有通讯网 ...

  9. UESTC 900 方老师炸弹 --Tarjan求割点及删点后连通分量数

    Tarjan算法. 1.若u为根,且度大于1,则为割点 2.若u不为根,如果low[v]>=dfn[u],则u为割点(出现重边时可能导致等号,要判重边) 3.若low[v]>dfn[u], ...

随机推荐

  1. POJ3013 Big Christmas Tree[转换 最短路]

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5 ...

  2. USACO1.1Broken Necklace[环状DP作死]

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  3. Carcraft

    魔兽登录系统   创建魔兽系统相关窗体: 登录窗体(frmLogin) 注册窗体(frmRegister) 主窗体   (frmMain) 实现魔兽登录系统: 登录的界面如下 实现思路: 1.创建一个 ...

  4. 社交化分享SDK for Unity

    前言 社交化分享,即分享到社交网络. 本文主要记录的是在Unity集成社交化分享SDK,现主流的分享SDK有如下: 1.友盟社交化分享 for unity 2.ShareSDK分享 for unity ...

  5. 关于OAUTH2.0的极品好文

    Web Server Flow: web ServerFlow是把oauth1.0的三个步骤缩略为两个步骤 首先这个是适合有server的第三方使用的. 1客户端http请求authorize 2服务 ...

  6. WAMP中phpMyAdmin登陆不了问题的解决方法

    WAMP中phpMyAdmin登陆不了问题的解决方法

  7. S2结业考试的第一次测验

    错题分析: 1:java中的错误处理是通过异常处理模型来实现的,那么异常处理模块能处理的错误是: A:运行时错误 B:逻辑错误 C:语法错误 D:内部错误 正确答案是:A 解析:运行时异常都是Runt ...

  8. ConcurrentHashMap是如何提高并发时的吞吐性能

    为并发吞吐性能所做的优化 ConcurrentHashMap使用了一些技巧来获取高的并发性能,同时避免了锁.这些技巧包括: 为不同的Hash bucket(所谓hash bucket即不同范围的key ...

  9. 隐写技巧——利用JPEG文件格式隐藏payload

    0x00 前言 继续对图片隐写技巧的学习,这次是对JPEG文件格式的学习和理解.同PNG文件的格式对比,JPEG文件相对简单,读取其中隐藏payload的方式大同小异,两者区别在于文件格式不同,可供利 ...

  10. StringBuffer and StringBuilder

    As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a ...