Codeforces 755C:PolandBall and Forest(并查集)
http://codeforces.com/problemset/problem/755/C
题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相等则p[i]是 id 较小的点。
思路:一开始没什么想法,画几分钟图发现不到什么东西,后来想着 i 和 p[i] 有关系,那么就代表 i 和 p[i] 是属于同一棵树,那么不就是并查集了嘛。抱着试一试的心态搞了一下居然过了。
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <algorithm>
- #include <string>
- #include <iostream>
- #include <stack>
- #include <map>
- #include <queue>
- #include <set>
- using namespace std;
- typedef long long LL;
- #define N 100010
- #define INF 0x3f3f3f3f
- int fa[N], num[N];
- int Find(int x) {
- if(x == fa[x]) return x;
- return fa[x] = Find(fa[x]);
- }
- void Merge(int x, int y) {
- x = Find(x), y = Find(y);
- if(x == y) return ;
- fa[x] = y;
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- for(int i = ; i <= n; i++) fa[i] = i;
- for(int i = ; i <= n; i++) scanf("%d", &num[i]);
- for(int i = ; i <= n; i++) {
- Merge(i, num[i]);
- }
- int ans = ;
- for(int i = ; i <= n; i++) if(fa[i] == i) ans++;
- printf("%d\n", ans);
- return ;
- }
Codeforces 755C:PolandBall and Forest(并查集)的更多相关文章
- CodeForces 755C PolandBall and Forest (并查集)
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...
- CodeForces - 755C PolandBall and Forest (并查集)
题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- Codeforces 859E Desk Disorder:并查集【两个属性二选一】
题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...
- Codeforces 651E Table Compression【并查集】
题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...
随机推荐
- Entity framework 配置文件,实现类,测试类
配置文件信息App.config: 数据库IP地址为192.168.2.186 ,数据库名为 Eleven-Six , 用户名 123456,密码654321 <?xml version=&qu ...
- 浅谈 Swift 中的 Optionals
input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...
- LAN公布java web项目的方法的外侧
1.进入路由器设置页面:http://192.168.1.1 2.找到"转发规则"选项,我现在的路由器tp-link,不同型号tp-link"转发规则"选项位置 ...
- Excel的Range对象(C#)
原文:Excel的Range对象(C#) Range 对象是 Excel 应用程序中最经常使用的对象:在操作 Excel 内的任何区域之前,都需要将其表示为一个 Range 对象,然后使用该 Rang ...
- MySQL半同步复制搭建
默认情况下,MySQL 5.5/5.6/5.7和MariaDB 10.0/10.1的复制是异步的,异步复制可以提供最佳性能,主库把binlog日志发送给从库,这一动作就结束了,并不会验证从库是否接收完 ...
- wchar_t string on Linux, OS X and Windows
Making wchar_t work on Linux, OS X and Windows for CMarkup release 10.1 I learned a couple of humble ...
- PHP阿里大鱼短信验证
PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定AppKEY和App Secret还有配置签名 第二步 在应用管理中选择SDK下载,或者直接点击http://down ...
- c#透明panel
先看下效果 纯透明的pane,然后设置一个半透明的图片,可以看出来显示了父控件的button 看代码 public partial class PanelEx : Panel { protected ...
- vista下开机启动 简单绕过UAC的方法(自己使用runas参数重新启动自己,有点意思)
背景 vista下,如果不开启UAC,那就没有我下面要说的问题了,呵呵.下面说的都是在vista开启UAC的前提下说的,win7也适用. 在vista下,系统开启了UAC,如果你的 ...
- delphi hook alt+F4 ctrl+delete+alt win键等
unit uHook; interface uses Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, Dialogs ...