2018 Multi-University Training Contest 10 E. TeaTree(dfs + 启发式合并)

本篇介绍了一道关于树形结构的数据结构题目,通过预处理因子并结合启发式合并来解决最大公因子的问题。文章提供了完整的AC代码实现。

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

TeaTree = 茶树 = 茶叔 = 茶理理。

题目大意:

给出一棵树,每棵树的节点会两两向最近公共祖先传递他们的gcd,求每个节点接收到的最大gcd。

先预处理出数据范围内所有数的因子,然后进行dfs,递归到最下面构造叶子节点的因子集合,然后一层层向上合并,当因子重复出现的时候,就记录到了“公因子”,选取重复出现因子中的最大值,就得到了“最大公因子”。

合并的时候使用启发式合并降低复杂度。

ac代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100005;
vector<int> V[maxn], G[maxn];
set<int> S[maxn];
int ans[maxn], in[maxn];


void init() {
    //预处理因子
    for(int i = 2; i < maxn; i++) {
        for(int j = i; j < maxn; j += i) {
            V[j].push_back(i);
        }
    }
}

int merge(int x, int y) {
    int ret = 1;
    //启发式合并,小的合并到大的上
    if(S[x].size() < S[y].size()) {
        swap(S[x], S[y]);
    }

    for(set<int>::iterator it = S[y].begin(); it != S[y].end(); it++) {
        if(S[x].count(*it)) {
            ret = max(ret, *it);
        } else {
            S[x].insert(*it);
        }
    }

    S[y].clear();

    return ret;
}

void dfs(int x) {
    for(int i = 0; i < G[x].size(); i++) {
        int j = G[x][i];
        dfs(j);
        ans[x] = max(ans[x], merge(x, j));
    }

    for(int i = 0; i < V[in[x]].size(); i++) {
        int j = V[in[x]][i];
        if(S[x].count(j)) {
            ans[x] = max(ans[x], j);
        } else {
            S[x].insert(j);
        }
    }
}

int main() {
    int n;
    init();
    while(~scanf("%d", &n)) {
        for(int i=1; i<=n; i++){
            G[i].clear();
            S[i].clear();
            ans[i] = -1;
        }
        int fa;
        for(int i = 2; i <= n; i++) {
            scanf("%d", &fa);
            ans[fa] = 1; 
            G[fa].push_back(i);
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &in[i]);
        }
        dfs(1);
        for(int i = 1; i <= n; i++) {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
}
用户表: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id 是该表的主键(具有唯一值的列)。 该表中的每行包括用户 ID 和用户名。 注册表: Register +-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+ (contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 该表中的每行包含用户的 ID 和他们注册的赛事。 编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 返回结果如下示例所示。 示例 1: 输入: Users 表: +---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+ Register 表: +------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+ 输出: +------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 解释: 所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值