QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#106263 | #4229. GCD Harmony | zaxwellmen# | TL | 2ms | 3424kb | C++20 | 1.2kb | 2023-05-17 05:05:42 | 2023-05-17 05:05:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define mp make_pair
#define rsz resize
#define pb push_back
int n, m = 101;
vector<int> val;
vector<vector<int>> dp, best, adj;
void dfs(int i, int p) {
for (int nb : adj[i]) if (nb != p) dfs(nb, i);
FOR(j, 1, m) {
if (j == val[i]) dp[i][j] = 0;
else dp[i][j] = j;
for (int nb : adj[i]) if (nb != p) {
int cur = 2*n;
FOR(k, 2, 101) if (j%k==0) cur = min(cur, best[nb][k]);
dp[i][j] += cur;
}
FOR(k, 2, 101) if (j%k==0) best[i][k] = min(best[i][k], dp[i][j]);
// cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
m = max(101, 2*n);
val.rsz(n);
F0R(i, n) cin >> val[i];
adj.rsz(n);
F0R(i, n-1) {
int a,b; cin >> a >> b; a--; b--;
adj[a].pb(b); adj[b].pb(a);
}
dp.assign(n, vector<int>(m, 1e9));
best.assign(n, vector<int>(101, 1e9));
dfs(0,0);
int ans = m;
F0R(j, 2*n) ans = min(ans, dp[0][j]);
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3424kb
input:
6 5 6 3 4 9 12 1 2 1 3 1 4 1 6 3 5
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3348kb
input:
13 2 5 5 5 5 5 5 3 3 3 3 3 3 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 2ms
memory: 3344kb
input:
9 2 5 5 5 5 3 3 3 3 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9
output:
14
result:
ok single line: '14'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3344kb
input:
8 13 13 13 2 13 13 13 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8
output:
13
result:
ok single line: '13'
Test #6:
score: -100
Time Limit Exceeded
input:
5000 59 25 51 26 33 99 2 13 29 58 5 47 96 83 63 22 69 89 41 90 20 97 85 90 55 17 54 75 54 24 90 54 74 78 81 77 2 47 68 18 60 81 99 86 7 6 76 43 17 43 92 25 36 26 47 100 63 66 2 16 47 25 48 86 24 2 10 42 44 80 92 48 49 21 49 40 32 85 53 88 15 30 4 27 77 16 6 80 50 56 46 14 3 48 92 50 93 35 69 29 48 4...