SPF POJ - 1523 割点+并查集
题意:
问你这个图中哪个点是割点,如果把这个点去掉会有几个子网
代码:
1 //给你几个点,用着几个点形成了一个图。输入边形成的图,问你这个图中有多少个割点。每一个割点去掉后会形成几个强连通分量
2 //方法:
3 //你可以想用tarjan算法求出来谁是割点。然后每一个割点单独用并查集判断。用所有边(不和这个割点相连的边)通过并查集判断一下
4 //然合并后有几个区域就可以了
5 #include<stdio.h>
6 #include<string.h>
7 #include<algorithm>
8 using namespace std;
9 const int spot=1010;
10 const int edge=500010;
11 struct node
12 {
13 int u,v;
14 }m[edge];
15 struct stu
16 {
17 int to,next;
18 } a[edge];
19 int head[spot],fa,low[spot],num[spot],root,indexx,sizes,flag1,n,fx[spot],qua,len,v[spot]; //fx指祖节点
20 bool flag[spot],flag2[spot],b[spot],have[spot];
21 void init()
22 {
23 indexx=0,sizes=0,flag1=1,n=0;
24 len=qua=0;
25 memset(head,-1,sizeof(head));
26 memset(low,0,sizeof(low));
27 memset(num,0,sizeof(num));
28 memset(flag,0,sizeof(flag));
29 memset(have,0,sizeof(have));
30 }
31 void add_edge(int from,int to) //并查集
32 {
33 a[sizes].to=to;
34 a[sizes].next=head[from];
35 head[from]=sizes++;
36 a[sizes].to=from;
37 a[sizes].next=head[to];
38 head[to]=sizes++;
39 n=max(n,from);
40 n=max(n,to);
41 m[len].u=from;
42 m[len++].v=to;
43 }
44 void dfs(int cur) //求割点
45 {
46 int child=0;
47 num[cur]=++indexx;
48 low[cur]=indexx;
49 int k,v;
50 for(k=head[cur]; k+1; k=a[k].next)
51 {
52 v=a[k].to;
53 if(!num[v])
54 {
55 dfs(v);
56 low[cur]=min(low[cur],low[v]);
57 if(low[v]>=num[cur])
58 {
59 child++;
60 if(cur!=fa || child>1) flag[cur]=1,qua++;
61 }
62 }
63 else
64 low[cur]=min(low[cur],num[v]);
65 }
66 }
67 int finds(int x)
68 {
69 if(x!=v[x])
70 {
71 int y=finds(v[x]);
72 return v[x]=y;
73 }
74 return x;
75 }
76 int panduan(int x)
77 {
78 for(int i=1;i<=n;++i)
79 {
80 if(have[i]) v[i]=i;
81 }
82 for(int i=0;i<len;++i)
83 {
84 if(m[i].u!=x && m[i].v!=x)
85 {
86 int fx=finds(m[i].u);
87 int fy=finds(m[i].v);
88 if(fx!=fy) v[fx]=fy;
89 }
90 }
91 int ans=0;
92 for(int i=1;i<=n;++i)
93 {
94 if(i!=x && v[i]==i) ++ans;
95 }
96 return ans;
97 }
98 int main()
99 {
100 int x,y,i,ss=0,ans,j;
101 while(scanf("%d",&x)&&x)
102 {
103 init();
104 scanf("%d",&y);
105 root=x;
106 have[x]=1;
107 have[y]=1;
108 add_edge(x,y);
109 for(;;)
110 {
111 scanf("%d",&x);
112 if(!x) break;
113 scanf("%d",&y);
114 add_edge(x,y);
115 have[x]=1,have[y]=1;
116 }
117 fa=root;
118 dfs(root);
119 printf("Network #%d\n",++ss);
120 for(i=1; i<=n; i++)
121 {
122 if(flag[i])
123 {
124 flag1=0;
125 int q=panduan(i);
126 printf(" SPF node %d leaves %d subnets\n",i,q);
127 }
128 }
129 if(flag1)
130 printf(" No SPF nodes\n");
131 printf("\n");
132 }
133 return 0;
134 }
SPF POJ - 1523 割点+并查集的更多相关文章
- Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题
Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...
- poj 2524 (并查集)
http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...
- [POJ 2588]--Snakes(并查集)
题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS Memory Limit: 65536K Description B ...
- poj 1456 Supermarket - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- Poj(1182),种类并查集
题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...
- Poj(1703),种类并查集
题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- IO软件层次结构与假脱机技术
IO软件层次结构 用户层软件->设备独立性软件->设备驱动软件->中断处理程序->硬件 用户层软件实现与用户交互的接口,用户可直接使用该层提供的,与IO操作相关的库函数对设备进 ...
- 九:APP及其他资产
APP提取一键反编译提取 APP抓数据包进行工具配合 各种第三方应用相关探针技术 各种服务器接口相关探针技术 APP提取及抓包及后续配合 某APK一键提取反编译 利用burp历史抓更多URL 某IP无 ...
- Session、Cookie与Token
http协议是无状态协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到 ...
- kubernets之向外部应用暴露应用
一 通过NodePort来暴露服务 前面已经介绍的服务的一些作用,例如将集群内部的应用暴露给集群内部的pod使用,将外部的应用通过服务暴露给内部应用使用,但是服务最大的作用不仅仅是这些 而是将集群内 ...
- LuoguP5488 差分与前缀和
题意 给定一个长为\(n\)的序列\(a\),求出其\(k\)阶差分或前缀和.结果的每一项都需要对\(1004535809\)取模. 打表找规律 先看前缀和,设\(n=5\),\(k=4\),按照阶从 ...
- 转 7 jmeter之参数化
7 jmeter之参数化 badboy里参数化(前面4 jmeter badboy脚本开发技术详解已讲过) jmeter里参数化-1 用户参数 1.打开badboy工具,点击红色按钮开始录制,在地 ...
- JavaScript中的异步函数
JavaScript中的异步函数 ES8 的 async/await 旨在解决利用异步结构组织代码的问题.为此, ECMAScript 对函数进行了扩展,为其增加了两个新关键字: async 和 aw ...
- How Load Balancing Policies Work
How Load Balancing Policies Work https://docs.cloud.oracle.com/en-us/iaas/Content/Balance/Reference/ ...
- @functools.lru_cache()
django.views.debug.get_default_exception_reporter_filter @functools.lru_cache()def get_default_excep ...
- loj1011愤怒的牛
题目描述 原题来自:USACO 2005 Feb. Gold 农夫约翰建造了一座有 n 间牛舍的小屋,牛舍排在一条直线上,第 i 间牛舍在 x_i 的位置,但是约翰的 m 头牛对小屋很不满意,因此经常 ...