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] ...
 
随机推荐
- 【转载】offer经验
			
http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html
 - Win10系列:JavaScript多媒体
			
在应用程序的日常使用中,经常会使用多媒体播放器来播放多媒体文件,包括视频.音频等,因此对于开发者来说,学习多媒体播放技术对开发应用是很有帮助的.本小节主要介绍如何使用HTML5和JavaScrip实现 ...
 - asp.net mvc 笔记一
			
webapi controller 中 action 名称 不能与 View controller 中的 action 名称相同,否则 Url.Action("actionName&quo ...
 - Mysql InnoDB三大特性-- change buffer
			
Mysql InnoDB三大特性-- change buffer
 - GFS中文翻译
			
Google文件系统 GFS是一个可扩展的分布式文件系统,用于大型的.分布式的.对大量数据进行访问的应用.它运行于廉价的普通硬件上,但可以提供容错功能.它可以给大量的用户提供总体性能较高的服务. 1. ...
 - pdf及word文档的读取 pyPDF2,docx
			
#!python3 #-*- coding:utf8 -*- #PyPDF2可能会打不开某些pdf文档,也不能提取图片,图表或者其他媒介从PDF文件中.但是它能提取文本从PDF中,转化为字符. imp ...
 - hustoj 管理员和后台设置
			
之前用过hustoj 的livecd版本,觉得有一些小问题,所以从头到尾搭建.主要包含的过程包括: 安装ubuntu系统 搭建hustoj 管理员和后台资源建设 本文介绍如何在搭建好hustoj的基础 ...
 - Python数据分析中对重复值、缺失值、空格的处理
			
对重复值的处理 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() from pandas import read_csv df = read_csv(文件位置) n ...
 - 控制台程序读取WIKI形式的TXT文件并一表格的形式显示在Word中
			
'Imports System.Collections.Generic 'Imports System.Text 'Imports System.IO 'Imports office = Micros ...
 - 如何从零安装Mysql
			
1.yum/rpm安装 2.采用二进制方式免编译安装MySQL. 3.考虑到MySQL5.4.xx及以后系列产品的特殊性,其编译方式和早期的第一条产品线的有所不同,这里采用cmake或gmake方式的 ...