Codeforces Round #430 (Div. 2)-搜索&STL-Ilya And The Tree
来源:互联网 发布:斯坦福商学院知乎 编辑:程序博客网 时间:2024/06/09 23:09
http://codeforces.com/contest/842/problem/C
给定一个树,树上每个点有一定价值。
这个点上的魅力值是 从1到这个点的路径 上的所有点价值的gcd。
你有一个操作,可以选择是否要把其中一个点的价值变成0。也就是不影响他的gcd。可以理解为每次求 一个点的魅力值时,都是独立的(也就是你在求一个点的时候,可以把一个点变成0,再求另一个点的魅力值时,可以选择其他点变成0,)
两种写法
一种是用set保留所有状态,(肯定在2e5之内)。
然后输出最大的。。
这个其实挺好的,
还有一种写法就比较的细腻,搜索时同时判断是否要变成0,我感觉这个也挺好。有机会补。
blog.csdn.net/shili_xu/article/details/77714112
#include <bits/stdc++.h>using namespace std;const int maxn=2e5+1000;vector<int>G[maxn];int a[maxn];set<int>se[maxn];int m;int gcd(int m,int n){ if(n==0) return m; else return gcd(n,m%n);}int dfs(int u,int fa,int GCd){ set<int>::iterator i; for(i=se[fa].begin();i!=se[fa].end();i++){ se[u].insert(__gcd(a[u],(*i))); } se[u].insert(GCd); GCd=__gcd(GCd,a[u]); se[u].insert(GCd); for(int i=0;i<G[u].size();i++){ int to=G[u][i]; if(to==fa) continue; dfs(to,u,GCd); }}int main(){ int a1,b1; while(~scanf("%d",&m)){ for(int i=1;i<=m;i++){ scanf("%d",&a[i]); } for(int i=1;i<m;i++){ scanf("%d%d",&a1,&b1); G[a1].push_back(b1); G[b1].push_back(a1); } dfs(1,0,0); for(int i=1;i<=m;i++) printf("%d ",*se[i].rbegin()); for(int i=0;i<maxn;i++){ G[i].clear(); se[i].clear(); } } return 0;}
阅读全文
0 0
- Codeforces Round #430 (Div. 2)-搜索&STL-Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(补题)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #293 (Div. 2)D. Ilya and Escalator
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- 【DP】 Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- iOS 你需要知道的团队代码规范,个人更应该规范
- POJ 1182 食物链
- i2ctools在android中的编译及使用
- LGV30的到来,是否能一雪前耻盖过三星呢?看到这个你就知道了!
- Cocos2dx-lua luabindings 示例
- Codeforces Round #430 (Div. 2)-搜索&STL-Ilya And The Tree
- MyBatis笔记二
- Android中svg动画框架PathView的使用
- AndroidStudio 自动导入内部类
- 分组取每组前几条记录(排名) 附group by与order by的研究
- 移动端网页纯原生js选项卡tab切换
- qt中用异或加密解密
- jQuery:多个AJAX/JSON并行请求对应一个回调函数处理方法$.when
- xml中嵌入Scala代码