((半个)智商题,主要难度在于实现)

题意:有一棵n个结点组成的树,其根是编号为1的结点。对于每一个结点,生成从根结点走到这个结点的路径(包括自身),选择路径上的一个点或者不选择任何点,使得其它点的最大公约数最大。每一个结点要分开考虑。

曾经错误做法:

ans[x][0]表示走到x点不选择任何点的最大,ans[x][1]表示走到x点选择1个点的最大。

ans[x][0]=gcd(ans[fa[x]][0],a[x])

ans[x][1]=max(ans[fa[x]][0],gcd(ans[fa[x]][1],a[x]))

错的地方:

如果a>=b,那么gcd(a,c)不一定大于等于gcd(b,c)。这里没有考虑这点。

举例:有一棵树2-3-4,对于4会得出1,正确是2。

错误2:set用错,打挂

事实上,set还有map的遍历都很容易写错,要小心。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<set>
  4. using namespace std;
  5. struct Edge
  6. {
  7. int to,next;
  8. bool type;
  9. }edge[];
  10. int n;
  11. int first[],a[],num_edge;
  12. bool vis[];
  13. int fa[];
  14. int ans0[];
  15. set<int> ans1[];//记录每个结点可能得到的所有gcd值
  16. //set<int>::iterator iter;
  17. int gcd(int a,int b)
  18. {
  19. int t;
  20. while(b!=)
  21. {
  22. t=a;
  23. a=b;
  24. b=t%b;
  25. }
  26. return a;
  27. }
  28. int dfs(int x)
  29. {
  30. vis[x]=true;
  31. int k=first[x];
  32. ans0[x]=gcd(ans0[fa[x]],a[x]);
  33. ans1[x].insert(ans0[fa[x]]);
  34. set<int>::iterator iter=ans1[fa[x]].begin();
  35. while(iter!=ans1[fa[x]].end())
  36. {
  37. ans1[x].insert(gcd(*iter,a[x]));
  38. iter++;
  39. }
  40. while(k!=)
  41. {
  42. if(!vis[edge[k].to])
  43. {
  44. fa[edge[k].to]=x;
  45. dfs(edge[k].to);
  46. }
  47. k=edge[k].next;
  48. }
  49. }
  50. int main()
  51. {
  52. int i,x,y,k;
  53. scanf("%d",&n);
  54. for(i=;i<=n;i++)
  55. scanf("%d",&a[i]);
  56. for(i=;i<n;i++)
  57. {
  58. scanf("%d%d",&x,&y);
  59. edge[++num_edge].to=y;
  60. edge[num_edge].next=first[x];
  61. first[x]=num_edge;
  62. edge[++num_edge].to=x;
  63. edge[num_edge].next=first[y];
  64. first[y]=num_edge;
  65. }
  66. ans0[]=a[];
  67. ans1[].insert();
  68. vis[]=true;
  69. k=first[];
  70. while(k!=)
  71. {
  72. fa[edge[k].to]=;
  73. dfs(edge[k].to);
  74. k=edge[k].next;
  75. }
  76. set<int>::reverse_iterator iter;
  77. for(i=;i<=n;i++)
  78. {
  79. iter=ans1[i].rbegin();
  80. printf("%d ",max(*iter,ans0[i]));
  81. }
  82. return ;
  83. }

Ilya And The Tree CodeForces - 842C的更多相关文章

  1. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  2. Codeforces Round #430 (Div. 2) C. Ilya And The Tree

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

  3. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  4. 【cf842C】 Ilya And The Tree(dfs、枚举因子)

    C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...

  5. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  8. C. Ilya And The Tree 树形dp 暴力

    C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意d ...

  9. codeforces 842C Ilya And The Tree

    Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...

随机推荐

  1. appium安装报错但运行成功

    npm install -g  appium ERR! fetch failed https://registry.npmjs.org/appium-uiauto/-/appium-uiauto-1. ...

  2. 二阶段 三阶段 提交 Paxos

    关于分布式事务.两阶段提交协议.三阶提交协议 - 文章 - 伯乐在线 http://blog.jobbole.com/95632/

  3. Do not use the <section> element as a generic container; this is what <div> is for, especially when the sectioning is only for styling purposes.

    Do not use the <section> element as a generic container; this is what <div> is for, espe ...

  4. HTML 客户端存储

    在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...

  5. map数据的分组,list数据排序 数据筛选

    sfit0144 (李四) 2015-01-10 18:00:251Sfit0734 (Sfit0734) 2015-01-10 18:00:38go homesfit0144 (李四) 2015-0 ...

  6. miller_rabin模板

    miller_rabin素数测试法 #include <iostream> #include <cstdlib> #include <stdio.h> #inclu ...

  7. poj2761静态区间第k大

    例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...

  8. 网络转载:局域网安全:解决ARP攻击的方法和原理

    局域网安全:解决ARP攻击的方法和原理 IT世界网2006-01-26 10:17   [故障原因] 局域网内有人使用ARP欺骗的木马程序(比如:传奇盗号的软件,某些传奇外挂中也被恶意加载了此程序). ...

  9. COGS-2049 疯狂动物城

    Description 你意外来到了一个未知的星球, 这里是一个动物乌托邦, 生活着一群拥有非凡智力的动物. 你遇到了一个叫做尼克的狐狸, 他准备给他的 GF 过生日 . 他将制作一个巨大的多层蛋糕, ...

  10. apache 安装及配置

    近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.or ...