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】的更多相关文章

  1. 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 ...

  2. 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...

  3. HDU4358 Boring counting【dsu on tree】

    Boring counting Problem Description In this problem we consider a rooted tree with N vertices. The v ...

  4. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  5. 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, ...

  6. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  7. 【Balanced Binary Tree】cpp

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  8. 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】

    平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...

  9. POJ 3468 A Simple Problem with Integers 【树状数组】

    题目链接:id=3468">http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b.要求输出v[a]到v[b]之 ...

随机推荐

  1. Tengine 四层代理:

    Tengine 四层代理: 1 ) 安装tengine ( nginx1.9 以上版本 编译以后要支持stream 模块) 1.1 ) tengine(nginx) 一定要是nginx-1.9.X 以 ...

  2. python模块详解 | selenium(持续更新中)

    目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...

  3. mysqldump 的-T参数

    /usr/local/mysql/bin/mysqldump -uroot -T /tmp lina xuehao 把lina数据库中的xuehao表在tmp目录下备份出来两个文件,一个是纯数据.tx ...

  4. Ice系列--强大如我IceGrid

    前言 IceGrid是一个提供服务定位和服务激活的组件,但它的功能远不止于此.从它的命名可以看出它的设计理念-网格计算(grid computing).网格计算被定义为由一系列关联的廉价计算机组成的计 ...

  5. MySQL增删改操作

    增删改操作 增加 看语法 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3-字段n) VALUES(值1,值2,值3-值n); #指定字段来插入数据,插入 ...

  6. 网络优化之net.ipv4.tcp_tw_recycle和tcp_tw_reuse参数

    网络优化之net.ipv4.tcp_tw_recycle和tcp_tw_reuse参数 - 一个人默默潜行 - 博客园 https://www.cnblogs.com/ppp1314520818/p/ ...

  7. __init__ raises an exception, then __del__ will still be called

    issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org ...

  8. 后台故障&性能分析常用工具

    说明 本文是一个归纳总结,把常用的一些指令,及它们常用的option简单记录了一下,目的是当我们需要工具去定位问题的时候,能够从中找到合适的工具,具体的用法网上有很多博文了,当然还有man手册.参考了 ...

  9. Oracle数据库之——分组查询,子查询及添加,更新,删除

    分组查询 写的顺序: select...from...where... group by...having...order by... 执行顺序: from...where...group by... ...

  10. Linux-处理用户输入

    Linux-处理用户输入 1.命令行参数 1.2读取参数 1.3 读取脚本名 1.4测试参数 2.特殊参数变量 2.1 参数统计 2.2抓取所有的数据 3.移动变量 4.处理选项 5.选项标准化 6. ...