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 ...
随机推荐
- layer弹层
获得 layer 文件包后,解压并将 layer 整个文件夹(不要拆分结构) 存放到你项目的任意目录,使用时,只需引入 layer.js 即可. 注意:引入layer.js前必须先引入jquery1. ...
- node.js内存泄露问题记录
先说一下.事情的来龙去脉. 公司开发一款游戏棋牌游戏,服务端的开发是IO密集型,开发的时候,考虑过使用python,java,node.js. 终于选择了node.js(node.js宣传的杀手功能. ...
- javascript模块化编程:CommonJS和AMD规范
AMD规范,异步模块定义.与CommonJS规范齐名并列. 作用都是利于JavaScript的模块化编程. 模块化编程的好处就是: 1.可重用 2.独立 3.能解决加载的依赖性问题 4.能解决重复加载 ...
- 简单的shell脚本编写
http://www.cnblogs.com/wuyuegb2312/p/3399566.html
- An O(ND) Difference Algorithm and Its Variations (1986)
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 The problems of finding a longest com ...
- Delphi ActiveForm发布全攻略
论坛上很多朋友(也包括我)提到ActiveForm的发布问题,都没有得到很好的解决.下面是本人开发ActiveForm的一点经验,拿出来跟大家分享,开发环境为 Win2000Server,IIS5.0 ...
- linux子系统的初始化_subsys_initcall()
http://my.oschina.net/u/572632/blog/305492#OSC_h1_3
- YTU 1068: 复制字符串
1068: 复制字符串 时间限制: 1 Sec 内存限制: 128 MB 提交: 602 解决: 382 题目描述 有一字符串,包含n个字符.写一函数,将此字符串中从第m个字符开始的全部字符复制成 ...
- SpringBoot使用logback日志记录
在resources里的配置文件: logback-spring.xml <?xml version="1.0" encoding="UTF-8" ?&g ...
- MySQL性能优化-I/O相关配置参数
本文介绍InnoDB和MyISAM两种存储引擎的I/O相关参数配置. 1.InnoDB I/O相关配置 Innodb是一种事务型的存储引擎,为了减少提交事务时产生的io开销,innodb采用了写日志 ...