QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#98973#4229. GCD Harmonytriplem5dsTL 62ms27200kbC++142.0kb2023-04-21 06:08:132023-04-21 06:08:17

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-21 06:08:17]
  • 评测
  • 测评结果:TL
  • 用时:62ms
  • 内存:27200kb
  • [2023-04-21 06:08:13]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define el '\n'
#define FIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
typedef unsigned long long ull;

template<typename T, typename X>
using hashTable = gp_hash_table<T, X>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

// mt19937_64 for long long
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());

const int N = 5e3 + 5;

vector<int> G[N];
int dp[N][1005], curVal[N], g[1005][1005];

int solve(int node, int par, int val) {
    int &ret = dp[node][val];
    if (~ret)
        return ret;

    ret = 0;
    for (auto &ch: G[node]) {
        if (ch == par)
            continue;

        int mn = 1e9;
        for (int newVal = 2; newVal <= 1000; newVal++) {
            if (g[val][newVal] == 1)
                continue;
            mn = min(mn, solve(ch, node, newVal) + (curVal[ch] == newVal ? 0 : newVal));
        }
        ret = min(ret + mn, (int) 1e9);
    }
    return ret;
}

void doWork() {
    int n;
    cin >> n;
    memset(dp, -1, sizeof dp);
    for (int i = 1; i <= n; i++)
        cin >> curVal[i];

    for (int i = 1; i <= 1000; i++)
        for (int j = 1; j <= 1000; j++)
            g[i][j] = __gcd(i, j);


    for (int i = 0, u, v; i < n - 1; i++)
        cin >> u >> v, G[u].push_back(v), G[v].push_back(u);

    int ans = 1e9;
    for (int i = 2; i <= 1000; i++)
        ans = min(ans, solve(1, -1, i) + (curVal[1] == i ? 0 : i));
    cout << ans;
}

signed main() {
    FIO
    int T = 1;
//    cin >> T;
    for (int i = 1; i <= T; i++)
        doWork();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 59ms
memory: 27196kb

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: 48ms
memory: 27200kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 62ms
memory: 27116kb

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: 56ms
memory: 27096kb

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: 59ms
memory: 27108kb

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...

output:


result: