题意:有一颗树,每个点有一个点权,边权都是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. hibernate配置注意事项

    1:多对一配置 private Set<DrawRecordModel> cjrecordsSet = new HashSet<DrawRecordModel>(); 正确 p ...

  2. 请求体中需要的true和requests包put请求冲突了

    python  put请求,添加请求头 不知道怎么解决

  3. Linux中的touch命令总结(一)

    touch命令有两个主要功能: 改变 timestamps 新建_空白_文件 例如,不带任何参数地输入: touch file1 file2 file3 将在当前目录下新建三个空白文件:file1, ...

  4. Spring Boot 2.x:SpringBoot

    几个重要的事件回调机制: 1.配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunlisten ...

  5. ci常量

    1. ENVIRONMENT产品的环境,有3种环境,分别是: development开发环境 testing测试环境 production生产环境 2. SELFCI的主入口文件名称 例如我的是: i ...

  6. <三剑客> 老大:awk命令用法

    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一 个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是lin ...

  7. 9.27-uname,useradd命令

    打印系统信息 [root@wen ~]# uname Linux [root@wen ~]# uname -r #内核版本 2.6.32-573.el6.x86_64 [root@wen ~]# un ...

  8. LG1440 求 m 区间内的最小值

    题目描述 一个含有 \(n\) 项的数列 (\(n≤ 2000000\)),求出每一项前的 \(m\) 个数到它这个区间内的最小值.若前面的数不足 \(m\) 项则从第 \(1\) 个数开始,若前面没 ...

  9. 2018-2019-2 《Java程序设计》第11周学习总结

    20175319 2018-2019-2 <Java程序设计>第11周学习总结 教材学习内容总结 本周学习<Java程序设计>第十三章java网络编程: - URL类 URL类 ...

  10. LINK : fatal error LNK1561: 必须定义入口点

    转自VC错误:http://www.vcerror.com/?p=1313 问题描述: 错误:LINK : fatal error LNK1561: 必须定义入口点 解决方法: 详细的解决方法可参考V ...