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. String常量池和intern方法

    String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo ...

  2. Java 设置PDF文档浏览偏好

    在查看PDF文档时,可进行一些浏览偏好设置,例如是否全屏浏览.隐藏或显示菜单栏/工具栏.设置页面布局模式等,下面将通过Java编程的方式来演示如何设置. 使用工具: Free Spire.PDF fo ...

  3. JavaFX OnMouseClick

    在JavaFX开发环境中,遇到一些坑是难免的,而且资料少得可怜! 先说一下我遇到的问题 : 只是一个点击事件而已 : 首先我有这么个界面 : 接下来呢 ? 我需要点击右上角的X,然后显示遮罩,弹出对话 ...

  4. js 双向绑定数据

    let aaa = []; let bbb = [1,2,3]; let ccc = [0,9,8]; aaa = bbb; //此时aaa与bbb被绑定(aaa指向bbb的指向) ,若使用push则 ...

  5. Mybatis获取代理对象

    mybatis-config.xml里标签可以放置多个environment,这里可以切换test和develop数据源 databaseIdProvider提供多种数据库,在xml映射文件里选择da ...

  6. tp5css和js引入问题

    由于以前用的是tp3.2,现在转用tp5开啊个人博客,在引入CSS和JS的时候遇到了一些坑 在3.2时期需要在路径中添加public,而在tp5中则直接引入static即可. 在config.php下 ...

  7. Of efficiency and methodology

    There are only too many articles and books which pertains to the discussion of efficiency and method ...

  8. FutrueTask原理及源码分析

    1.前言 相信很多人了解到FutureTask是因为ThreadPoolExecutor.submit方法,根据ThreadPoolExecutor.submit的使用,我们可以先猜一下FutureT ...

  9. NAS

    NAS, Network Attached Storage, 网络附属存储, 简单来说就是连接在网络上, 可以存储资料的装置.可以用来做私有网盘,同步各种设备的照片.视频.音频和文件. 常见的 NAS ...

  10. 带图标和多行显示的ListBox

    源码https://www.codeproject.com/Articles/15464/Extending-the-ListBox-to-show-more-complex-items 定义控件 u ...