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 ...
随机推荐
- POJ 2482 Stars in Your Window(线段树+扫描线)
题目链接 非常不容易的一道题,把每个点向右上构造一个矩形,将问题转化为重合矩形那个亮度最大,注意LL,注意排序. #include <cstdio> #include <cstrin ...
- sanic官方文档解析之蓝图
1,蓝图(Blueprints) 蓝图可用于子路由的应用,代替增加路由的存在,蓝图的定义和增加路由的方法相似,灵活的在应用中注册,并且可插拔的方式. 尤其是在大型应用中使用蓝图的时候在你逻辑打断的地方 ...
- iOS开发——高级篇——多线程的安全隐患
资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题 一.解 ...
- sim的准确识别技术
几个月钱,我换了一个手机,本着工科男动手能力强的原则,自己用✂️把sim卡剪成了一个小卡,然后成功的可以使用了. 然而就在昨天,我将卡拿出之后,再放回去,却无法识别我的sim卡了. 我上网查了方法,怀 ...
- spring boot---WebFilter注解 实现自定义登录过滤器
https://my.oschina.net/wangnian/blog/647976 http://www.jianshu.com/p/05c8be17c80a
- alsa和oss声音系统比较
OSS(Open Sound System) OSS的含义为,Open Sound System,是unix平台上一个统一的音频接口.以前,每个Unix厂商都会提供一个自己专有的API,用来处理音频. ...
- Hihocoder #1098 : 最小生成树二·Kruskal算法 ( *【模板】 )
#1098 : 最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用 ...
- HDU2476 String painter —— 区间DP
题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others) Me ...
- html5--6-57 阶段练习6-折叠导航栏
html5--6-57 阶段练习6-折叠导航栏 实例 @charset="UTF-8"; *{ ; ; } h3+div{ ; overflow: hidden; transiti ...
- oracle:block 的 water mark问题
看了小布老师关于block里面数据存储的high water mark的实验,自己也做了一遍. SQL> create table x(i int,name varchar(20)); Tabl ...