QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#413071#4229. GCD HarmonyDanielChang#TL 1ms3756kbC++171.4kb2024-05-17 02:25:292024-05-17 02:25:29

Judging History

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

  • [2024-05-17 02:25:29]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3756kb
  • [2024-05-17 02:25:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main(){
	ios::sync_with_stdio(false); cin.tie(0);

	int n;
	cin >> n;

	const int N = 2*n + 1;

	vector<vector<int>> f(N);
	for(int i=2; i<N; i++){
		for(int j=2; j<N; j++){
			if(gcd(i, j) > 1) f[i].push_back(j);
		}
	}

	vector<int> arr(n);
	for(auto &d : arr) cin >> d;
	vector<vector<int>> adj(n);
	for(int i=1; i<n; i++){
		int a, b;
		cin >> a >> b;
		a--, b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	const int INF = 1e9;
	vector<vector<int>> dp(n, vector<int>(N, -1));
	function<void(int, int)> dfs = [&](int u, int par){
		for(int c : adj[u]){
			if(c == par) continue;
			dfs(c, u);
		}
		for(int i=2; i<N; i++){
			dp[u][i] = arr[u] == i ? 0 : i;
			for(int c : adj[u]){
				if(c == par) continue;
				int mnC = INF;
				for(int j : f[i]){
					if(dp[c][j] != -1) mnC = min(mnC, dp[c][j]);
				}
				if(mnC == INF){
					dp[u][i] = -1;
					break;
				}
				dp[u][i] += mnC;
				if(dp[u][i] > 2*n){
					dp[u][i] = -1;
					break;
				}
			}
			if(dp[u][i] > 2*n){
				dp[u][i] = -1;
				break;
			}
		}
	};
	dfs(0, -1);	
	int res = INF;
	for(int i=2; i<N; i++){
		if(dp[0][i] != -1) res = min(res, dp[0][i]);
	}
	assert(res != INF);
	cout << res;
}
// gcc a.cpp -std=gnu++14 -o a.exe
// g++ a.cpp -g -O2 -std=gnu++14 -static -o a.exe

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3516kb

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: 3744kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3756kb

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: 0ms
memory: 3748kb

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: 3608kb

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: