QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#326514 | #8213. Graffiti | HaccerKat | WA | 1ms | 3844kb | C++20 | 2.3kb | 2024-02-13 12:17:32 | 2024-02-13 12:17:33 |
Judging History
answer
#include "bits/stdc++.h"
#ifdef DEBUG
#include "algo/debug.hpp"
#else
#define dbg(...)
#define dbgm(...)
#endif
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// using u128 = __uint128_t;
// using i128 = __int128;
const int mod = 1000000007;
const int N = 300005;
const int LOG = 20;
const int inf = 1e9;
const double eps = 1e-11;
int n, m, k, qq;
string s;
vector<int> adj[N];
int type;
ll dp[N][2][2];
int deg[N];
void dfs(int u, int p = -1) {
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
for (int x = 0; x < 2; x++) {
vector<ll> a;
ll cur = 0;
for (int v : adj[u]) {
if (v == p) continue;
a.push_back(dp[v][1][x] - dp[v][0][x]);
cur += dp[v][0][x];
}
sort(a.rbegin(), a.rend());
int sz = a.size();
for (int y = 0; y < (u ? 2 : 1); y++) {
for (int i = 0;; i++) {
ll z = y + i, d = deg[u], val;
if (type == 0) val = (d - z) * (d - z - 1);
if (type == 1) val = z * (d - z);
if (type == 2) val = z * (z - 1);
if (type == 3) val = (z / 2) * ((z + 1) / 2);
if (x) val = 0;
dp[u][x][y] = max(dp[u][x][y], cur + val);
if (i == sz) break;
cur += a[i];
}
}
}
}
void solve() {
cin >> n >> s;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
deg[u]++, deg[v]++;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (s.size() == 1) {
cout << n << "\n";
}
else if (s.size() == 2) {
int x = (n - 1) * (s[0] == s[1] ? 2 : 1);
cout << x << "\n";
}
else {
if (s[0] == s[1] && s[1] == s[2]) type = 0;
else if ((s[0] == s[1] && s[1] != s[2]) || (s[0] != s[1] && s[1] == s[2])) type = 1;
else if (s[0] == s[2] && s[0] != s[1]) type = 2;
else type = 3;
dfs(0);
cout << max(dp[0][0][0], dp[0][1][0]) << "\n";
}
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3616kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
5 bob 3 2 5 1 1 4 2 4
output:
4
result:
ok 1 number(s): "4"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3844kb
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:
37
result:
ok 1 number(s): "37"
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3828kb
input:
50 abc 14 26 46 47 10 13 30 19 33 46 32 50 39 6 35 13 8 5 28 3 2 21 17 22 22 6 5 20 19 3 38 3 16 2 18 34 13 6 47 6 9 28 1 2 37 47 50 10 12 34 40 19 42 19 26 46 43 3 44 47 31 47 49 18 45 34 27 13 7 34 6 34 3 45 11 44 21 13 29 24 15 40 48 39 24 6 41 47 23 27 36 21 25 21 4 20 20 44
output:
54
result:
wrong answer 1st numbers differ - expected: '37', found: '54'