time limit per test 4.5 seconds
memory limit per test 256 megabytes
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.
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.
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.
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,并且路径要最长。
解题心得:
#include
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;
}