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

题意:有一棵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. UVA - 11019 Matrix Matcher hash+KMP

    题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...

  2. hive增加Update、Delete支持

    一.配置hive-site.xml二.建表三.操作四.总结 一.配置hive-site.xml CDH版本先进入Hive配置页 选择高级,找到hive-site.xml 的 Hive 客户端高级配置代 ...

  3. Release Candidate

    RC_百度百科 https://baike.baidu.com/item/RC/7311964?fr=aladdin RC=Release Candidate,含义是"发布候选版" ...

  4. Remove FileUtil#copyMerge

    [HADOOP-12967] Remove FileUtil#copyMerge - ASF JIRA https://issues.apache.org/jira/browse/HADOOP-129 ...

  5. 使用libcurl的包装库cpr发起http请求

    cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...

  6. SDUT 周赛 神奇的树(简单题 注意数据类型的溢出 )

    神奇的树 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 SDUT有一颗神奇的苹果树.假如某天早上这树上有x个苹果,那么这树这一天 ...

  7. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  8. java邮件发送(含附件)

    1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在sr ...

  9. 简单的JDBC封装

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  10. 《SONG FROM PI: A MUSICALLY PLAUSIBLE NETWORK FOR POP MUSIC GENERATION》论文笔记

    出处:ICLR 2017 Motivation 提出一个通用的基于RNN的pop music生成模型,在层次结构中封装了先验乐理知识(prior knowledge about how pop mus ...