Ilya And The Tree
来源:互联网 发布:淘宝网高跟鞋 编辑:程序博客网 时间:2024/06/04 21:01
题意:求根节点到所有叶子节点,所有路径上的点的最大公约数,可以让一个点为0,暴力搜索,set去重
#include<bits\stdc++.h>using namespace std;typedef long long ll;#define pb push_backvector <int>g[200005];set<int>s[200005];int a[200005];void dfs(int x,int fa,int now){ for(auto tmp:s[fa]) s[x].insert(__gcd(tmp,a[x])); s[x].insert(now); now=__gcd(now,a[x]); s[x].insert(now); // cout<<x<<endl; // getchar(); for(auto tmp:g[x]) { if(tmp!=fa) dfs(tmp,x,now); }}int main(){int n;while(~scanf("%d",&n)){ int x,y; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<n;i++) {scanf("%d%d",&x,&y);g[x].pb(y);g[y].pb(x);} dfs(1,0,0); for(int i=1;i<=n;i++) printf("%d ",*s[i].rbegin());} return 0;}
阅读全文
0 0
- Ilya And The Tree
- 842C Ilya And The Tree
- Ilya And The Tree(dfs,数学)
- Codeforces 842C Ilya And The Tree
- Codeforces 842 C Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842C Ilya And The Tree【Dfs】
- C. Ilya And The Tree(树形搜索+dp)
- Codeforces 842 C. Ilya And The Tree 树上dp
- Codeforces 430 Div 2 C.Ilya And The tree
- codeforces 842C. Ilya And The Tree(dfs)
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces 842C Ilya And The Tree(树上DP+因子个数估计)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- SpringMVC框架中,获得request对象的几种方式
- 【hadoop+spark】搭建spark过程
- MYSQL中批量替换某个字段的部分数据
- js获取url中的解析字符串
- eclipse JavaScript Validator
- Ilya And The Tree
- 全角、半角字符长度验证
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)
- 自定义View
- c# 登录 防止sql注入 mysql数据库
- Ubuntu shutter截图像qq截图一样方便
- ThreadLocal理解
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- 从0开始学Python——我的学习之路