QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#333322 | #8213. Graffiti | yllcm | WA | 2ms | 12436kb | C++14 | 3.7kb | 2024-02-19 20:08:41 | 2024-02-19 20:08:41 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
#define int long long
using namespace std;
inline int read() {
int x = 0; bool op = false;
char c = getchar();
while(!isdigit(c))op |= (c == '-'), c = getchar();
while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return op ? -x : x;
}
const int N = 3e5 + 10;
int n;
vector<int> G[N];
char s[5];
namespace A {
void MAIN() {
printf("%lld\n", n);
return ;
}
};
namespace AA {
void MAIN() {
printf("%lld\n", 2 * (n - 1));
return ;
}
};
namespace AB {
void MAIN() {
printf("%lld\n", n - 1);
return ;
}
};
namespace AAA {
void MAIN() {
int ans = 0;
for(int i = 1; i <= n; i++)ans += G[i].size() * (G[i].size() - 1);
printf("%lld\n", ans);
}
};
namespace AAB {
int ans = 0;
int f[N][2], g[N];
void dfs(int u, int fa) {
int sum0 = 0;
vector<int> A;
for(int v : G[u]) {
if(v == fa)continue;
dfs(v, u);
g[u] += max(g[v], f[v][1]);
sum0 += g[v]; A.pb(f[v][0] - g[v]);
}
if(u == 1)ans = max(ans, g[u]);
sort(A.begin(), A.end(), greater<int>());
for(int j = 1; j < A.size(); j++)A[j] += A[j - 1];
for(int o = 0; o < 2; o++)
for(int j = 0; j <= A.size(); j++) {
f[u][o] = max(f[u][o], sum0 + (j ? A[j - 1] : 0) + (j + !o) * ((int)A.size() - j + o));
if(u == 1)ans = max(ans, sum0 + (j ? A[j - 1] : 0) + j * ((int)A.size() - j));
}
return ;
}
void MAIN() {
dfs(1, 0);
printf("%lld\n", ans);
return ;
}
};
namespace ABA {
int ans = 0;
int f[N][2], g[N];
void dfs(int u, int fa) {
int sum0 = 0;
vector<int> A;
for(int v : G[u]) {
if(v == fa)continue;
dfs(v, u);
g[u] += max(g[v], f[v][0]);
sum0 += f[v][1]; A.pb(g[v] - f[v][1]);
}
if(u == 1)ans = max(ans, g[u]);
sort(A.begin(), A.end(), greater<int>());
for(int j = 1; j < A.size(); j++)A[j] += A[j - 1];
for(int o = 0; o < 2; o++)
for(int j = 0; j <= A.size(); j++) {
f[u][o] = max(f[u][o], sum0 + (j ? A[j - 1] : 0) + (j + !o) * (j + !o - 1));
if(u == 1)ans = max(ans, sum0 + (j ? A[j - 1] : 0) + j * (j - 1));
}
return ;
}
void MAIN() {
dfs(1, 0);
printf("%lld\n", ans);
return ;
}
};
namespace ABC {
int ans = 0;
int f[N][2], g[N];
void dfs(int u, int fa) {
int sum0 = 0;
vector<int> A;
for(int v : G[u]) {
if(v == fa)continue;
dfs(v, u);
g[u] += max(g[v], f[v][0]);
sum0 += f[u][1]; A.pb(g[v] - f[u][1]);
}
if(u == 1)ans = max(ans, g[u]);
sort(A.begin(), A.end(), greater<int>());
for(int j = 1; j < A.size(); j++)A[j] += A[j - 1];
for(int o = 0; o < 2; o++)
for(int j = 0; j <= A.size(); j++) {
f[u][o] = max(f[u][o], sum0 + (j ? A[j - 1] : 0) + ((j + !o) / 2) * (j + !o - (j + !o) / 2));
if(u == 1)ans = max(ans, sum0 + (j ? A[j - 1] : 0) + (j / 2) * (j - (j / 2)));
}
return ;
}
void MAIN() {
dfs(1, 0);
printf("%lld\n", ans);
return ;
}
};
signed main() {
n = read();
scanf("%s", s);
for(int i = 1; i < n; i++) {
int u = read(), v = read();
G[u].pb(v); G[v].pb(u);
}
if(strlen(s) == 1)return A::MAIN(), 0;
if(strlen(s) == 2) {
if(s[0] == s[1])return AA::MAIN(), 0;
else return AB::MAIN(), 0;
}
if(strlen(s) == 3) {
if(s[0] == s[1] && s[1] == s[2])return AAA::MAIN(), 0;
if(s[0] == s[1] || s[1] == s[2])return AAB::MAIN(), 0;
if(s[0] == s[2])return ABA::MAIN(), 0;
return ABC::MAIN(), 0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11280kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 11060kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 11100kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 12384kb
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: 12436kb
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:
36
result:
wrong answer 1st numbers differ - expected: '37', found: '36'