题目链接

题意

给定一棵根为\(1\)的树。定义每个点的美丽值为根节点到它的路径上所有点的\(gcd\)值。但是对于每个点,在计算它的美丽值时,可以将这条路径上某个点的值变为\(0\)来最大化它的美丽值。现问每个点的美丽值最大是多少。

注意,每个点的美丽值计算是独立的,即每次可以选择变为\(0\)的点可以是不同的。

思路

对于某一条路径,考虑上面变化的点。

如果变为\(0\)的是,

根节点

那么可以直接一路往下ans0[v]=gcd(ans0[u],val[v]);(\(u\)是\(v\)的父亲节点);

非根节点

如果不是根节点,而是中间不知道哪一个点,那该怎么办呢?

要想到的是,此时也是有一个不变量的,那就是根——根节点的权值始终不会改变,也就是说整条路径不管怎么变,最终的\(gcd\)值都是根节点的权值的因子

因此,可以预处理出根节点的所有因子,然后\(dfs\)的时候统计路径上的每个因子的个数。对于一个深度为\(dep\)的点来说,只有当根节点到它的路径上某个因子的出现次数\(\geq dep-1\),这个因子才可能是满足条件的\(gcd\). 从大到小枚举即可。

Code

#include <bits/stdc++.h>
#define maxn 200010
using namespace std;
typedef long long LL;
struct Edge {
int to, ne;
Edge(int _to=0, int _ne=0): to(_to), ne(_ne) {}
}edge[maxn * 2];
int tot, t, ne[maxn], ans0[maxn], ans1[maxn], val[maxn], cnt[maxn], divi[maxn];
void add(int u, int v) {
edge[tot] = Edge(v, ne[u]);
ne[u] = tot++;
}
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
void accum(int x, bool sgn) {
for (int i = 0; i < t; ++i) {
if (x % divi[i] == 0) if (sgn==1) ++cnt[i]; else --cnt[i];
}
}
void dfs(int u, int fa, int dep) {
for (int i = ne[u]; ~i; i = edge[i].ne) {
int v = edge[i].to;
if (v == fa) continue;
ans0[v] = gcd(ans0[u], val[v]);
accum(val[v], 1);
for (int j = t-1; j >= 0; --j) if (cnt[j] >= dep) { ans1[v] = divi[j]; break; }
dfs(v, u, dep+1);
accum(val[v], 0);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &val[i]);
memset(ne, -1, sizeof(ne));
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
for (int i = 1; i*i <= val[1]; ++i) {
if (i*i == val[1]) divi[t++] = i;
else if (val[1]%i==0) divi[t++] = i, divi[t++] = val[1]/i;
}
ans0[1] = 0;
sort(divi, divi+t);
dfs(1, -1, 0);
printf("%d", val[1]);
for (int i = 2; i <= n; ++i) printf(" %d", max(ans0[i], ans1[i])); printf("\n");
return 0;
}

Codeforces 842C Ilya And The Tree 树上gcd的更多相关文章

  1. codeforces 842C Ilya And The Tree

    Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...

  2. codeforces 842C Ilya And The Tree (01背包+dfs)

    (点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...

  3. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  4. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

  5. CodeForces 812E Sagheer and Apple Tree 树上nim

    Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...

  6. Codeforces Round #430 (Div. 2) C. Ilya And The Tree

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

  7. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 【cf842C】 Ilya And The Tree(dfs、枚举因子)

    C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...

  9. C. Ilya And The Tree 树形dp 暴力

    C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意d ...

随机推荐

  1. Java基础面试操作题: 线程问题,写一个死锁(原理:只有互相都等待对方放弃资源才会产生死锁)

    package com.swift; public class DeadLock implements Runnable { private boolean flag; DeadLock(boolea ...

  2. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  3. python源码剖析学习记录-01

    学习<Python源码剖析-深度探索动态语言核心技术>教程         Python总体架构,运行流程   File Group: 1.Core Modules 内部模块,例如:imp ...

  4. shell 练习题

    1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 read -p "Please Input A File: " file if [ ! -e ...

  5. linux时区

    1. UTC时区切换到CST 时区# echo "export TZ='Asia/Shanghai'" >> /etc/profile # cat /etc/profi ...

  6. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 FSM Coding Goals

    1.the fsm coding style should be easily modifiable to change state encoding and FSM styles. FSM 的的 状 ...

  7. 妹子图爬取__RE__BS4

    妹子图爬取 页面链接 感谢崔大佬: 原文链接 正则实现代码: import requests import re import os import random class mzitu(): def ...

  8. hdu2604 递推转换矩阵快速幂

    刚开始还以为用位运算与或几下几个循环就搞定了,算着算着发现不行........ 还是一种固定的切题角度,我假设有长度为n,总的排列数位f(n),怎么算他呢?从后往前考虑,因为大多数情况,都是用前面的结 ...

  9. JAVA-基础(六) Java.serialization 序列化

    序 列 化 序列化(serialization)是把一个对象的状态写入一个字节流的过程. Serializable接口 只有一个实现Serializable接口的对象可以被序列化工具存储和恢复.Ser ...

  10. MongoDB学习-->Spring Data Mongodb-->MongodbTemplate

    配置文件application-dev.yml: server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: ma ...