CodeForces - 1101D:GCD Counting (树分治)
You are given a tree consisting of n vertices. A number is written on each vertex; the number on vertex i is equal to ai
.
Let's denote the function g(x,y)
as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex x to vertex y (including these two vertices). Also let's denote dist(x,y) as the number of vertices on the simple path between vertices x and y, including the endpoints. dist(x,x)=1 for every vertex x
.
Your task is calculate the maximum value of dist(x,y)
among such pairs of vertices that g(x,y)>1
.
Input
The first line contains one integer n
— the number of vertices (1≤n≤2⋅105)
.
The second line contains n
integers a1, a2, ..., an (1≤ai≤2⋅105)
— the numbers written on vertices.
Then n−1
lines follow, each containing two integers x and y (1≤x,y≤n,x≠y) denoting an edge connecting vertex x with vertex y
. It is guaranteed that these edges form a tree.
Output
If there is no pair of vertices x,y
such that g(x,y)>1, print 0. Otherwise print the maximum value of dist(x,y)
among such pairs.
Examples
3
2 3 4
1 2
2 3
1
3
2 3 4
1 3
2 3
2
3
1 1 1
1 2
2 3
0
题意:让你求最长的路径长度,满足路上gcd不为1;
思路:分治的做法比较暴力,但是时限比较长,有板子就直接上了。 由于gcd具有收敛性,路径上的gcd不会太多,而且越远回越接近1,我们记录每一个gcd的最深的位置即可。 (我用的以前的板子,所以用了map,此题的数据量可以不用map
还有一个思路,我们枚举素因子,然后把含有这个素因子的点标记出来,求他们的最远距离,更新答案。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
const int inf=0x7FFFFFFF;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cnt,N,sn;
int a[maxn],sz[maxn],son[maxn],vis[maxn],root,res;
int dep[maxn];
ll ans[maxn];
map<int,int>mp,tp;
map<int,int>::iterator it1,it2;
inline void read(int &x) {
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c<=''&&c>='') x=(x<<)+(x<<)+c-'',c=getchar();
}
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void getroot(int u,int fa) //找重心
{
sz[u]=; son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=fa&&!vis[To[i]]){
getroot(To[i],u);
sz[u]+=sz[To[i]];
son[u]=max(son[u],sz[To[i]]);
}
}
son[u]=max(son[u],sn-son[u]);
if(root==||son[root]>son[u]) root=u;
}
void getans(int u,int fa,int num) //对于当前链产生的新GCD
{
dep[u]=dep[fa]+; tp[num]=max(tp[num],dep[u]);
for(int i=Laxt[u];i;i=Next[i]){
if(!vis[To[i]]&&To[i]!=fa){
getans(To[i],u,__gcd(num,a[To[i]]));
}
}
}
void solve(int u) //解决以u为根的子问题
{
mp.clear(); mp[a[u]]=; ans[a[u]]++; dep[u]=;
for(int i=Laxt[u];i;i=Next[i])
if(!vis[To[i]]) {
tp.clear(); getans(To[i],u,__gcd(a[u],a[To[i]]));
for(it1=mp.begin();it1!=mp.end();it1++)
for(it2=tp.begin();it2!=tp.end();it2++){
int g=__gcd((*it1).first,(*it2).first);
if(g>) res=max(res,(*it1).second+(*it2).second-);
}
for(it2=tp.begin();it2!=tp.end();it2++)
mp[(*it2).first]=max((*it2).second,mp[(*it2).first]);
}
}
void dfs(int u) //分治
{
vis[u]=; solve(u);
for(int i=Laxt[u];i;i=Next[i]){
if(vis[To[i]]) continue;
root=; sn=sz[To[i]];
getroot(To[i],); dfs(root);
}
}
int main()
{
read(N); int u,v,Max=;
for(int i=;i<=N;i++) read(a[i]),Max=max(Max,a[i]);
for(int i=;i<N;i++) {
read(u);read(v);
add(u,v); add(v,u);
}
if(Max>) res=;
root=; sn=N; getroot(,); dfs(root);
printf("%d\n",res);
return ;
}
CodeForces - 1101D:GCD Counting (树分治)的更多相关文章
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- Ultimate Weirdness of an Array CodeForces - 671C (gcd,线段树)
大意: 定义一个数列的特征值为两个数gcd的最大值, $f(l,r)$表示数列删除区间$[l,r]$的元素后剩余元素的特征值, 求$\sum_{i=1}^n\sum_{j=i}^n{f(i,j)}$ ...
- CF1101D GCD Counting 点分治+质因数分解
题意:求最长的树上路径点值的 $gcd$ 不为 $1$ 的长度. 由于只要求 $gcd$ 不为一,所以只要 $gcd$ 是一个大于等于 $2$ 的质数的倍数就可以了. 而我们发现 $2\times 1 ...
- CodeForces - 990G GCD Counting
Discription You are given a tree consisting of nn vertices. A number is written on each vertex; the ...
- CF990G GCD Counting 点分治+容斥+暴力
只想出来 $O(nlogn\times 160)$ 的复杂度,没想到还能过~ Code: #include <cstdio> #include <vector> #includ ...
- Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...
- Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting
G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE..... 需要每次清空一下子树的map... #inc ...
- CF1101D GCD Counting
题目地址:CF1101D GCD Counting zz的我比赛时以为是树剖或者点分治然后果断放弃了 这道题不能顺着做,而应该从答案入手反着想 由于一个数的质因子实在太少了,因此首先找到每个点的点权的 ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
随机推荐
- 微信小程序 HMACSHA256 哈希加密
下载CryptoJS, 增加红色的这句 module.exports = CryptoJS /* CryptoJS code.google.com/p/crypto-js (c) 2009-2012 ...
- summary_16th Nov, 2018
一. 编程语言的分类: a. 机器语言:直接使用二进制指令去编写程序,必须考虑硬件细节 b:汇编语言:用英文标签取代二进制指令去编写程序,必须考虑硬件细节 c:高级语言:用人类能理解的方式编写程序,通 ...
- 使用Postman在Chrome下进行rest请求测试
1.首先下载postman,我已经下载好,放在云盘里了. http://pan.baidu.com/s/1c1YoGKS 密码 dgfw 2.打开Chrome,点击更多工具->扩展程序~打开. ...
- vsCode快捷键设置
// 快捷键设置 keyiing.json // 将键绑定放入此文件中以覆盖默认值 [ /* // 转换大写 { "key" : "ctr ...
- git merge branch
git branch look at your branches git branch newbranch git checkout newbrach do something git check ...
- linux文件管理 文件操作
文件操作 pwd 命令 该命令的英文解释为print working direction(打印工作目录).输入pwd命令,Linux输出当前目录. cd 命令 用来改变所在目录 cd / 转到根目录 ...
- xpath & <tr><td><br>
python : 3.6 lxml : 4.2.1 from lxml.html import etree test_html = ''' <!DOCTYPE html PUBLIC " ...
- el表达式原样输出,不被解析
今天遇到了,在jar包都有的前提下EL表达式原样输出,不被解析,原因是: page指令中确少 isELIgnored="false" 加上就好了 <%@ page langu ...
- set循环遍历删除特定元素
使用Iterator迭代器 public class Demo { public static void main(String[] args) { Set<Object> obj = n ...
- 《Python》线程池、携程
一.线程池(concurrent.futures模块) #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 P ...