Codeforces 842C Ilya And The Tree【Dfs】
来源:互联网 发布:轻松赚钱的软件 编辑:程序博客网 时间:2024/06/06 04:11
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.
Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).
Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
26 21 2
6 6
36 2 31 21 3
6 6 6
110
10
题目大意:
求从根节点1到每一个节点x路径上的gcd最大值,我们可以用一次机会使得路径上的某个数变成0.
思路:
爆搜+set去重即可。因为每个数的因子数最多为logn个,那么对应一条路径上的gcd的值也不会多。
Ac代码:
#include<stdio.h>#include<string.h>#include<vector>#include<set>using namespace std;vector<int>mp[250000];int ans[250000];int a[250000];int gcd(int x,int y){ return y==0?x:gcd(y,x%y);}void Dfs(int u,int from,set<int>now,int Val){ for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(v==from)continue; set<int>nex; nex.insert(Val); set<int>::iterator it; for(it=now.begin();it!=now.end();it++) { nex.insert(gcd(*it,a[v])); } ans[v]=max(ans[v],*nex.rbegin()); Dfs(v,u,nex,gcd(Val,a[v])); }}int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)mp[i].clear(); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n-1;i++) { int x,y;scanf("%d%d",&x,&y); mp[x].push_back(y); mp[y].push_back(x); } ans[1]=a[1]; set<int>temp;temp.clear(); temp.insert(a[1]); temp.insert(0); Dfs(1,-1,temp,a[1]); for(int i=1;i<=n;i++)printf("%d ",ans[i]); printf("\n"); }}
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842C Ilya And The Tree【Dfs】
- codeforces 842C. Ilya And The Tree(dfs)
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842C Ilya And The Tree
- Codeforces 842 C Ilya And The Tree
- Codeforces 842 C. 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+最大公约数+因子+树)
- 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 dfs+set
- 842C Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- codeforce 842C. Ilya And The Tree(dfs用set存储到每种可能)
- Ilya And The Tree(dfs,数学)
- CAD二次开发(C#)之Application 对象
- ie8 new Date('2017-08-30')值为NaN
- ActiveMq的简介
- HTTP请求错误400、401、402、403、404、405、406、407、412、414、415、500、501、502解析
- Android自定义控件中获取文字宽高的方法已经drawText中的xy
- Codeforces 842C Ilya And The Tree【Dfs】
- 键盘上被忽略的键
- Service Broker 服务代理 (单数据库、数据库间、实例间会话)
- java反射获取当前对象属性的值
- js中几种实用的跨域方法原理详解
- java 分布式锁方案
- 第三方账号登录------微信
- java 虚拟机
- 链表合并