QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#350402#8213. Graffitiucup-team1198#WA 2ms3664kbC++205.0kb2024-03-10 18:11:372024-03-10 18:11:38

Judging History

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

  • [2024-03-10 18:11:38]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3664kb
  • [2024-03-10 18:11:37]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int MAXN = 300300;

vector<int> G[MAXN];

long long dp0[MAXN];
vector<long long> dp1[MAXN];

// abb
void dfs1(int v, int p) {
    vector<pair<long long, long long>> children;
    for (int u : G[v]) {
        if (u == p)
            continue;
        dfs1(u, v);
        long long mx1 = 0;
        long long mx2 = 0;
        for (int k = 0; k < dp1[u].size(); ++k) {
            mx1 = max(mx1, dp1[u][k] + k);
            mx2 = max(mx2, dp1[u][k] + int(dp1[u].size()) - 1 - k);
        }
        children.emplace_back(mx2, dp0[u]);
        dp0[v] += max(mx1, dp0[u]);
    }
    sort(children.begin(), children.end(), [](const pair<long long, long long>& a, const pair<long long, long long>& b) {
        return a.first - a.second > b.first - b.second;
    });
    dp1[v].resize(children.size() + 1);
    for (int i = 0; i < children.size(); ++i)
        dp1[v][0] += children[i].second;
    for (int i = 0; i < children.size(); ++i) {
        dp1[v][i + 1] = dp1[v][i] + children[i].first - children[i].second;
    }
    for (int i = 0; i <= children.size(); ++i)
        dp1[v][i] += i * 1ll * (int(children.size()) - i);
}

// aba
void dfs2(int v, int p) {
    vector<pair<long long, long long>> children;
    for (int u : G[v]) {
        if (u == p)
            continue;
        dfs2(u, v);
        long long mx1 = 0;
        long long mx2 = 0;
        for (int k = 0; k < dp1[u].size(); ++k) {
            mx1 = max(mx1, dp1[u][k] + 2 * k);
            mx2 = max(mx2, dp1[u][k]);
        }
        children.emplace_back(dp0[u], mx2);
        dp0[v] += max(mx1, dp0[u]);
    }
    sort(children.begin(), children.end(), [](const pair<long long, long long>& a, const pair<long long, long long>& b) {
        return a.first - a.second > b.first - b.second;
    });
    dp1[v].resize(children.size() + 1);
    for (int i = 0; i < children.size(); ++i)
        dp1[v][0] += children[i].second;
    for (int i = 0; i < children.size(); ++i) {
        dp1[v][i + 1] = dp1[v][i] + children[i].first - children[i].second;
    }
    for (int i = 0; i <= children.size(); ++i) {
        dp1[v][i] += i * 1ll * (i - 1);
    }
}

// abc
void dfs3(int v, int p) {
    vector<pair<long long, long long>> children;
    for (int u : G[v]) {
        if (u == p)
            continue;
        dfs3(u, v);
        long long mx1 = 0;
        long long mx2 = 0;
        for (int k = 0; k < dp1[u].size(); ++k) {
            mx1 = max(mx1, dp1[u][k] + k);
            mx2 = max(mx2, dp1[u][k]);
        }
        children.emplace_back(mx2, dp0[u]);
        dp0[v] += max(mx1, dp0[u]);
    }
    sort(children.begin(), children.end(), [](const pair<long long, long long>& a, const pair<long long, long long>& b) {
        return a.first - a.second > b.first - b.second;
    });
    dp1[v].resize(children.size() + 1);
    long long cur = 0;
    for (int i = 0; i < children.size(); ++i)
        cur += children[i].second;
    dp1[v][0] = cur;
    for (int i = 0; i < children.size(); ++i) {
        cur += children[i].first - children[i].second;
        int cnt = i + 1;
        dp1[v][(cnt + 1) / 2] = max(dp1[v][(cnt + 1) / 2], cur + (cnt + 1) / 2 * 1ll * (cnt - (cnt + 1) / 2));
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        G[a - 1].emplace_back(b - 1);
        G[b - 1].emplace_back(a - 1);
    }
    if (s.size() == 1) {
        cout << n << '\n';
        return 0;
    }
    if (s.size() == 2) {
        if (s[0] == s[1]) {
            cout << 2 * (n - 1) << '\n';
        } else {
            cout << n - 1 << '\n';
        }
        return 0;
    }
    if (s[0] == s[1] && s[1] == s[2]) {
        // aaa
        long long ans = 0;
        for (int i = 0; i < n; ++i)
            ans += (G[i].size()) * (int(G[i].size()) - 1);
        cout << ans << '\n';
        return 0;
    }
    if (s[0] == s[1] || s[1] == s[2]) {
        // abb
        dfs1(0, -1);
        long long ans = dp0[0];
        for (long long x : dp1[0])
            ans = max(ans, x);
        cout << ans << '\n';
        return 0;
    }
    if (s[0] == s[2]) {
        // aba
        dfs2(0, -1);
        long long ans = dp0[0];
        for (long long x : dp1[0])
            ans = max(ans, x);
        cout << ans << '\n';
        return 0;
    }
    // abc
        dfs3(0, -1);
        long long ans = dp0[0];
        for (long long x : dp1[0])
            ans = max(ans, x);
        cout << ans << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3556kb

input:

1
a

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

3
orz
1 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2
ab
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

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

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:

35

result:

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