http://codeforces.com/problemset/problem/755/C

题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相等则p[i]是 id 较小的点。

思路:一开始没什么想法,画几分钟图发现不到什么东西,后来想着 i 和 p[i] 有关系,那么就代表 i 和 p[i] 是属于同一棵树,那么不就是并查集了嘛。抱着试一试的心态搞了一下居然过了。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <string>
  7. #include <iostream>
  8. #include <stack>
  9. #include <map>
  10. #include <queue>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. #define N 100010
  15. #define INF 0x3f3f3f3f
  16. int fa[N], num[N];
  17.  
  18. int Find(int x) {
  19. if(x == fa[x]) return x;
  20. return fa[x] = Find(fa[x]);
  21. }
  22.  
  23. void Merge(int x, int y) {
  24. x = Find(x), y = Find(y);
  25. if(x == y) return ;
  26. fa[x] = y;
  27. }
  28.  
  29. int main()
  30. {
  31. int n;
  32. scanf("%d", &n);
  33. for(int i = ; i <= n; i++) fa[i] = i;
  34. for(int i = ; i <= n; i++) scanf("%d", &num[i]);
  35. for(int i = ; i <= n; i++) {
  36. Merge(i, num[i]);
  37. }
  38. int ans = ;
  39. for(int i = ; i <= n; i++) if(fa[i] == i) ans++;
  40. printf("%d\n", ans);
  41. return ;
  42. }

Codeforces 755C:PolandBall and Forest(并查集)的更多相关文章

  1. CodeForces 755C PolandBall and Forest (并查集)

    题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...

  2. CodeForces - 755C PolandBall and Forest (并查集)

    题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...

  3. codeforces 755C. PolandBall and Forest

    C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  5. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  6. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  7. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  8. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  9. Codeforces 859E Desk Disorder:并查集【两个属性二选一】

    题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...

  10. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

随机推荐

  1. Entity framework 配置文件,实现类,测试类

    配置文件信息App.config: 数据库IP地址为192.168.2.186 ,数据库名为 Eleven-Six , 用户名 123456,密码654321 <?xml version=&qu ...

  2. 浅谈 Swift 中的 Optionals

    input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...

  3. LAN公布java web项目的方法的外侧

    1.进入路由器设置页面:http://192.168.1.1 2.找到"转发规则"选项,我现在的路由器tp-link,不同型号tp-link"转发规则"选项位置 ...

  4. Excel的Range对象(C#)

    原文:Excel的Range对象(C#) Range 对象是 Excel 应用程序中最经常使用的对象:在操作 Excel 内的任何区域之前,都需要将其表示为一个 Range 对象,然后使用该 Rang ...

  5. MySQL半同步复制搭建

    默认情况下,MySQL 5.5/5.6/5.7和MariaDB 10.0/10.1的复制是异步的,异步复制可以提供最佳性能,主库把binlog日志发送给从库,这一动作就结束了,并不会验证从库是否接收完 ...

  6. 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 ...

  7. PHP阿里大鱼短信验证

    PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定AppKEY和App Secret还有配置签名 第二步 在应用管理中选择SDK下载,或者直接点击http://down ...

  8. c#透明panel

    先看下效果 纯透明的pane,然后设置一个半透明的图片,可以看出来显示了父控件的button 看代码 public partial class PanelEx : Panel { protected ...

  9. vista下开机启动 简单绕过UAC的方法(自己使用runas参数重新启动自己,有点意思)

    背景      vista下,如果不开启UAC,那就没有我下面要说的问题了,呵呵.下面说的都是在vista开启UAC的前提下说的,win7也适用.      在vista下,系统开启了UAC,如果你的 ...

  10. delphi hook alt+F4 ctrl+delete+alt win键等

    unit uHook; interface uses  Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, Dialogs ...