QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#320868 | #8213. Graffiti | ucup-team1631# | WA | 0ms | 3860kb | C++23 | 2.9kb | 2024-02-03 22:56:41 | 2024-02-03 22:56:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
return a > b ? (a = b, 1) : 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int n;
cin >> n;
string w;
cin >> w;
vector<vector<int>> G(n);
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
if (w.size() == 1) {
cout << n << endl;
return 0;
}
if (w.size() == 2) {
int ans = n - 1;
if (w[0] == w[1]) {
ans *= 2;
}
cout << ans << endl;
return 0;
}
if (w[0] == w[2]) {
vector<int> col(n, -1);
auto dfs = [&](auto& dfs, int v) -> void {
for (auto c : G[v]) {
if (col[c] == -1) {
col[c] = col[v] ^ 1;
dfs(dfs, c);
}
}
};
col[0] = 0;
dfs(dfs, 0);
vector<ll> cnt(2);
rep(i, 0, n) {
ll d = G[i].size();
cnt[col[i]] += d * (d - 1);
}
if (w[0] == w[1]) {
cout << cnt[0] + cnt[1] << endl;
} else {
cout << max(cnt[0], cnt[1]) << endl;
}
return 0;
}
auto wr = w;
reverse(all(wr));
set<char> chars;
for (auto c : w) chars.insert(c);
vector<map<string, ll>> cnt(n), val(n);
auto dfs = [&](auto& dfs, int v, int p) -> void {
bool is_leaf = true;
for (int c : G[v]) {
if (c != p) {
is_leaf = false;
dfs(dfs, c, v);
}
}
if (is_leaf) {
for (auto c0 : chars) {
string c01 = string(1, c0) + "?";
cnt[v][c01] = 1;
}
return;
}
for (auto c0 : chars) {
for (int c : G[v]) {
if (c == p) continue;
for (auto [c12, x] : cnt[c]) {
string c012 = string(1, c0) + c12;
string c01 = string(1, c0) + c12[0];
cnt[v][c01] += x;
val[v][c01] += val[c][c12];
if (c012 == w || c012 == wr) {
val[v][c01] += x;
}
}
}
}
};
dfs(dfs, 0, -1);
ll ans = 0;
for (auto [_, x] : val[0]) chmax(ans, x);
cout << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3856kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
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: 0ms
memory: 3676kb
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:
11695
result:
wrong answer 1st numbers differ - expected: '37', found: '11695'