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号点与查询 ...
随机推荐
- l命令练习题1
1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) [root@localhost ~]# mkdir -p /guanli/zonghe | mk ...
- Java 获取操作系统相关的内容
package com.hikvision.discsetup.util; import java.lang.reflect.Field; import java.net.InetAddress; i ...
- Mysql的行级锁与表级锁
在计算机科学中,锁是在执行多线程时用于强行限制资源访问的同步机制,即用于在并发控制中保证对互斥要求的满足. 在DBMS中,可以按照锁的粒度把数据库锁分为行级锁(INNODB引擎).表级锁(MYISAM ...
- DesignPattern系列__10单例模式
单例模式介绍 单例模式,是为了确保在整个软件体统中,某个类对象只有一个实例,并且该类通常会提供一个对外获取该实例的public方法(静态方法). 比如日志.数据库连接池等对象,通常需要且只需要一个实例 ...
- 转载 | 一种让超大banner图片不拉伸、全屏宽、居中显示的方法
现在很多网站的Banner图片都是全屏宽度的,这样的网站看起来显得很大气.这种Banner一般都是做一张很大的图片,然后在不同分辨率下都是显示图片的中间部分.实现方法如下: <html> ...
- python3从入门到精通之数据类型,布尔类型介绍
数据的类型 为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型的. Number(数字) int(整型) float(浮点型) complex(复数) bool(布尔) String( ...
- 【译】为什么要了解HTTP
原文地址:Why should I care about HTTP? 原作信息:by Devon Campbell. Dec 15 '18 Originally published at raddev ...
- .netcore持续集成测试篇之 .net core 2.1项目集成测试
系列目录 从.net到.net core以后,微软非常努力,以每年一到两个大版本的频率在演进.net core,去年相继发布了.net core 2.1和2.2,其中2.1是长期支持版,不断的快速更新 ...
- MYSQL--表与表之间的关系、修改表的相关操作
表与表之间的操作: 如果所有信息都在一张表中: 1.表的结构不清晰 2.浪费硬盘空间 3.表的扩展性变得极差(致命的缺点) 确立表与表之间的关系.一定要换位思考(必须在两者考虑清楚之后才能得出结论) ...
- Springboot源码分析之EnableAspectJAutoProxy
摘要: Spring Framwork的两大核心技术就是IOC和AOP,AOP在Spring的产品线中有着大量的应用.如果说反射是你通向高级的基础,那么代理就是你站稳高级的底气.AOP的本质也就是大家 ...