HDU 6430 Problem E. TeaTree(虚树)
Problem E. TeaTree
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.
Next line there are n-1 positive integers f[2] ,f[3], …, f[n], f[i] describe the father of node i on tree.
Next line there are n positive integers v[2] ,v[3], …, v[n], v[i] describe the value of node i.
n<=100000, f[i]<i, v[i]<=100000
For the nodes who heard nothing, output -1.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = ; struct Edge ///存原树
{
int to,next;
} E[maxn << ];
int frist[maxn], sign = ;
inline void AddEdge(int u, int v)
{
E[sign].to = v;
E[sign].next = frist[u];
frist[u] = sign++;
} /*
struct void_tree ///存虚树
{
int to,next;
}vtree[maxn];
int sign2=0,first2[maxn];
inline void add_edge(int u, int v)
{
vtree[sign2].to = v;
vtree[sign2].next = first2[u];
first2[u] = sign2++;
}
*/
int dfn[maxn]; ///dfs序
int deep[maxn]; ///节点深度
int s[maxn]; ///栈
int a[maxn]; ///存需要建立虚树的点
int index=;
int top; ///栈顶 int fa[][maxn]; void dfs(int u) ///预处理dep和fa[0][j]
{
dfn[u] = ++index;
for(int i=frist[u];i;i=E[i].next)
{
if(E[i].to!=fa[][u])
{
deep[E[i].to]=deep[u]+;
fa[][E[i].to]=u;
dfs(E[i].to);
}
}
}
inline int LCA(int u,int v) ///求LCA
{
if(deep[u]>deep[v])swap(u,v);
for(int i=;~i;i--)
if(deep[fa[i][v]]>=deep[u])
v=fa[i][v];
if(u==v)return u;
for(int i=;~i;i--)
if(fa[i][u]!=fa[i][v])
{
u=fa[i][u];
v=fa[i][v];
}
return fa[][u];
}
int answer[maxn]; inline void insert_point(int x,int num) ///建立虚树
{
if(top == )
{
s[++top] = x;
return ;
}
int lca = LCA(x, s[top]);
if(lca == s[top])
{
s[++top]=x;
return ;
}
while(top >= && dfn[s[top - ]] >= dfn[lca])
{
answer[s[top - ]]=num; ///不建边了,直接更新答案
//add_edge(s[top - 1], s[top]);
top--;
}
if(lca != s[top])
{
answer[lca]=num;
//add_edge(lca, s[top]);
s[top] = lca;
}
s[++top] = x;
} inline int comp(const int &a, const int &b)
{
return dfn[a] < dfn[b];
} vector<int>mmp[maxn]; int main()
{
int n,x,m;
memset(answer,-, sizeof(answer));
memset(frist, -, sizeof(frist));
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&x);
AddEdge(x,i);
} deep[]=fa[][]=;
dfs();
for(int i=;i<=;i++)
for(int j=;j<=n;j++)
fa[i][j]=fa[i-][fa[i-][j]]; ///将节点存入它权值因子的vector
int Max = ;
for(int i=; i<=n; i++)
{
scanf("%d",&x);
Max = max(Max,x);
int temp = sqrt(x+);
for(int j=; j<=temp; j++)
{
if(x%j==)
{
mmp[j].push_back(i);
if(j!=x/j)
mmp[x/j].push_back(i);
}
}
} for(int k=; k<=Max; k++)
{
m = mmp[k].size();
if(m<=)continue;
for(int i = ; i <= m; i++)a[i] = mmp[k][i-];
sort(a + , a + m + , comp);
//memset(first2,-1,sizeof(first2));
top=;
for(int i = ; i <= m; i++)
insert_point(a[i],k);
while(top > )
{
answer[s[top - ]]=k; ///不建边了,直接更新答案
//add_edge(s[top - 1], s[top]);
top--;
}
//dfs(s[top]); ///省去dfs
}
for(int i=;i<=n;i++)
printf("%d\n",answer[i]);
return ;
}
HDU 6430 Problem E. TeaTree(虚树)的更多相关文章
- HDU - 6430 Problem E. TeaTree 2018 Multi-University Training Contest 10 (LCA+枚举因子)
题意:一棵树,每个点都有自己val(1 <= val <= 1e5),而任意两个点u,v可以对lca(u,v) 产生gcd(valu,valv)的贡献,求每个点能接受到来自子树贡献的最大值 ...
- CF1073G Yet Another LCP Problem 后缀自动机 + 虚树 + 树形DP
题目描述 记 $lcp(i,j)$ 表示 $i$ 表示 $i$ 这个后缀和 $j$ 这个后缀的最长公共后缀长度给定一个字符串,每次询问的时候给出两个正整数集合 $A$ 和 $B$,求$\sum_{i\ ...
- hdu 6035 Colorful Tree(虚树)
考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...
- HDU 5687 Problem C 【字典树删除】
传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...
- hdu 6430 线段树 暴力维护
Problem E. TeaTree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Oth ...
- HDU 6035 (虚树)(统计颜色)
HDU 6035 Colorful Tree Problem : 给一棵树,每个结点有一种颜色,定义每条路径的权值为这条路径上颜色的种数,询问所有路径(C(n,2)条)的权值之和. Solution ...
- Codeforces986E Prince's Problem 【虚树】【可持久化线段树】【树状数组】
我很喜欢这道题. 题目大意: 给出一棵带点权树.对每个询问$ u,v,x $,求$\prod_{i \in P(u,v)}gcd(ai,x)$.其中$ P(u,v) $表示$ u $到$ v $的路径 ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
随机推荐
- 目标检测评价指标mAP 精准率和召回率
首先明确几个概念,精确率,召回率,准确率 精确率precision 召回率recall 准确率accuracy 以一个实际例子入手,假设我们有100个肿瘤病人. 95个良性肿瘤病人,5个恶性肿瘤病人. ...
- 泥瓦匠 5 年 Java 的成长感悟(下)
继续<泥瓦匠 5 年 Java 的成长感悟(上)>,大致包括下面几点: 学技术的心态 学技术的学法 工作的心态 工作的硬技能 工作的软实力 听点雷子的民谣,我就安静地感概感概.上次说写的, ...
- 自定义SWT控件六之自定义Tab
6.自定义tab 本章节提供的自定义tab 分为两类 tab上带删除按钮和添加按钮,可删除tab和添加tab tab不可删除和添加 6.1 不可删除tab package com.view.contr ...
- Cassandra之Docker环境实践
Cassandra简介 Cassandra是一个开源分布式NoSQL数据库系统. 它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon D ...
- Windows 纠错
4:在Windows应用程序中,当需要将窗体显示为模式对话框时,需要调用窗体的()方法.(选择一项)A:Activate()B:ShowDialog()C:Show()D:Close()正确答案是 B ...
- C#打开并选择特定类型文件并返回文件名
public string[] GetOpenFileDialogReturnFileFullName(bool multiSelect = false) { ...
- ssm 搭建项目各项配置
首先配置 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- shardingjdbc 强制路由走主库查询实时数据 避免主从同步数据延迟
@Beanpublic shardingsphere.demo.entity.Order order(){ shardingsphere.demo.entity.Order order=new Ord ...
- Hadoop学习(9)-spark的安装与简单使用
spark和mapreduce差不多,都是一种计算引擎,spark相对于MapReduce来说,他的区别是,MapReduce会把计算结果放 在磁盘,spark把计算结果既放在磁盘中有放在内存中,ma ...
- coursera课程《how to learning 怎么学习》 总结
总体来说,学完课程没有茅舍顿开的感觉,而是更加印证了之前的那个认知:大道至简,践则无敌,很多的学习方法上学的时候老师都教过我们,关键是我们能否坚持执行.课程讲了很多脑科学有关学习的知识,但对于我们实践 ...