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]之 ...
随机推荐
- mysql 连接url中需要添加useUnicode=true&characterEncoding=UTF-8
下面是示例: 数据库中Username是张三 在数据库配置时没有配置编码 useUnicode=true&characterEncoding=UTF-8 导致期望与实际不同 配置useUni ...
- Oracle 锁表以及解锁
-- kill_exec 列为解锁的语句,copy出来执行即可.select 'alter system kill session ''' || s.sid || ',' || s.serial# | ...
- Laya 小游戏通用框架设计理念
当前在用laya做小游戏开发,做了几个项目,总结了一下游戏中所需要的一些模块,大概理了一下,然后写成一套自己习惯使用的框架 总结了一下其中的模块 大概要分为一下模块 1.Base 模块 存放一些 ...
- ctfhub技能树—sql注入—Cookie注入
手注 打开靶机 查看页面信息 查找cookie 测试是否为cookie注入 抓包 尝试注入 成功查询到数据库名 查询表名 查询字段名 查询字段信息 成功拿到flag sqlmap 查询数据库名 pyt ...
- SAP系统跨平台字符编码转换
SAP系统在进行了夸平台的迁移,可能会遇到操作系统层文件编码不同,导致SAP系统无法识别或者乱码的问题.例如SAP系统从AIX平台迁移到linux平台,SAP应用服务器的编码会发生变化,从4102变化 ...
- SAP轻松访问会话管理器等设置
对于SAP的登陆后初始界面,是有一个配置表,可以进行设置的,例如隐藏SAP的标准菜单,设置轻松访问页面右边的图片内容等等这一切的设置都可以通过维护SSM_CUST表来实现可以通过SM30来维护内容,该 ...
- python 10函数式编程
函数式编程 函数是Python内建支持的一种封装, ...
- Node.js 安全指南
当项目周期快结束时,开发人员会越来越关注应用的"安全性"问题.一个安全的应用程序并不是一种奢侈,而是必要的.你应该在开发的每个阶段都考虑应用程序的安全性,例如系统架构.设计.编码, ...
- (二)基于Netty的高性能Websocket服务器(netty-websocket-spring-boot)
@toc Netty是一款基于NIO(Nonblocking I/O,非阻塞IO)开发的网络通信框架,对比于BIO(Blocking I/O,阻塞IO),他的并发性能得到了很大提高. 1.Netty为 ...
- Docker容器内Mysql大小写敏感方案解决
Docker容器内Mysql大小写敏感方案解决 一.(lower_case_table_names)参数说明 二.Docker 部署 MySql 并修改为大小写不敏感 2.1直接在Docker启动的时 ...