Problem E. TeaTree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
Sample Input
4 1 1 3 4 1 6 9
Sample Output
2 -1 3 -1
题意:
一棵n个节点的树,1号节点为根,每个节点都有一个权值vi(≤100000), 对于每个节点x,找出Gcd(ai, bi)的最大值,其中x点必须是a和b的最近公共祖先,且a!=b,如果当前节点是叶子输出-1
题解:
- 考虑暴力dp,dp[x][y]表示x节点所在的子树中是否存在某个节点的权值是y的倍数(1or0)
- 对于当前节点x的其中两个儿子u, v,如果存在p满足dp[u][p] = dp[v][p] = 1,那么p就是x点的一个可能的解,找到最大的p
这个dp时间复杂度和空间复杂度都是n²的, 肯定不行
- 可以发现dp[x][y]要不是0要不是1,并且满足dp[u][p] = dp[v][p] = 1的p只需要一个且运算即可找到,所以可以bitset优化
时间复杂度和空间复杂度都降为n²/32,这个时间复杂度是可以过得,但是空间复杂度还是不行
- 然后还可以发现其实≥500的因子实际上非常少,所以其实可以只对≤500的因子进行上述操作,对于>500的因子直接暴力
- 每个节点开个vector或链表存所有的>500因子,然后每次合并都将因子少的那个合并到因子大的上面,并且清空链表
玄学复杂度(<600ms, 80M)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<bitset>
#include<iostream>
using namespace std;
#define LL long long
vector<int> G[100005], P[100005], temp;
bitset<503> B[100005], now;
int val[100005], ans[100005], flag[100005];
int Check(bitset<503> p)
{
int i;
for(i=500;i>=0;i--)
{
if(p[i])
return i;
}
return -1;
}
int Check2(int u, int v)
{
int now, i, k;
now = -1;
for(i=0;i<P[u].size();i++)
flag[P[u][i]] = 1;
for(i=0;i<P[v].size();i++)
{
k = P[v][i];
if(flag[k]==1)
now = max(now, k);
else
P[u].push_back(k);
}
P[v].clear();
for(i=0;i<P[u].size();i++)
flag[P[u][i]] = 0;
return now;
}
void Sech(int u)
{
int i, v;
for(i=1;i*i<=val[u];i++)
{
if(val[u]%i==0)
{
B[u][i] = 1;
if(val[u]/i<=500) B[u][val[u]/i] = 1;
P[u].push_back(val[u]/i);
}
}
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
Sech(v);
now = B[v]&B[u];
ans[u] = max(ans[u], Check(now));
ans[u] = max(ans[u], Check2(u, v));
B[u] |= B[v];
}
}
int main(void)
{
int n, i, x;
scanf("%d", &n);
memset(ans, -1, sizeof(ans));
for(i=2;i<=n;i++)
{
scanf("%d", &x);
G[x].push_back(i);
}
for(i=1;i<=n;i++)
scanf("%d", &val[i]);
Sech(1);
for(i=1;i<=n;i++)
printf("%d\n", ans[i]);
return 0;
}