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 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.
给出一棵生成树,每个节点都有一个值,现在要求出每个节点的美丽值的最大值,美丽值的定义为从根节点到该节点(包含)路径上所有点的值的gcd,求解每个 点时可以把路径上某一个点的值变为0(就相当于删除这个节点的数)。你可以认为每个点美丽值的求解是独立的(每次删除的点都不会影响下一次)。
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.
2
6 2
1 2
6 6
3
6 2 3
1 2
1 3
6 6 6
1
10
10
我们用c[i]表示从1~i不变化的gcd值
用set[i]表示已变化的gcd的值的集合
对于u->v有
set[v].insert(c[u])表示将v变化
set[v][]=gcd(set[u][i],a[v])在v之前已变化
但这样可能存在疑问,每一次的集合都增加一个,而且还要遍历
这岂不是n^2???
但set去重后数量却远远达不到n,也就√n
因为a[x]的因数总共就不超过√a[x]个,不可能超过这么多,而a[x]<=20^5
所以可以过
输出直接输出集合中最大的数
代码这里用了STL中的集合
%%%%yzh巨佬用n^2暴力强行过:
http://www.cnblogs.com/Yuzao/p/7451552.html
果然巨佬还是强无敌啊
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct Node
{
int next,to;
}edge[];
int num,head[],c[],a[],n;
set<int>Set[];
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
int gcd(int a,int b)
{
if (!b) return a;
return gcd(b,a%b);
}
void dfs(int x,int pa)
{
for (int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
c[v]=gcd(c[x],a[v]);
Set[v].insert(c[x]);
for (set<int>::iterator i=Set[x].begin();i!=Set[x].end();i++)
Set[v].insert(gcd(a[v],*i));
dfs(v,x);
}
}
int main()
{int i,u,v;
cin>>n;
for (i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
c[]=a[];
Set[].insert();
dfs(,);
cout<<a[]<<' ';
for (i=;i<n;i++)
printf("%d ",*(--Set[i].end()));
if (n>)
cout<<*(--Set[n].end())<<endl;
}
最新文章
- java异常处理(父子异常的处理)
- Win7上的ASP.NET MVC3项目在Win10上运行的一个坑
- JQuery插件Validation的使用-遁地龙卷风
- UVA 156 Ananagrams ---map
- Linux基础与Linux下C语言编程基础
- ArcGIS API for Silverlight代码中使用Template模板
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&;&;bzoj 2820 YY的GCD&;&;BZOJ 3529 数表)
- 简单的monkey使用
- Business Intelligence (BI)
- wpf 透明效果 需要DwmApi.dll文件,然后定义一个函数去画Aero区域,从而实现整个窗口的Aero化。
- PAT (Advanced Level) 1049. Counting Ones (30)
- Python爬虫 股票数据爬取
- 责任链模式-Chain of Responsibility(Java实现), 例2
- DB2 因版本问题 Reorg 出错 解决办法
- vim 常用 NERDTree 快捷键
- Cordova结合Vue学习Camera
- C. Vasya and Multisets
- Windows平台下面Oracle11.2.0.1 升级Oracle11.2.0.4 的简单步骤
- [No0000D4]批处理全部代码详解Allbat
- jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第一话):初次启动jenkins,输入给定密码后登录失败问题解决