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

题意:有一棵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的更多相关文章

  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. iOS APP第一次上架遇到的问题

     现在苹果审核时越来越严了,我们有两个APP时同时上线的,代码用的也是一套的.但是有其中一个是第一次发布所以就拒了,信息就是下图.大概意思是用到支付了吗?用户是怎么来的.值需要把这些信息回复了.就OK ...

  2. Rowkey is the Crux Rowkey Design

    Apache HBase ™ Reference Guide http://hbase.apache.org/book.html#rowkey.design The Effect of ColumnF ...

  3. R学习-- 数组和矩阵

    生成 4行5列的数组,逐列逐行赋值x = array(1:20, dim= c(4,5)) 依据已知向量生成二维数组i = array(c(1:3,3:1,4:6,5:7), dim=c(3,4))也 ...

  4. iOS 设置TextView控件内容行间距

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (textView.text.length < 1) { textV ...

  5. exception.py

    try: raise Exception except Exception as e: print(e) try: raise Exception('comment') except Exceptio ...

  6. Github--开源代码仓库式系统(转)

    要了解Github,我们首先要知道Git,Git是管理代码的工具,写代码不是件轻松的事儿,一个人写的时候已经不轻松了,一群人写就更不轻松了,但这世界上很多事都是怎么不轻松怎么来的,大部分人都会和别人一 ...

  7. C++ 下啥时候用struct, 啥时候用class

    struct 由c语言引入.在c语言中,是定义结构化数据的标准选择. c++ 同时支持struct 和 class. 原因之一是c++ 是 c 的超集,涵盖c 已支持的语言要素,将更好的支持向下兼容( ...

  8. Javaweb中利用kaptcha生成验证码

    引入kaptcha-2.3-jdk15.jar包 在web.xml中进行配置 <servlet> <servlet-name>Kaptcha</servlet-name& ...

  9. 2.25-2.26 MapReduce执行流程Shuffle讲解

    原文链接:https://langyu.iteye.com/blog/992916 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是 ...

  10. k8s-调度器、预选策略及优选函数-二十

    一.简介 master上运行着三个最核心的组件,apiserver.scheduler.controller manager.此外,master还依赖于ectd存储节点,最好ectd是有冗余能力的集群 ...