HDU6430 Problem E. TeaTree【dsu on tree】
Problem E. TeaTree
Problem Description
Recently, TeaTree acquire new knoledge gcd (Greatest Common Divisor), now she want to test you.
As we know, TeaTree is a tree and her root is node 1, she have n nodes and n-1 edge, for each node i, it has it’s value v[i].
For every two nodes i and j (i is not equal to j), they will tell their Lowest Common Ancestors (LCA) a number : gcd(v[i],v[j]).
For each node, you have to calculate the max number that it heard. some definition:
In graph theory and computer science, the lowest common ancestor (LCA) of two nodes u and v in a tree is the lowest (deepest) node that has both u and v as descendants, where we define each node to be a descendant of itself.
给出一棵以\(1\)为根的树,每个点有权值\(w[i]\),两个不同的点\(u,v\)可以使他们的\(lca\)得到一个大小为\(gcd(w[u],w[v])\)值,现在问每个点所能得到的最大值
可以考虑枚举每个\(lca\),然后任意两个不同的子树中的两个值取\(gcd\),这样是\(n^2\)的
考虑启发式合并,用一个数组来记录所有出现过的因子,假设当前处理到点\(u\),现在已经处理出了一些\(u\)的子树中的所有因子了,现在到\(v\)的子树,那就可以先对\(v\)为根的子树中的所有点找出最大出现过的因子,然后再把子树里的所有点的因子加入进去,这样可以防止处理到相同子树中的点
//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int n,w[MAXN],sz[MAXN],son[MAXN],ret[MAXN],res,app[MAXN],st[MAXN],ed[MAXN],id[MAXN],num;
vector<int> d[MAXN],G[MAXN];
void preprocess(){
for(int i = 1; i < MAXN; i++) for(int j = i; j < MAXN; j+=i) d[j].emplace_back(i);
for(int i = 1; i < MAXN; i++) reverse(d[i].begin(),d[i].end());
}
void dfs(int u){
sz[u] = 1; st[u] = ++num; id[num] = u;
for(int v : G[u]){
dfs(v);
sz[u] += sz[v];
if(sz[v]>sz[son[u]]) son[u] = v;
}
ed[u] = num;
}
void update(int val, int inc){ for(int x : d[val]) app[x] += inc; }
int query(int val){ for(int x : d[val]) if(app[x]) return x; return -1; }
void search(int u, bool clear){
for(int v : G[u]) if(v!=son[u]) search(v,true);
if(son[u]) search(son[u],false);
res = -1;
for(int v : G[u]) if(v!=son[u]) {
for(int i = st[v]; i <= ed[v]; i++) res = max(res,query(w[id[i]]));
for(int i = st[v]; i <= ed[v]; i++) update(w[id[i]],1);
}
res = max(res,query(w[u]));
update(w[u],1);
ret[u] = res;
if(clear) for(int i = st[u]; i <= ed[u]; i++) update(w[id[i]],-1);
}
void solve(){
scanf("%d",&n);
for(int i = 2; i <= n; i++){
int par; scanf("%d",&par);
G[par].emplace_back(i);
}
for(int i = 1; i <= n; i++) scanf("%d",&w[i]);
dfs(1);
search(1,false);
for(int i = 1; i <= n; i++) printf("%d\n",ret[i]);
}
int main(){
preprocess();
solve();
return 0;
}
HDU6430 Problem E. TeaTree【dsu on tree】的更多相关文章
- HDU6504 Problem E. Split The Tree【dsu on tree】
Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 ...
- 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...
- HDU4358 Boring counting【dsu on tree】
Boring counting Problem Description In this problem we consider a rooted tree with N vertices. The v ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- HDU6191 Query on A Tre【dsu on tree + 01字典树】
Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- 【Balanced Binary Tree】cpp
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
- POJ 3468 A Simple Problem with Integers 【树状数组】
题目链接:id=3468">http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b.要求输出v[a]到v[b]之 ...
随机推荐
- 利用Python下载:You-Get的安装及使用方法
You-Get是一个非常优秀的网站视频下载工具.使用You-Get可以很轻松的下载到网络上的视频.图片及音乐. 1.打开这个网址https://www.python.org/ 下载并安装Python, ...
- es6 Array.from + new Set 去重复
// es6 set数据结构 生成一个数据集 里面的元素是唯一的 const items = new Set([1, 2, 3, 4, 5, 5, 5, 5]); // items 是个对象 item ...
- 【MySQL 基础】MySQ LeetCode
MySQL LeetCode 175. 组合两个表 题目描述 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+----- ...
- spring ioc踏出第一步
spring IOC(容器) AOP(面向切面编程) IOC容器:他的功能就是可以整合像 Structs2 .Hibernate.Mybatis: IOC:控制反转:所谓的控制就是控制资源的获取方法, ...
- mysql—if函数
在mysql中if()函数的具体语法如下:IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值. 开始实 ...
- MyBatis初级实战之四:druid多数据源
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- idea 启动热部署Devtolls
1.在子工程pom.xml中添加devtools jar包到需要启动的项目中 1 <dependency> 2 <groupId>org.springframework.boo ...
- Django中多表关联的展示问题:
增加一个知识点,当表中有多对多关联时,在前端展示的时候是一个列表,所以在展示的时候需要这样做: 表结构: class ProjectEnv(models.Model): project = model ...
- 聊聊.net应用程序的Docker镜像
要在容器中运行.net应用程序,你需要在容器镜像中安装.net Framework或.net Core 运行时.这不是你需要自己管理的东西,因为微软提供的Docker镜像已经安装了运行时,你可以使用 ...
- Mybatis plus通用字段自动填充的最佳实践总结
在进行持久层数据维护(新增或修改)的时候,我们通常需要记录一些非业务字段,比如:create_time.update_time.update_by.create_by等用来维护数据记录的创建时间.修改 ...