Codeforces:D. GCD Counting(树上点分治)

D. GCD Counting

time limit per test 4.5 seconds
memory limit per test 256 megabytes

Problem Description

You are given a tree consisting of n
vertices. A number is written on each vertex; the number on vertex i is equal to ai.

Let’s denote the function g(x,y) as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex x to vertex y (including these two vertices). Also let’s denote dist(x,y) as the number of vertices on the simple path between vertices x and y, including the endpoints. dist(x,x)=1 for every vertex x.

Your task is calculate the maximum value of dist(x,y) among such pairs of vertices that g(x,y)>1.

Input

The first line contains one integer n — the number of vertices (1≤n≤2⋅10^5).

The second line contains n integers a1, a2, …, an (1≤ai≤2⋅10^5) — the numbers written on vertices.

Then n−1 lines follow, each containing two integers x and y (1≤x,y≤n,x≠y) denoting an edge connecting vertex x with vertex y. It is guaranteed that these edges form a tree.

Output

If there is no pair of vertices x,y such that g(x,y)>1, print 0. Otherwise print the maximum value of dist(x,y) among such pairs.

Examples

Input
3
2 3 4
1 2
2 3

Output
1

Input
3
2 3 4
1 3
2 3

Output
2

Input
3
1 1 1
1 2
2 3

Output
0



题意:给你一棵树,树的每个节点上有个权值,要求你找一条路径,路径上节点权值的gcd不为1,并且路径要最长。

解题心得:

  • 看题意很容易想到树dp,其实点分治也可以解决,复杂度为(nlog(n))只是在重心dfs的时候感觉很暴力,其实只是常数有一点大,题上给了4.5s肯定能过的。
#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 100;

int num[maxn], n, centroid[maxn], Max, subtree_size[maxn];

vector<int> ve[maxn], prim_num[maxn];

bool vis[maxn];

void get_prim_num() {
    for (int i = 2; i < maxn; i++) {
        if (vis[i]) continue;
        prim_num[i].push_back(i);
        for (int j = i + i; j < maxn; j += i) {
            prim_num[j].push_back(i);
            vis[j] = true;
        }
    }
}

void init() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &num[i]);

    for (int i = 1; i < n; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--, b--;
        ve[a].push_back(b);
        ve[b].push_back(a);
    }
}

int compute_subtree_size(int v, int p) {
    int c = 1;
    for (int i = 0; i < ve[v].size(); i++) {
        int w = ve[v][i];
        if (w == p || centroid[w]) continue;
        c += compute_subtree_size(w, v);
    }
    return subtree_size[v] = c;
}

pair<int, int> search_centroid(int v, int p, int t) {
    pair<int, int> res = make_pair(INT_MAX, -1);
    int m = 0, s = 1;
    for (int i = 0; i < ve[v].size(); i++) {
        int w = ve[v][i];
        if (w == p || centroid[w]) continue;

        res = min(res, search_centroid(w, v, t));

        m = max(m, subtree_size[w]);
        s += subtree_size[w];
    }

    m = max(m, t - s);
    res = min(res, make_pair(m, v));

    return res;
}

int dfs(int v, int p, int prim, int len) {
    int Max_len = len;
    for (int i = 0; i < ve[v].size(); i++) {
        int w = ve[v][i];
        if (centroid[w] || w == p) continue;
        if (num[w] % prim == 0) {
            Max_len = max(dfs(w, v, prim, len + 1), Max_len);
        }
    }

    return Max_len;
}

void checke(int v) {
    static vector<int> path;
    int va = num[v];
    if(va > 1) Max = max(Max, 1);
    for (int i = 0; i < prim_num[va].size(); i++) {
        int w = prim_num[va][i];
        path.clear();
        for (int j = 0; j < ve[v].size(); j++) {
            if(centroid[ve[v][j]]) continue;
            if (num[ve[v][j]] % w != 0) {
                if(Max == 0) Max = 1;
                continue;
            }
            path.push_back(dfs(ve[v][j], v, w, 2));
        }

        if (path.size() < 1) return;
        if (path.size() < 2) {
            Max = max(Max, path[0]);
        } else {
            sort(path.begin(), path.end());
            Max = max(Max, path[path.size() - 1] + path[path.size() - 2] - 1);
        }

    }
}

void solve_subproblem(int v) {
    compute_subtree_size(v, -1);
    int s = search_centroid(v, -1, subtree_size[v]).second;
    centroid[s] = true;

    for (int i = 0; i < ve[s].size(); i++) {
        int w = ve[s][i];
        if (centroid[w]) continue;
        solve_subproblem(w);
    }

    checke(s);
    centroid[s] = false;
}

int main() {
    //freopen("1.in", "r", stdin);
    get_prim_num();
    init();
    solve_subproblem(0);
    printf("%d", Max);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值