当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题。赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上。

  其实没有那么复杂,这个题目我们的操作只有三个 1、确定根节点。2、解环。 3连接子树。

  如果题目中给出了一个或者多个根节点,我们任选一个即可,证明:假设有k个可行根节点,那么选择一个不动,改动k-1次,每种选择都是这样。但如果题目中没有可选根节点,就不可以随便去选了,首先明确这种情况一定存在了1个或者多个环,我们一定要从环中选取根节点,因为这样只进行了一次操作就破环了环结构而且确定了根节点,如果在环外选择根节点,则改动为根节点需要1步,解环也需要一步,不是最优解!

  为了操作方便,使用并查集寻找环,并将找到第一个环的任意一个节点作为根节点。

  在确定了根节点之后,如果遇到其他可行根节点,直接连接到已确定根节点即可,就是连接子树的操作。

  注意:这个注意是对我自己说的,我犯了一个低级错误,我在判断环的时候居然没有用find判断,而是用fa判断,当时还感觉很困惑,以为合并方式不对,发现了以后才感觉自己真是好久没做过并查集了。。

  代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define N 200020
  6. int a[N],fa[N];
  7. int Find(int x)
  8. {
  9. return x==fa[x] ? x : fa[x] = Find(fa[x]);
  10. }
  11. void unin(int x,int y)
  12. {
  13. int fax = Find(x);
  14. int fay = Find(y);
  15. fa[fay] = fax;
  16. }
  17. int main()
  18. {
  19. int root = -,ans=,n;
  20. cin>>n;
  21. for(int i = ; i <= n; i++)
  22. {
  23. cin>>a[i];
  24. fa[i] = i;
  25. if(a[i]==i) root = i;
  26. }
  27. for(int i = ; i <= n; i++)
  28. {
  29. if(i == a[i] && a[i] == root) continue;
  30. else if(a[i] == i && i != root)
  31. {
  32. a[i] = root;
  33. ans++;
  34. unin(root,i);
  35. }
  36. else if(a[i] != i && Find(a[i]) != Find(i))
  37. {
  38. unin(a[i],i);
  39. }
  40. else if(a[i] != i && Find(a[i]) == Find(i))
  41. {
  42. ans++;
  43. if(root == -)
  44. {
  45. root = i;
  46. }
  47. a[i] = root;
  48. unin(root,i);
  49. }
  50. }
  51. cout<<ans<<endl;
  52. for(int i = ; i <= n; i++)
  53. {
  54. if(i==n) cout<<a[i]<<endl;
  55. else cout<<a[i]<<" ";
  56. }
  57. return ;
  58. }

CodeForces 698B Fix a Tree (并查集应用)的更多相关文章

  1. Codeforces 699D Fix a Tree 并查集

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

  2. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  3. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. CodeForces 698B Fix a Tree

    并查集,构造. 先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$1$,因此,问题不会特别复杂,画画图就能知道了. 如果给出的序列中已经存在$a[i]=i$,那么随便取一个$a[i]=i$的 ...

  5. Codeforces Round #363 (Div. 2) 698B Fix a Tree

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes     A tree is an und ...

  6. 【CodeForces】915 F. Imbalance Value of a Tree 并查集

    [题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...

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

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

  8. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

随机推荐

  1. 基于AFN的多张图片上传

    不废话,直接上代码 NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.166:8080/Discipli ...

  2. Uploadify 上传后的文件删除,上传队列无法更新问题

    1. 定义一个上传限制数量 var uploadLimit = 3; 2. 点击页面的删除图片成功后,将uploadLimit++操作 3. 通过uploadify的settings方式重置上传限制数 ...

  3. centos6.8安装superctl 后台管理工具

    下载安装python yum install python-setuptools 从官网下载supervisor包 https://pypi.python.org/pypi/supervisor 解压 ...

  4. python基础-更新篇

    对于windows来说就很简单了: 下载最新版本的python,然后卸载老版本,重新安装即可 对于linux系统的主机来说就有点复杂了: 现在linux主机自带的python版本都使2.x版的,而且L ...

  5. lnmp一键安装包配置laravel项目

    laravel一键安装包:https://lnmp.org/install.html 在server中加入 location / { try_files $uri $uri/ /index.php?$ ...

  6. oracle数据库 参数open_cursors和session_cached_cursor详解!

    open_cursors 每个session(会话)最多能同时打开多少个cursor(游标) session_cached_cursor 每个session(会话)最多可以缓存多少个关闭掉的curso ...

  7. 使用MyEclipse构建MAVEN项目

    这里用的是MyEclpise的自带的MAVEN插件.Maven最好配置成你自己安装的那个,MyEclipse自带会有些许Bug.用nexus代理Maven的中央仓库,setting.xml的配置文件修 ...

  8. EditText文本中用正则匹配是否包含数字,及判断文本只能是纯汉字或纯字母

    遇到判断EditText中文本,是否为制定格式 EditText et; Button btn; @Override protected void onCreate(Bundle savedInsta ...

  9. xcode6+ios8 横屏下启动画面不显示问题修改

    本文转载自汉果博客 » xcode6+ios8 横屏下启动画面不显示问题修改 最近我做游戏 发现xcode6+ios8 横屏下启动画面不显示   显示黑屏 . 设置横屏后 设置catalog 添加使用 ...

  10. 重读The C programming Lanuage 笔记三:简单计算器程序

    //简单计算器 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <str ...