NOJ——1274The battle of Red Cliff(并查集按秩合并)
2024-09-04 17:28:36
[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 5
1 2
1 3
1 4
1 5
1 6
1
5 2
1 2
1 3
2- 样例输出
4
2
WA数次,后来换了个思路,直接求被烧掉的船所在集合的元素个数即可。答案就是总船数减掉集合元素个数。
代码:
#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;
}
最新文章
- 浅谈Web自适应
- Eclipse快捷键与使用技巧总结
- 几年前做家教写的C教程(之五专讲结构体与文件操作)
- CGFloat Float 互转
- 【2016-10-13】【坚持学习】【Day4】【模板方法模式】
- 清北学堂模拟day4 业务办理
- 如果在Xcode6中创建空工程, 删除冗余信息
- Django如何设置proxy
- 50个Android开发技巧(11 为文字加入特效)
- No curses/termcap library found
- *** Terminating app due to uncaught exception &#39;NSUnknownKeyException&#39;, reason: &#39;[ViewController >; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
- java用户界面——加载图片 jpg GIF
- 译-Web Service剖析: XML, SOAP 和WSDL 用于独立于平台的数据交换
- Django中的信号
- FortiGate数据流分析 debug flow
- Effective Java 第三版——49. 检查参数有效性
- CURL操作
- 从hive导出数据到mysql
- thinkphp3.2.2有预览的多图上传
- 函数中返回char *类型
热门文章
- input输入大于0的小数和整数
- 香港城市大学:全球首创3D打印微型机器人技术 有望作治疗癌症用途
- [转]C++中sizeof(struct)怎么计算?
- 【转】 树莓派初次启动攻略for Mac
- MarkdownPad 2 Pro 注册码
- Object-C知识点 (五) NSObject的继承关系
- 【贪心 哈夫曼树】bzoj2923: [Poi1998]The lightest language
- CPL学习笔记(二)
- php代码压缩
- 03大端和小端(Big endian and Little endian)