QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#323225#8213. GraffitikarunaWA 1ms3836kbC++201.9kb2024-02-08 23:09:292024-02-08 23:09:30

Judging History

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

  • [2024-02-08 23:09:30]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3836kb
  • [2024-02-08 23:09:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 303030;

int n, q;
vector<int> g[MAXN];

ll dp[2][2][MAXN];

void dfs(int p, int v) {
	for (int x : g[v]) if (p != x) {
		dfs(v, x);
	}
	for (int s = 0; s < 2; s++) for (int t = 0; t < 2; t++) {
		vector<ll> V;
		ll tot = 0;
		for (int x : g[v]) if (p != x) {
			tot += dp[t][1][x];
			V.push_back(dp[t][0][x] - dp[t][1][x]);
		}
		sort(V.rbegin(), V.rend());

		for (int a = 0; a <= V.size(); a++) {
			int b = V.size() - a;
			ll val = tot;
			if (q == 1) {
				if (s == 0 && t == 1) val += 2 * a;
				if (t == 1) val += (ll)a * (a - 1);
			}
			if (q == 2) {
				if (s == 0 && t == 0) val += b;
				if (s == 1 && t == 0) val += a;
				if (t == 0) val += (ll)a * b;
			}
			if (q == 3) {
				if (s == 0 && t == 1) val += (ll)((a + 1) / 2) * ((a + 2) / 2);
				else val += (ll)(a / 2) * ((a + 1) / 2);
			}
			dp[s][t][v] = max(dp[s][t][v], val);
			if (a != V.size()) {
				tot += V[a];
			}
		}
	}
}
int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
	cin >> n;
	string S; cin >> S;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	if (S.size() == 1) return !(cout << n << '\n');
	if (S.size() == 2) return !(cout << n - 1 << '\n');

	if (S[0] == S[2]) {
		if (S[0] == S[1]) {
			ll ans = 0;
			for (int v = 1; v <= n; v++) {
				ans += (ll)g[v].size() * (g[v].size() - 1) / 2;
			}
			return !(cout << ans << '\n');
		}
		else q = 1;
	}
	else {
		if (S[0] == S[1] || S[1] == S[2])
			q = 2;
		else
			q = 3;
	}

	int p, v;
	for (int i = 1; i <= n; i++) {
		if (g[i].size() == 1) {
			p = i;
			v = g[p][0];
			break;
		}
	}
	dfs(p, v);
	ll ans = 0;
	for (int s = 0; s < 2; s++) for (int t = 0; t < 2; t++) {
		ans = max(ans, dp[s][t][v]);
	}
	cout << ans << '\n';
}

详细

Test #1:

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

input:

1
a

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

3
orz
1 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2
ab
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

5
bob
3 2
5 1
1 4
2 4

output:

4

result:

ok 1 number(s): "4"

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3624kb

input:

50
abc
23 14
24 25
1 3
47 46
2 26
22 41
34 19
7 14
50 24
29 38
17 25
4 26
35 37
21 14
11 4
13 27
8 25
5 10
20 27
44 27
15 39
19 9
30 12
38 27
39 27
41 40
14 48
32 7
16 37
3 13
42 5
48 27
49 25
6 5
26 9
31 17
36 7
43 29
9 5
45 9
18 9
40 42
27 5
25 42
46 10
37 42
12 48
28 26
33 5

output:

42

result:

wrong answer 1st numbers differ - expected: '37', found: '42'