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. 【SVN】SVN Working copy is too old

    前天在使用 SVN 客户端 CornerStone 的时候遇到了这个问题,代码不能提交了…… 遇到这个问题的时候怎么办? 解决办法: 找到报错对应的文件夹,里面有个 .svn 的文件夹,去掉再 com ...

  2. 在ABP中灵活使用AutoMapper

    demo地址:ABP.WindowsService 该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. AutoMapper简介 Auto ...

  3. Intent 常用方法总结

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本文主要是总结Intent 常用的方法,并封装成Utils类中 主要涉及以下内容 ...

  4. ieda控制台缓冲区限制问题

    一.现象 控制台输出数据若超过默认值时,将从后向前取默认值大小数据(1024) 二.解决方案 1.配置文件(idea安装目录/bin/idea.properties) 2.找到该栏:idea.cycl ...

  5. Unity的弱联网Json数据传输

    注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...

  6. html的一些基本属性介绍

    一.html的属性类型: 1.常见标签属性: a.<h1>:align对其方式      例如:<h1  align="right"> hhhhh</ ...

  7. Mysql-巧用join来优化sql

    0. 准备相关表来进行接下来的测试 相关建表语句请看:https://github.com/YangBaohust/my_sql user1表,取经组 +----+-----------+------ ...

  8. python第一课--基础知识

    python简介 Python是一种计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的 ...

  9. Feign详细构建过程及自定义扩展

    探究清楚 feign 的原理,自定义 feign 功能 **spring-cloud-openfeign-core-2.1.1.RELEASE.jar** 中 **HystrixFeign** 的详细 ...

  10. reponse.addHeader中文名字乱码