Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
来源:互联网 发布:python 爬虫 动态页面 编辑:程序博客网 时间:2024/06/05 08:13
地址
http://codeforces.com/contest/842/problem/C
题意
给一颗树,树上每个点都有一个权值,一个节点的beauty是这个点到根节点所有权值去掉一个值(或者一个值都不去掉)的gcd,求每个节点的最大beauty
思路
考虑树dp的方法,一个节点的最大beauty要么是不算自己的权值计算出的gcd(这个可以通过dfs轻易维护出),要么是算了自己的权值计算出的gcd,如果算自己的权值,那么这个gcd一定是这个权值的某个因数,用logn的预处理枚举这个权值的所有因数,只要这个因数出现了d(d是层数,默认根节点是第0层)次以上,那么这个数可以作为beauty值,维护其最大值就好。
代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>using namespace std;#define PB push_back#define ll rt << 1#define rr rt << 1 | 1#define lson l, mid, ll#define rson mid + 1, r, rrtemplate<class T1, class T2> inline void gmax(T1 &a, T2 b) { if (a < b) a = b; }typedef long long LL;const int N = 2e5 + 5;int n;int a[N], b[N], cnt[N];int head[N], enxt[N << 1], ev[N << 1], en;vector<int> factors[N];void dfs(int u, int fa, int d, int g) { b[u] = g; for (int factor: factors[a[u]]) if (++cnt[factor] >= d) gmax(b[u], factor); g = __gcd(g, a[u]); for (int i = head[u]; ~i; i = enxt[i]) { if (ev[i] == fa) continue; dfs(ev[i], u, d + 1, g); } for (int factor: factors[a[u]]) --cnt[factor];}int main() { for (int i = 1; i < N; ++i) for (int j = i; j < N; j += i) factors[j].PB(i); while (~scanf("%d", &n)) { for (int i = 1; i <= n; ++i) { scanf("%d", a + i); head[i] = -1; } int u, v; for (int i = 1; i < n; ++i) { scanf("%d%d", &u, &v); ev[en] = v; enxt[en] = head[u]; head[u] = en++; ev[en] = u; enxt[en] = head[v]; head[v] = en++; } dfs(1, 0, 0, 0); b[1] = a[1]; for (int i = 1; i <= n; ++i) { printf("%d%c", b[i], i == n ? '\n' : ' '); } en = 0; }}/*57 9 3 3 71 21 32 42 5*/
阅读全文
0 0
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(补题)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Codeforces Round #430 (Div. 2)-搜索&STL-Ilya And The Tree
- Codeforces 842 C. Ilya And The Tree 树上dp
- 【DP】 Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- Codeforces Round #297 (Div. 2) C.Ilya and Sticks
- Codeforces Round #297 (Div. 2) C - Ilya and Sticks
- java泛型<? extends E>和<? super E>的区别和适用场景
- Intent.FLAG_ACTIVITY_FORWARD_RESULT作用
- 跳台阶java实现
- 使用wifi连接adb
- QT5.8+VS2015环境配置(全)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- deeplin ( linux ) STS 中不能选择tomcat 的问题
- Tomcat Connector运行模式从bio方式改成nio方式
- oracle 备忘
- Prime Number(CodeForces
- js中的apply和call的用法
- 剑指Offer—58—对称的二叉树
- Python 单线程 多线程
- 系统级性能分析工具 — Perf