G - Ice_cream's world I (并查集)
39 World 查集 ICE
2023-09-11 14:22:48 时间
ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.
InputIn the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file. OutputOutput the maximum number of ACMers who will be awarded.
One answer one line.
Sample Input
8 10 0 1 1 2 1 3 2 4 3 4 0 5 5 6 6 7 3 6 4 7
Sample Output
3
AC代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 const int maxn=1010; 8 9 int n,m; 10 int father[maxn]; 11 12 void makeSet(){ 13 for(int i=0;i<n;i++){ 14 father[i]=i; 15 } 16 } 17 18 int findSet(int x){ 19 if(x!=father[x]){ 20 father[x]=findSet(father[x]); 21 } 22 return father[x]; 23 } 24 25 int main(){ 26 27 //freopen("input.txt","r",stdin); 28 29 while(~scanf("%d%d",&n,&m)){ 30 makeSet(); 31 int ans=0; 32 int a,b; 33 while(m--){ 34 scanf("%d%d",&a,&b); 35 int fx=findSet(a); 36 int fy=findSet(b); 37 if(fx==fy) 38 ans++; 39 else 40 father[fx]=fy; 41 } 42 printf("%d\n",ans); 43 } 44 return 0; 45 }
做这道题快有一年了,可是现在看来还是那么的陌生,真不知道自己这一年到底学到了什么?自己是不是在虚度光阴?
这有一个并查集详解,讲得很好
相关文章
- eclipse中编译出现错误undefined reference to `_sbrk'
- 获取PHP文件绝对地址$_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
- 【异常】redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'PSETEX'
- Error: Unexpected HTTP status 413 'Request Entity Too Large' on
- SpringBoot上传时出现异常 Required request part 'file' is not present
- [Angular] 'providedIn' for service
- [AngularJS + Unit Testing] Testing Directive's controller with bindToController, controllerAs and isolate scope
- [TypeScript] What's New in TypeScript 1.4
- Error:A problem occurred configuring project ':app'.
- python requests报错:Exception: ('Connection broken: IncompleteRead(3794 bytes read, 4398 more expect ed)', IncompleteRea
- python print 打印的数据包含中文,打印报错UnicodeDecodeError: 'gbk' codec can't decode bytes in position 459-460: illegal multibyte sequence解决办法
- win10 解决Failed installing 'Tomcat8' service
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
- iis配置问题报错 -- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies
- Atitit jsr规范有多少个 407个。Jsr规范大全
- Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途
- atitit.atiOrmStoreService 框架的原理与设计 part1 概述与新特性
- Atitit. C#.net clr 2.0 4.0新特性
- Atitit.常用语言的常用内部api 以及API兼容性对源码级别可移植的重要性 总结
- atitit.基于 Commons CLI 的命令行原理与 开发
- paip.python NameError name 'xxx' is not defined
- paip.proxool连接池 :Attempt to refer to a unregistered pool by its alias 'xx'
- DataGrip设置长sql语句自动换行 & Navicat工具 结构同步 & idea启动maven项目报:'@' that cannot start any token. (Do not use @ for indentation)
- e'charts 地图 + 背景旋转
- python '%r'或者'{!r}'的意思
- The remote name could not be resolved: 'report.dalabs.cn'
- 如何安装mariadb服务器和解决 can't connect to local mysql server through socket...