NOJ——1274The battle of Red Cliff(并查集按秩合并)
来源:互联网 发布:淘宝的延长收货是几天 编辑:程序博客网 时间:2024/04/30 02:24
[1274] The battle of Red Cliff
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
Zero loves palying Sanguo game very much, The battle of Red Cliff is one of scenes. Now he will put a fire to one of ships. Some ships are connected, some are not.Now, zero puts his fire, please tell him how many ships will be left.
- 输入
- Input until EOF.
There are two positive integers N(2<N<100) and M(0<M<100), N means the number of ships, M means the number of command.
Then M lines follows. Each line has two positive integers x and y means he will connect these two ships by a rope.
Last line will include a number of ship's that zero will burn. - 输出
- The ship's number is from 1 N, every ship which is connected a burning ship will also be burned.Now, please tell zero how many ships will be left.
- 样例输入
10 51 21 31 41 51 615 21 21 32
- 样例输出
42
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<stack>#include<cmath>#include<queue>#include<set>#include<map>using namespace std;typedef long long LL;#define INF 0x3f3f3f3f#define MM(a) memset(a,0,sizeof(a))int pos[1100];int rank[1100];inline int find(int x){if(x!=pos[x])return pos[x]=find(pos[x]);return pos[x];}inline void joint(int a,int b){int aa=find(a),bb=find(b);if(aa!=bb){if(rank[aa]<rank[bb]){pos[aa]=bb;rank[bb]+=rank[aa];}else{pos[bb]=aa;rank[aa]+=rank[bb];}}}int main(void){int m,n,i,ans,x,y,t;while (~scanf("%d%d",&n,&m)){MM(pos);MM(rank);for (i=0; i<1100; i++){pos[i]=i;rank[i]=1;}for (i=1; i<=m; i++){scanf("%d%d",&x,&y);joint(x,y);}scanf("%d",&t);ans=0;t=find(t);printf("%d\n",n-rank[t]);}return 0;}
0 0
- NOJ——1274The battle of Red Cliff(并查集按秩合并)
- NOJ [1274] The battle of Red Cliff
- NOJ——1508火烧赤壁2(并查集+启发式合并+逆序加边)
- HDU5542 The Battle of Chibi (DP)
- The Battle of Guandu
- NOJ——1559Jump to the Top of Mountain(简单暴力DFS+渣渣代码)
- HDU5545-The Battle of Guandu
- HDU5542-The Battle of Chibi
- The Battle of Chibi HDU
- HDU 5542 The Battle of Chibi——树状数组 + dp
- Tales of Mystery and Imagination——3、 The Masque of the Red Death
- HDU 5542 The Battle of Chibi(DP+树状数组)
- HDU 5545 The Battle of Guandu(最短路)
- HDU 5545 The Battle of Guandu (最短路Dijkstra)
- HDU 5542 The Battle of Chibi(树状数组+dp)
- HDU 5542 The Battle of Chibi(dp+树状数组)
- NOJ 1317——Exercise of Sort
- NOJ——[1143] Irrigation of Farmland
- QT中实现定时器
- JAVA反射(一)获取Class对象的三种方式及其区别
- 文章标题
- OBIEE提示器中出现为空的一条记录
- 运输问题[网络流24题之17]
- NOJ——1274The battle of Red Cliff(并查集按秩合并)
- Dijkstra算法升级
- 文章标题
- Spark内存管理-UnifiedMemoryManager和StaticMemoryManager
- springMVC(11)------数据列表显示
- 文章标题
- 线段树统计树
- 浙大研究生复数上机考试 第4题
- 蘑菇阵