题意:有一颗树,每个点有一个点权,边权都是1,问路径上的所有点的gcd不是1的最长路径是多少?

思路:之前补这道题的时候,用分解质因数 + 树形DP做的,其实用点分治可以更暴力一点。对于这类统计树上路径的问题,点分治是一种通用,高效的解法。对于所有的路径,无非两种情况:经过某个点,不经过某个点,我们对于每个点统计的相当于是经过该点的路径的信息。点分治为以下几步:1:找重心。2:以找到的重心为根,解决经过该点的路径的问题。3:递归的找其它子树的重心,解决问题。找经过该点的路径时,直接搜索求gcd,所有说非常暴力无脑。找到之后暴力合并。复杂度应该是O(n * (log(n) ^ 2));

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int> G[maxn];
map<int, int> mp, tmp;
map<int, int>::iterator it1, it2;
bool v[maxn];
int sz[maxn], tot, mx, root, a[maxn], ans;
void add(int x, int y) {
G[x].push_back(y);
G[y].push_back(x);
}
void get_root(int x, int fa) {
sz[x] = 1;
int max_part = 0;
for (auto y : G[x]) {
if(y == fa || v[y]) continue;
get_root(y, x);
sz[x] += sz[y];
max_part = max(max_part, sz[y]);
}
max_part = max(max_part, tot - sz[x]);
if(max_part < mx) {
mx = max_part;
root = x;
}
}
void get_ans(int x, int dep, int fa, int z) {
if(z == 1) return;
tmp[z] = max(tmp[z], dep);
for (auto y : G[x]) {
if(v[y] || y == fa) continue;
get_ans(y, dep + 1, x, __gcd(z, a[y]));
}
}
void solve(int x) {
mp.clear();
mp[a[x]] = 1;
for (auto y : G[x]) {
if(v[y]) continue;
tmp.clear();
get_ans(y, 1, x, __gcd(a[x], a[y]));
for (it1 = mp.begin(); it1 != mp.end(); it1++) {
for (it2 = tmp.begin(); it2 != tmp.end(); it2++) {
if(__gcd(it1 -> first, it2 -> first) != 1) {
ans = max(ans, (it1 -> second) + (it2 -> second));
}
}
}
for (it2 = tmp.begin(); it2 != tmp.end(); it2++) {
mp[it2 -> first] = max(mp[it2 -> first], (it2 -> second) + 1);
}
}
}
void div(int x) {
v[x] = 1;
solve(x);
for (auto y : G[x]) {
if(v[y]) continue;
tot = sz[y];
mx = 1e6;
get_root(y, x);
div(root);
}
}
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if(a[i] > 1) ans = 1;
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
}
tot = n;
mx = 1e6;
get_root(1, -1);
div(root);
printf("%d\n", ans);
}

  

Codeforces 1101D 点分治的更多相关文章

  1. CodeForces - 1101D:GCD Counting (树分治)

    You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...

  2. Codeforces 293E 点分治+cdq

    Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...

  3. codeforces 161D 点分治

    传送门:https://codeforces.com/problemset/problem/161/D 题意: 求树上点对距离恰好为k的点对个数 题解: 与poj1741相似 把点分治的模板改一下即可 ...

  4. Codeforces 475D CGCDSSQ(分治)

    题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...

  5. Codeforces 888G(分治+trie)

    按位贪心,以当前考虑位是0还是1将数分成两部分,则MST中这两部分之间只会存在一条边,因为一旦有两条或以上的边,考虑两条边在原图中所成的环,显然这两条边有一条是环上的权值最大边,不会出现在MST中.则 ...

  6. Codeforces 888G Xor-MST - 分治 - 贪心 - Trie

    题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...

  7. Codeforces 990G 点分治+暴力

    题意:给出一棵点带权的树,求i\(\in\)[1,200000]所有路径的上点权的gcd==i的个数. 考虑点分治,对于一棵以u为根的子树,如何统计经过u的路径的答案? 显然既然是经过点u的路径,那么 ...

  8. 【题解】Radio stations Codeforces 762E CDQ分治

    虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...

  9. Radio stations CodeForces - 762E (cdq分治)

    大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...

随机推荐

  1. ResourceBundle读取配置文件

    import java.util.ResourceBundle; /** * Created by win7 on 2017/5/20. */public class Test1 { public s ...

  2. Centos 安装.NET Core环境

    https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install 一.概述 本篇讨论如何把项目发布到Linux环境,主要包括 ...

  3. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  4. springMVC的controller更改了,如何不重启,而自动刷新的解决办法(亲测,一招解决)

    Tomcat  con/ service.xml  配置如下一行代码: <Context reloadable="true"/> </Host> 然后以de ...

  5. BDE(一款数据库引擎,通过它可以连接不同数据库)

    BDE(Borland Database Engine)是Inprise公司的数据库引擎,它结合了SQL Links允许程序员通过它能够连接到各种不同的数据库.BDE是BORLAND 数据库引擎的缩写 ...

  6. webpack 4.+版本需要注意的地方

    在webpack打包的时候需要npm下载webpack-cli,而且打包JS的命令从以前的webpack .\src\main.js  .\dist\boundle.js 要编程 webpack .\ ...

  7. wxparse使用(富文本插件)

    优点:目前已知唯一可以转化HTML到小程序识别的插件 缺点:转换一个HTML标签可能需要大量的微信小程序标签还有样式 配置:第一步,下载 https://github.com/icindy/wxPar ...

  8. idea中git stash--解决pull冲突或切换分支问题

    场景1:pull代码时提示冲突,本地代码和远程代码有冲突 场景2:当切换分支时,当前分支的代码又不想commit 这两种场景可以使用git stash来解决,将当前未commit的代码暂存起来. 操作 ...

  9. 【ngx-ueditor】百度编辑器按下Shift键不触发contentChange事件

    背景:基于Angular 6,引入ngx-ueditor 发现现象:如果以Shift键+任意键结尾,则ngModel会丢失包含shift键的字符 例如:输入“ABC+AB++++”,则ngModel中 ...

  10. drf:筛选,序列化

    1.基础 restful规范: - url:一般用名词 http://www.baidu.com/article (面向资源编程) - 根据请求方式的不同做不同操作:get,post,put,dele ...