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

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
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;
}

你可能感兴趣的:(ACM,启发式合并)