Ilya And The Tree CodeForces - 842C
((半个)智商题,主要难度在于实现)
题意:有一棵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的遍历都很容易写错,要小心。
- #include<cstdio>
- #include<algorithm>
- #include<set>
- using namespace std;
- struct Edge
- {
- int to,next;
- bool type;
- }edge[];
- int n;
- int first[],a[],num_edge;
- bool vis[];
- int fa[];
- int ans0[];
- set<int> ans1[];//记录每个结点可能得到的所有gcd值
- //set<int>::iterator iter;
- int gcd(int a,int b)
- {
- int t;
- while(b!=)
- {
- t=a;
- a=b;
- b=t%b;
- }
- return a;
- }
- int dfs(int x)
- {
- vis[x]=true;
- int k=first[x];
- ans0[x]=gcd(ans0[fa[x]],a[x]);
- ans1[x].insert(ans0[fa[x]]);
- set<int>::iterator iter=ans1[fa[x]].begin();
- while(iter!=ans1[fa[x]].end())
- {
- ans1[x].insert(gcd(*iter,a[x]));
- iter++;
- }
- while(k!=)
- {
- if(!vis[edge[k].to])
- {
- fa[edge[k].to]=x;
- dfs(edge[k].to);
- }
- k=edge[k].next;
- }
- }
- int main()
- {
- int i,x,y,k;
- scanf("%d",&n);
- for(i=;i<=n;i++)
- scanf("%d",&a[i]);
- for(i=;i<n;i++)
- {
- scanf("%d%d",&x,&y);
- edge[++num_edge].to=y;
- edge[num_edge].next=first[x];
- first[x]=num_edge;
- edge[++num_edge].to=x;
- edge[num_edge].next=first[y];
- first[y]=num_edge;
- }
- ans0[]=a[];
- ans1[].insert();
- vis[]=true;
- k=first[];
- while(k!=)
- {
- fa[edge[k].to]=;
- dfs(edge[k].to);
- k=edge[k].next;
- }
- set<int>::reverse_iterator iter;
- for(i=;i<=n;i++)
- {
- iter=ans1[i].rbegin();
- printf("%d ",max(*iter,ans0[i]));
- }
- return ;
- }
Ilya And The Tree CodeForces - 842C的更多相关文章
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- 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 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- 【cf842C】 Ilya And The Tree(dfs、枚举因子)
C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- C. Ilya And The Tree 树形dp 暴力
C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意d ...
- 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 ...
随机推荐
- appium安装报错但运行成功
npm install -g appium ERR! fetch failed https://registry.npmjs.org/appium-uiauto/-/appium-uiauto-1. ...
- 二阶段 三阶段 提交 Paxos
关于分布式事务.两阶段提交协议.三阶提交协议 - 文章 - 伯乐在线 http://blog.jobbole.com/95632/
- 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 ...
- HTML 客户端存储
在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...
- map数据的分组,list数据排序 数据筛选
sfit0144 (李四) 2015-01-10 18:00:251Sfit0734 (Sfit0734) 2015-01-10 18:00:38go homesfit0144 (李四) 2015-0 ...
- miller_rabin模板
miller_rabin素数测试法 #include <iostream> #include <cstdlib> #include <stdio.h> #inclu ...
- poj2761静态区间第k大
例题:poj2761 题目要求:给定一个长度为n的序列,给定m个询问,每次询问求[l,r]区间内的第k大: 对于这道题目来说,很多算法都可以使用,比如说树套树(一个负责划分区间,一个负责维护这段区间内 ...
- 网络转载:局域网安全:解决ARP攻击的方法和原理
局域网安全:解决ARP攻击的方法和原理 IT世界网2006-01-26 10:17 [故障原因] 局域网内有人使用ARP欺骗的木马程序(比如:传奇盗号的软件,某些传奇外挂中也被恶意加载了此程序). ...
- COGS-2049 疯狂动物城
Description 你意外来到了一个未知的星球, 这里是一个动物乌托邦, 生活着一群拥有非凡智力的动物. 你遇到了一个叫做尼克的狐狸, 他准备给他的 GF 过生日 . 他将制作一个巨大的多层蛋糕, ...
- apache 安装及配置
近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.or ...