POJ 1523 SPF 求割点的好(板子)题!

题意:

给个无向图,问有多少个割点,对于每个割点求删除这个点之后会产生多少新的点双联通分量



题还是很果的

怎么求割点请参考tarjan无向图

关于能产生几个新的双联通分量,对于每个节点u来说,我们判断他是否是割点,即判断是否满足他的儿子v的low[v]>dfn[u]

而这个时候割掉这个点就会让双联通分量增加,所以搞一个数组记录一下这个操作的次数就行

请注意在是否是根节点的问题上特判

!!注意输出格式!!

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define N 5010
 5 #define M 10100
 6 #define Max(a,b,c) max(max(a,b),c)
 7 using namespace std;
 8 int head[N],n,m,ecnt=2,u,v,dfn[N],low[N],indx,du[N],ans,isap[N],ap,task,sum[N];
 9 struct edge
10 {
11     int u,v,nxt;
12 }e[M*2];
13 void add(int u,int v)
14 {
15     e[ecnt].v=v;
16     e[ecnt].nxt=head[u];
17     e[ecnt].u=u;
18     head[u]=ecnt++;
19     e[ecnt].v=u;
20     e[ecnt].nxt=head[v];
21     e[ecnt].u=v;
22     head[v]=ecnt++;
23 }
24 void dfs(int u,int fa)
25 {
26     dfn[u]=low[u]=++indx;
27     int n_ch=0;
28     for (int i=head[u];i;i=e[i].nxt)
29     {
30     int v=e[i].v;
31     if (!dfn[v])
32     {
33         dfs(v,i);
34         low[u]=min(low[u],low[v]);
35         if (low[v]>=dfn[u])
36         isap[u]=1,ap++,sum[u]++;
37         n_ch++;
38     }
39     else
40         if (v!=fa)
41         low[u]=min(dfn[v],low[u]);
42     }
43     if (fa==-1 && n_ch==1)
44     isap[u]=0,ap--;
45     if (isap[u] && fa!=-1) sum[u]++;
46 }
47 void init()
48 {
49     n=0;
50     memset(sum,0,sizeof(sum));
51     memset(head,0,sizeof(head));
52     memset(dfn,0,sizeof(dfn));
53     memset(isap,0,sizeof(isap));
54     ecnt=2;
55     ans=0;
56     indx=0;
57     ap=0;
58 }
59 void solve()
60 {
61     if (n==0) return;
62     task++;
63         for (int i=1;i<=n;i++)
64     if (!dfn[i]) dfs(i,-1);
65     printf("Network #%d\n",task);
66     if (ap==0)
67     printf("  No SPF nodes\n");
68     for (int i=1;i<=n;i++)
69     if (isap[i])
70         printf("  SPF node %d leaves %d subnets\n",i,sum[i]);
71     putchar(‘\n‘);
72     init();
73 }
74 int main()
75 {
76     //  freopen("1.in","r",stdin);
77     while (1)
78     {
79     while (scanf("%d",&u)!=EOF)
80     {
81         if (u==0)
82         {
83         solve();
84         continue;
85         }
86         if (scanf("%d",&v)==EOF) break;
87         n=Max(n,u,v);
88         add(u,v);
89         //   printf("%d %d\n",u,v);
90     }
91     if (n==0) break;
92     }
93     return 0;
94 }
时间: 2025-01-08 04:32:19

POJ 1523 SPF 求割点的好(板子)题!的相关文章

poj 1523 SPF 求割点以及删除该割点后联通块的数量

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7136   Accepted: 3255 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(割点)

大致题意: 给出一个连通的无向图,求哪些点是割点,对于每个割点,求出去掉这个点后连通分量的个数.如果没有割点的话输出" No SPF nodes". 思路: 求割点用tarjan即可,然后要求删除割点后连通分量的个数,每次找到割点,再在从割点dfs即可,也可以直接在tarjan算法中记录child个数.那么如果割点是根,那么答案就是child,不是根的割点答案是child+1 //188K 0MS C++ 2121B #include<cstdio> #include<

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

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

POJ 1523 SPF 割点 Tarjan

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9317   Accepted: 4218 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 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 1144 无向图求割点

学长写的: #include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define maxn 10005int dfn[maxn];///代表最先遍历到这个点的时间int low[maxn];///这个点所

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【点双连通求去掉割点后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(割点)

题目大意:给出一张图,问去割点后,连通分量的个数有多少 解题思路:割点的水题,套模版就可以 不得不吐槽一下输入.. #include <cstdio> #include <cstring> #define min(a,b) ((a)<(b)?(a):(b)) #define N 1010 #define M 2000010 struct Edge{ int to, next; }E[M]; int head[N], num[N], pre[N], lowlink[N]; in