Codeforces 842C Ilya And The Tree 树上gcd
题目链接
题意
给定一棵根为\(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的更多相关文章
- 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 ...
- codeforces 842C Ilya And The Tree (01背包+dfs)
(点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...
- 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 ...
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
- CodeForces 812E Sagheer and Apple Tree 树上nim
Sagheer and Apple Tree 题解: 先分析一下, 如果只看叶子层的话. 那么就相当于 经典的石子问题 nim 博弈了. 那我们看非叶子层. 看叶子层的父亲层. 我们可以发现, 如果从 ...
- 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 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【cf842C】 Ilya And The Tree(dfs、枚举因子)
C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...
- C. Ilya And The Tree 树形dp 暴力
C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意d ...
随机推荐
- Missing letters-freecodecamp算法题目
Missing letters 1.要求 从传递进来的字母序列中找到缺失的字母并返回它. 如果所有字母都在序列中,返回 undefined. 2.思路 设定缺失变量miss 在for循环遍历字符串的各 ...
- pandas归一化操作
归一化操作有两种 1.max和min的归一化操作 min-max标准化(Min-Max Normalization) 返回结果0~1 公式: 实例: 如: 随机生成假数据如下 df = DataFra ...
- Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食
1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...
- vagrant 安装ubuntu12.04 64 bit
1 下载用于ubuntu 12.04 用于vagrant的镜像,虚拟机是virtualbox $ wget http://files.vagrantup.com/precise64.box jb@e3 ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- luogu1578 奶牛浴场 枚举点最大子矩阵
建议看看王知昆dalao的论文,讲得很好 #include <algorithm> #include <iostream> #include <cstring> # ...
- Xampp 配置出现403无法访问
找到\xampp\apache\conf\httpd.conf配置文件 Access forbidden! You don’t have permission to access the reques ...
- [转]查看Linux版本信息
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/version Linux version 2.6 ...
- [整理]配置SSH密钥自动登录远程服务器
原理: 公钥私钥匹配通过验证,允许访问服务器. 简单步骤: 1.在本地创建一对密钥 2.将公钥传到需要访问的服务器上 3.将公钥放入服务器的authorized_keys,确保访问时能通过验证 4.本 ...
- Windows杂技
WINDOWS下 ,在某目录下按住shift加鼠标右键,可以直接有当前目录的dos窗口 win10不能用 解决办法是在当前文件夹的地址栏输入cmd然后回车 发现Powershell可以当作dos执行相 ...