QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#690797 | #8213. Graffiti | DinoHadzic | WA | 3ms | 12572kb | C++14 | 1.8kb | 2024-10-31 03:58:23 | 2024-10-31 03:58:23 |
Judging History
answer
#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const int N = 3e5 + 7;
int n;
string s;
vector <int> adj[N];
ll dp[N][3][4];
void smax(ll &x, ll y) {x = max(x, y);}
void dfs(int x, int p = -1) {
for (auto y : adj[x]) if (y != p) dfs(y, x);
vector <ll> v;
ll sum = 0;
for (auto y : adj[x]) {
if (y == p) continue;
for (int o = 0; o < 2; o++) for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) smax(dp[x][2*o][i], dp[y][j][2*o]);
v.pb(dp[y][0][1]-dp[y][1][1]);
sum += dp[y][1][1];
}
sort(all(v)); reverse(all(v));
ll mx[3] = {sum, sum, sum};
if (s[1] != s[2]) {
for (int i = 0; i < v.size(); i++) {
sum += v[i];
for (int j = 0; j < 2; j++) smax(mx[j], (s[0] == s[2]) ? sum+(ll)(i+1)*(i+2*j) : sum+(ll)((i+1+2*j)/2)*((i+2)/2));
}
for (int i = 0; i < 4; i++) dp[x][1][i] = mx[i%2==0];
}
else {
for (int i = 0; i < v.size(); i++) {
sum += v[i];
for (int j = 0; j < 3; j++) smax(mx[j], sum+(ll)(i+1+(!j))*(v.size()-i-1+(j==1)));
}
dp[x][1][0] = mx[0];
dp[x][1][1] = dp[x][1][2] = mx[1];
dp[x][1][3] = mx[2];
}
}
int main () {
FIO;
cin >> n >> s;
for (int i = 0; i < n-1; i++) {
int x, y; cin >> x >> y; x--; y--;
adj[x].pb(y);
adj[y].pb(x);
}
if (s.size() == 1) {
cout << n;
return 0;
}
if (s.size() == 2) {
cout << (n-1)*(1+(s[0]==s[1]));
return 0;
}
if (s[0] == s[1] && s[1] == s[2]) {
ll res = 0;
for (int i = 0; i < n; i++) res += (ll)adj[i].size()*((ll)adj[i].size()-1);
cout << res;
return 0;
}
if (s[0] == s[1]) swap(s[0], s[2]);
dfs(0);
cout << max(dp[0][0][3], max(dp[0][1][3], dp[0][2][3]));
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 10824kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 3ms
memory: 11568kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 12572kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 10724kb
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: 3ms
memory: 11888kb
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:
32
result:
wrong answer 1st numbers differ - expected: '37', found: '32'