POJ 1523

无向图求割点模板题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string.h>
 4 using namespace std;
 5 const int N=1002;
 6
 7 int subnets[N];
 8 int dfn[N],low[N];
 9 int count,mun,son,ks;
10
11 bool map[N][N];
12 bool vis[N];
13
14 void init(){
15     memset(map,0,sizeof(map));
16     for(int i=0;i<=N;++i){
17         vis[i]=dfn[i]=low[i]=subnets[i]=0;
18     }
19     count=1,son=0;
20     mun=0;
21     dfn[1]=low[1]=vis[1]=1;
22 }
23 void dfs(int x){
24     for(int i=1;i<=mun;i++){
25         if(map[x][i]){
26             if(!vis[i]){
27                 vis[i]=1;
28                 dfn[i]=low[i]=++count;
29                 dfs(i);
30                 low[x]=min(low[x],low[i]);
31                 if(low[i]>=dfn[x]){
32                     if(x==1)son++;
33                     else subnets[x]++;
34                 }
35             }
36             else low[x]=min(low[x],dfn[i]);
37         }
38     }
39 }
40 void print(){
41     bool yes=0;
42     if(son>1)subnets[1]=son-1;
43     printf("Network #%d\n",++ks);
44     for(int i=1;i<=mun;i++){
45         if(subnets[i]){
46             yes=1;
47             printf("  SPF node %d leaves %d subnets\n",i,subnets[i]+1);
48         }
49     }
50     if(!yes)printf("  No SPF nodes\n");
51     printf("\n");
52 }
53 int main(){
54     //freopen("test.txt","r",stdin);
55     int a,b;
56     ks=0;
57     while(scanf("%d",&a)&&a){
58         init();
59         scanf("%d",&b);
60         map[a][b]=map[b][a]=1;
61         mun=max(max(b,a),mun);
62         while(scanf("%d",&a)&&a){
63             scanf("%d",&b);
64             map[a][b]=map[b][a]=1;
65             mun=max(max(b,a),mun);
66         }
67         dfs(1);
68         print();
69     }
70     return 0;
71 }

POJ 1523

时间: 2024-07-30 06:34:36

POJ 1523的相关文章

SPF(poj 1523) 割点入门

1 /***************************************** 2 SPF(poj 1523) 3 割点入门 4 http://poj.org/problem?id=1523 5 6 ******************************************/ 7 8 #include<iostream> 9 #include<algorithm> 10 #include<cstdio> 11 #include<cstring&

POJ 1523 SPF(强连通分量求割点)

题目地址:POJ 1523 这题猛的一看..貌似有点难的样子.不过仔细一想,那个每个割点所分成一次子图不就都能找到这个割点一次吗,那么只要记录下它作为割点的次数再+1不就行了.也算是求割点的裸题吧.这个题的输出很坑...需要注意一下.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #incl

POJ 1523 网络连通

题目大意: 给你一个网络组,每台机子与其他机子的关系,让你找到所有的割点,如果没有割点,输出无 这道题目就是最直接的求割点问题,我在这里用的是邻接矩阵来存储机子之间的关系 割点问题的求解需要对深度优先搜索序数有比较好的理解 dfn[]用于存储当前的优先搜索序数,low[]存储当前点通过子节点或是回路所能达到的最小优先搜索序数 当(u,v)一组边dfn[u]<=low[v]时,那么u就是一个割点(这是u不作为树的顶点时的情况) u作为顶点时,也即dfn[u]==1时,要看u的儿子节点数,大于等于2

poj 1523 SPF

SPF http://poj.org/problem?id=1523 Time Limit: 1000MS   Memory Limit: 10000K       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 fail

【POJ 1523】SPF(割点)

儿子数大于1的树根或者 Low[v] >= DFN[u]的非树根节点v 就是割点. #include <cstdio> #include <cstring> const int N = 1001; const int M = 1000010; struct Edge { int to,next; bool cut;//是否为桥的标记 }edge[M]; int head[N],tot; int Low[N],DFN[N],Stack[N]; int Index,top; bo

POJ 1523 SPF 解题报告

思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一下讨论: 如果这个割点是根节点,在tarjan算法中搜到几个孩子结点(low[v] >= dfn[u]),他就能割出几个联通块,如果这个割点是孩子结点,那么他所形成的联通块的个数+1,因为他还有一条与父亲结点间接或直接相连的边. 代码如下: #include<map> #include<

poj 1523 SPF【点双连通求去掉割点后bcc个数】

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7246   Accepted: 3302 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

POJ 1523 SPF(无向图割顶)

SPF 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 s

poj 1523 SPF 无向图求割点

SPF 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 s