QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#735417#6814. Group HomeworkQFshengxiuTL 673ms124388kbC++231.4kb2024-11-11 19:50:482024-11-11 19:56:22

Judging History

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

  • [2024-11-11 19:56:22]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:TL
  • 用时:673ms
  • 内存:124388kb
  • [2024-11-11 19:55:17]
  • hack成功,自动添加数据
  • (/hack/1167)
  • [2024-11-11 19:50:48]
  • 评测
  • 测评结果:100
  • 用时:666ms
  • 内存:124596kb
  • [2024-11-11 19:50:48]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
using PII = pair<int, int>;
const int N = 2e5 + 10;
int a[N];
int n, cnt, cnt2;
vector<int> g[N];
map<int, int> f[N], d[N];

int dfs(int u, int fa)
{

	if (d[u][fa])
		return d[u][fa];
	int maxn = a[u];
	for (auto v : g[u])
	{
		if (v == fa)
			continue;
		maxn = max(maxn, dfs(v, u) + a[u]);
	}

	return d[u][fa] = maxn;
}
int dfs2(int u, int fa)
{

	if (f[u][fa])
		return f[u][fa];
	vector<int> nx = {0, 0};
	int maxn = 0;
	for (auto v : g[u])
	{
		if (v == fa)
			continue;
		maxn = max(maxn, dfs2(v, u));
		nx.push_back(dfs(v, u));
	}
	sort(nx.begin(), nx.end(), [&](int a, int b)
		 { return a > b; });
	return f[u][fa] = max(maxn, nx[0] + nx[1] + a[u]);
}
void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		vector<int> dx = {0, 0, 0, 0};
		for (auto v : g[i])
			dx.push_back(dfs(v, i));
		sort(dx.begin(), dx.end(), [&](int a, int b)
			 { return a > b; });
		ans = max(ans, dx[0] + dx[1] + dx[2] + dx[3]);
	}
	for (int i = 1; i <= n; i++)
	{
		for (auto v : g[i])
		{
			ans = max(ans, dfs2(v, i) + dfs2(i, v));
		}
	}
	cout << ans << endl;
}
signed main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T = 1;
	solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 22496kb

input:

5
1 9 9 9 9
1 2
1 3
1 4
1 5

output:

36

result:

ok 1 number(s): "36"

Test #2:

score: 0
Accepted
time: 3ms
memory: 23076kb

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 3ms
memory: 22664kb

input:

3
1 2 3
1 2
2 3

output:

6

result:

ok 1 number(s): "6"

Test #4:

score: 0
Accepted
time: 4ms
memory: 25024kb

input:

2
10000 10000
1 2

output:

20000

result:

ok 1 number(s): "20000"

Test #5:

score: 0
Accepted
time: 3ms
memory: 22780kb

input:

7
9 1 5 1 1 5 8
1 2
2 3
2 4
4 5
5 6
5 7

output:

29

result:

ok 1 number(s): "29"

Test #6:

score: 0
Accepted
time: 7ms
memory: 22952kb

input:

7
1 9 9 9 9 9 9
1 2
2 3
2 4
1 5
5 6
5 7

output:

54

result:

ok 1 number(s): "54"

Test #7:

score: 0
Accepted
time: 0ms
memory: 22696kb

input:

7
9 1 9 9 9 9 9
2 1
1 3
1 4
2 5
5 6
5 7

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 0ms
memory: 22560kb

input:

7
9 9 9 1 9 9 9
4 2
2 3
2 1
4 5
5 6
5 7

output:

54

result:

ok 1 number(s): "54"

Test #9:

score: 0
Accepted
time: 0ms
memory: 25428kb

input:

7
9 9 9 9 1 9 9
5 2
2 3
2 4
5 1
1 6
1 7

output:

54

result:

ok 1 number(s): "54"

Test #10:

score: 0
Accepted
time: 8ms
memory: 26388kb

input:

7
9 9 9 9 9 9 1
7 2
2 3
2 4
7 5
5 6
5 1

output:

54

result:

ok 1 number(s): "54"

Test #11:

score: 0
Accepted
time: 111ms
memory: 94976kb

input:

200000
1 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 100...

output:

1999990000

result:

ok 1 number(s): "1999990000"

Test #12:

score: 0
Accepted
time: 7ms
memory: 26056kb

input:

7
1 9 9 9 9 9 9
1 2
1 3
1 4
1 5
3 6
4 7

output:

54

result:

ok 1 number(s): "54"

Test #13:

score: 0
Accepted
time: 7ms
memory: 22860kb

input:

7
9 9 9 9 1 9 9
5 2
5 3
5 4
5 1
3 6
4 7

output:

54

result:

ok 1 number(s): "54"

Test #14:

score: 0
Accepted
time: 139ms
memory: 98760kb

input:

200000
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000...

output:

1999990000

result:

ok 1 number(s): "1999990000"

Test #15:

score: 0
Accepted
time: 156ms
memory: 104756kb

input:

200000
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000...

output:

1999990000

result:

ok 1 number(s): "1999990000"

Test #16:

score: 0
Accepted
time: 4ms
memory: 24172kb

input:

3000
1045 740 2434 1261 1464 880 428 1004 998 684 1602 1867 257 1400 879 377 57 2369 1295 1034 627 620 1392 1255 325 342 2270 970 50 481 2482 2013 2364 361 2496 86 323 2000 1212 1289 1279 1044 2397 490 1272 1258 2284 396 2283 1172 2341 970 174 442 2112 1781 1504 2207 1977 828 200 1327 864 1975 776 1...

output:

3726293

result:

ok 1 number(s): "3726293"

Test #17:

score: 0
Accepted
time: 3ms
memory: 26660kb

input:

3000
3346 3640 2625 3369 3764 2814 3492 3842 4260 4433 3749 3750 4017 3944 4514 4841 3303 4722 2743 2717 4251 4494 2847 4705 3224 3177 3175 4153 3830 2755 3519 4497 2988 4704 4538 3774 4804 3167 4393 3811 4226 4930 4286 4724 4124 3946 2999 3250 2722 3665 3865 3866 4589 4321 4022 3224 3612 2595 3338 ...

output:

11264882

result:

ok 1 number(s): "11264882"

Test #18:

score: 0
Accepted
time: 3ms
memory: 27384kb

input:

3000
6349 6488 7302 5045 7359 6980 5443 6516 5961 6012 7251 5022 6700 7396 6066 6164 7040 7188 6927 5361 7001 7013 5474 6812 6452 6411 6221 6335 5680 5884 6276 5807 5947 5062 6067 6321 6420 5672 7194 6981 5429 6643 5899 7168 6412 6522 7152 6683 5421 5889 6583 5233 6663 7289 5719 7004 5452 6782 7456 ...

output:

9406295

result:

ok 1 number(s): "9406295"

Test #19:

score: 0
Accepted
time: 3ms
memory: 25692kb

input:

3000
8987 8549 9238 8668 8861 9781 7841 8526 7982 9240 9515 9592 9826 7690 8887 9650 9976 8115 8841 9885 9857 7530 8752 7707 9973 8719 8778 8215 7695 7671 8951 9872 8905 9234 7520 9983 8274 8301 8563 9942 8474 8875 9697 8596 9335 7872 9317 7984 9698 9952 8884 9537 8850 7905 9355 7926 8862 9104 9667 ...

output:

26262559

result:

ok 1 number(s): "26262559"

Test #20:

score: 0
Accepted
time: 498ms
memory: 124388kb

input:

200000
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000...

output:

2000000000

result:

ok 1 number(s): "2000000000"

Test #21:

score: 0
Accepted
time: 501ms
memory: 113444kb

input:

200000
1045 740 2434 1261 1464 880 428 1004 998 684 1602 1867 257 1400 879 377 57 2369 1295 1034 627 620 1392 1255 325 342 2270 970 50 481 2482 2013 2364 361 2496 86 323 2000 1212 1289 1279 1044 2397 490 1272 1258 2284 396 2283 1172 2341 970 174 442 2112 1781 1504 2207 1977 828 200 1327 864 1975 776...

output:

249892209

result:

ok 1 number(s): "249892209"

Test #22:

score: 0
Accepted
time: 509ms
memory: 108180kb

input:

200000
3346 3640 2625 3369 3764 2814 3492 3842 4260 4433 3749 3750 4017 3944 4514 4841 3303 4722 2743 2717 4251 4494 2847 4705 3224 3177 3175 4153 3830 2755 3519 4497 2988 4704 4538 3774 4804 3167 4393 3811 4226 4930 4286 4724 4124 3946 2999 3250 2722 3665 3865 3866 4589 4321 4022 3224 3612 2595 333...

output:

750185584

result:

ok 1 number(s): "750185584"

Test #23:

score: 0
Accepted
time: 663ms
memory: 99540kb

input:

200000
6349 6488 7302 5045 7359 6980 5443 6516 5961 6012 7251 5022 6700 7396 6066 6164 7040 7188 6927 5361 7001 7013 5474 6812 6452 6411 6221 6335 5680 5884 6276 5807 5947 5062 6067 6321 6420 5672 7194 6981 5429 6643 5899 7168 6412 6522 7152 6683 5421 5889 6583 5233 6663 7289 5719 7004 5452 6782 745...

output:

624877553

result:

ok 1 number(s): "624877553"

Test #24:

score: 0
Accepted
time: 673ms
memory: 101488kb

input:

200000
8987 8549 9238 8668 8861 9781 7841 8526 7982 9240 9515 9592 9826 7690 8887 9650 9976 8115 8841 9885 9857 7530 8752 7707 9973 8719 8778 8215 7695 7671 8951 9872 8905 9234 7520 9983 8274 8301 8563 9942 8474 8875 9697 8596 9335 7872 9317 7984 9698 9952 8884 9537 8850 7905 9355 7926 8862 9104 966...

output:

875216034

result:

ok 1 number(s): "875216034"

Test #25:

score: 0
Accepted
time: 664ms
memory: 101748kb

input:

200000
1531 2195 2132 774 2490 1477 1320 477 2483 1862 2339 1497 1856 614 1251 2088 968 2234 401 813 1679 881 919 2415 223 2465 1232 1913 2080 498 2250 1534 2483 1318 1015 1185 987 703 706 215 593 642 1499 355 988 609 1701 220 2319 18 1730 321 367 754 37 616 1327 684 508 63 1136 350 1796 243 294 154...

output:

124947778

result:

ok 1 number(s): "124947778"

Test #26:

score: 0
Accepted
time: 0ms
memory: 25752kb

input:

7
4 1 8 4 5 8 10
1 2
2 3
3 4
2 5
4 6
3 7

output:

40

result:

ok 1 number(s): "40"

Extra Test:

score: -3
Extra Test Failed : Time Limit Exceeded on 1

input:

200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:


result: