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.
Input
On the first line, there is a positive integer n, which describe the number of nodes.
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
Output
Your output should include n lines, for i-th line, output the max number that node i heard.
For the nodes who heard nothing, output -1.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430
题意:
一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的最大值,要求输出每个节点的heard值。
树节点个数1e5,权值为最大1e5。
思路:由于权值最大才1e5,开一个1e5的vector,考虑将树节点存入它权值因子的vector(比如权值为9的结点,将他存入v[1],v[3],v[9]里),然后按照每一个vector建立一棵虚树。将这颗虚树上除了叶子的点的答案都更新。但是由于只要更新非叶子节点的值,我就直接在建立虚树边的时候更新了,省去建边和搜索的过程。
 #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(虚树)的更多相关文章

  1. HDU - 6430 Problem E. TeaTree 2018 Multi-University Training Contest 10 (LCA+枚举因子)

    题意:一棵树,每个点都有自己val(1 <= val <= 1e5),而任意两个点u,v可以对lca(u,v) 产生gcd(valu,valv)的贡献,求每个点能接受到来自子树贡献的最大值 ...

  2. CF1073G Yet Another LCP Problem 后缀自动机 + 虚树 + 树形DP

    题目描述 记 $lcp(i,j)$ 表示 $i$ 表示 $i$ 这个后缀和 $j$ 这个后缀的最长公共后缀长度给定一个字符串,每次询问的时候给出两个正整数集合 $A$ 和 $B$,求$\sum_{i\ ...

  3. hdu 6035 Colorful Tree(虚树)

    考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...

  4. HDU 5687 Problem C 【字典树删除】

    传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...

  5. hdu 6430 线段树 暴力维护

    Problem E. TeaTree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  6. HDU 6035 (虚树)(统计颜色)

    HDU 6035 Colorful Tree Problem : 给一棵树,每个结点有一种颜色,定义每条路径的权值为这条路径上颜色的种数,询问所有路径(C(n,2)条)的权值之和. Solution ...

  7. Codeforces986E Prince's Problem 【虚树】【可持久化线段树】【树状数组】

    我很喜欢这道题. 题目大意: 给出一棵带点权树.对每个询问$ u,v,x $,求$\prod_{i \in P(u,v)}gcd(ai,x)$.其中$ P(u,v) $表示$ u $到$ v $的路径 ...

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

  9. bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...

随机推荐

  1. 【CocoaPods】ERROR: While executing gem ... Gem::DependencyError

    今天安装 CocoaPods 时遇到了这个问题. ERROR: While executing gem ... (Gem::DependencyError) Unable to resolve dep ...

  2. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  3. Button 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  4. Java 设计模式 – Observer 观察者模式

    目录 [隐藏] 1 代码 1.1 观察者接口: 1.2 被观察者: 1.3 观众类 : 1.4 电影类: 1.5 效果如下: 代码 说明都在注释: 观察者接口: package ObserverMod ...

  5. 一个基于TCP/IP的服务器与客户端通讯的小项目(超详细版)

    1.目的:实现客户端向服务器发送数据 原理: 2.建立两个控制台应用,一个为服务器,用于接收数据.一个为客户端,用于发送数据. 关键类与对应方法: 1)类IPEndPoint: 1.是抽象类EndPo ...

  6. print('', end='')

    print函数的end参数,从python3才开始支持,所以如果要使用这种模式,需要对应使用python3

  7. pipreqs 生成requirements.txt文件时编码错误问题

    1,首先安装pipreqs --> pip install pipreqs 2.生成相应项目的路径  --> pipreqs  e:\a\b 在此时可能会遇见 UnicodeDecodeE ...

  8. 假装前端工程师(一)Icework + GitHub pages 快速构建可自定义迭代开发的 react 网站

    icework + gh-pages 超快部署超多模版页面 项目地址:https://github.com/yhyddr/landingpage效果地址:https://yhyddr.github.i ...

  9. java面向对象中的集合

    1.学习集合的原因? A.数组是面向过程的,集合是面向对象的. B.集合是类,具备类的封装,继承,多态...超强功能. C.数组是固定长度,集合是可变长度 D.数组没办法获得真实的元素个数:集合可以. ...

  10. keras 学习-线性回归

    园子里头看到了一些最基础的 keras 入门指导, 用一层网络,可以训练一个简单的线性回归模型. 自己学习了一下,按照教程走下来,结果不尽如人意,下面是具体的过程. 第一步: 生成随机数据,绘出散点图 ...