QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65311 | #4584. Not One | T3alaadl3k2olyehymn3k# | WA | 83ms | 16744kb | C++17 | 1.4kb | 2022-11-29 18:25:29 | 2022-11-29 18:25:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, P = 19;
int n, w[N], tree_size[N];
int sp[P][2 * N], LOG[2 * N];
vector<int> g[N];
vector<int> flat, tmp;
int st[N], ed[N];
int dfs(int u, int p) {
st[u] = (int) flat.size();
flat.push_back(w[u]);
int ans = 1;
for (int v : g[u])
if (v != p)
ans += dfs(v, u);
ed[u] = (int) flat.size();
flat.push_back(w[u]);
return tree_size[u] = ans;
}
void build_sp() {
for (int i = 0; i < flat.size(); ++i)
sp[0][i] = flat[i];
for (int i = 2; i <= flat.size(); ++i)
LOG[i] = LOG[i / 2] + 1;
for (int p = 1; p < P; ++p) {
for (int i = 0; i + (1 << p) <= P; ++i) {
int x = sp[p - 1][i];
int y = sp[p - 1][i + (1 << (p - 1))];
sp[p][i] = __gcd(x, y);
}
}
}
int query(int l, int r) {
int p = LOG[r - l + 1];
return __gcd(sp[p][l], sp[p][r - (1 << p) + 1]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> w[i];
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
build_sp();
int ans = 0;
for (int u = 1; u <= n; ++u)
if (query(st[u], ed[u]) > 1)
ans = max(ans, tree_size[u]);
cout << ans << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 11904kb
input:
7 10 5 8 6 10 6 4 1 2 2 3 2 4 4 5 4 6 4 7
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 2ms
memory: 10356kb
input:
4 1 1 1 1 1 2 2 3 3 4
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 4ms
memory: 11520kb
input:
5 100 100 100 100 100 3 4 1 2 3 5 2 4
output:
5
result:
ok single line: '5'
Test #4:
score: 0
Accepted
time: 0ms
memory: 10436kb
input:
2 1 1 1 2
output:
0
result:
ok single line: '0'
Test #5:
score: -100
Wrong Answer
time: 83ms
memory: 16744kb
input:
100000 860163 795323 862289 543383 792647 337047 353985 959953 874318 573652 69827 958063 571741 704399 311826 920477 792478 151531 872269 592307 853819 865817 940735 620657 937154 696551 749279 552523 836161 707467 389626 459089 563763 668884 810391 639709 419361 580342 519595 836124 494959 669379 ...
output:
2
result:
wrong answer 1st lines differ - expected: '213', found: '2'