QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359478 | #8213. Graffiti | installb# | WA | 2ms | 3676kb | C++20 | 4.2kb | 2024-03-20 18:18:34 | 2024-03-20 18:18:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define M 300005
int n,m,ty;
char S[5];
vector<int>edge[M];
using ll = long long;
ll f[M][3][3];
using pll = pair<ll,ll>;
vector<pll>A;
ll solve1(){//C(#a,2) + sum
vector<ll>p;
ll ret = 0, s = 0;
for(auto [x,y]: A)
s += y, p.push_back(x-y);
sort(p.begin(),p.end());
reverse(p.begin(),p.end());
ret = s;
for(int i=0;i<p.size();i++){
s += p[i];
ret = max(ret,(1ll*i*(i+1)>>1)+s);
}
return ret;
}
using tll = tuple<ll,ll,ll>;
vector<tll>B;
vector<tll>add[M];
ll solve2(){
int cnt = 0,sz = B.size();
for(int i=0;i<=sz;i++)add[i].clear();
ll ret = 0,s = 0,s_set = 0;
multiset<ll>S1,S2,D;
for(auto [x,y,z]:B){
s += x;
ret += max(y,z);
s_set += max(y,z) - x;
if(y > z && y - z < sz) add[y-z].push_back(tll(x,y,z));
if(z >= y) S2.insert(z-x);
else S1.insert(y-x);
}
for(int i=1;i<=sz;i++){
s_set += S2.size();
while(!D.empty() && !S1.empty() && *D.rbegin()+i == *S1.begin()){
S1.erase(S1.begin());
auto it = D.end();--it;
D.erase(it);
}
if(S1.empty()){
s_set -= (*S2.begin())+i;
S2.erase(S2.begin());
}else if(S2.empty()){
s_set -= (*S1.begin());
S1.erase(S1.begin());
}else if((*S1.begin()) <= (*S2.begin()) + i){
s_set -= (*S1.begin());
S1.erase(S1.begin());
}else{
s_set -= (*S2.begin())+i;
S2.erase(S2.begin());
}
for(auto [x,y,z]: add[i]){
if(S1.count(y-x))S1.erase(S1.lower_bound(y-x)),S2.insert(z-x);
else D.insert(z-x);
}
ret = max(ret, s + s_set);
}
return ret;
}
void dfs(int F,int now){
for(int &to:edge[now])if(to!=F){
dfs(now,to);
}
if(ty == 1){
for(int a=0;a<2;a++)
for(int b=0;b<2;b++){
if(a == 0){
A.clear();
for(int &to:edge[now])if(to!=F)
A.push_back(pll(f[to][0][a]+(a==0&&b==1),f[to][1][a]+(a==0&&b==0)));
f[now][a][b] = solve1();
}else {
for(int &to:edge[now])if(to!=F)
f[now][a][b] += max(f[to][0][a],f[to][1][a]);
}
}
}else{
for(int a=0;a<3;a++)
for(int b=0;b<3;b++){
if(a == 1){
B.clear();
for(int &to:edge[now])if(to!=F)
B.push_back(tll(f[to][0][a]+(b==2),f[to][1][a],f[to][2][a]+(b==0)));
f[now][a][b] = solve2();
}else {
for(int &to:edge[now])if(to!=F)
f[now][a][b] += max(f[to][0][a],f[to][1][a]);
}
}
}
}
ll redfs(int F,int now,int flg){
ll ret = 0,sz=edge[now].size();
// if(flg)cout<<now<<' '<<sz<<endl;
if(flg) ret += sz*(sz-1);
for(int &to:edge[now])if(to!=F)
ret += redfs(now,to,!flg);
return ret;
}
int main(){
cin>>n;
scanf("%s",S+1);
m=strlen(S+1);
if(m==1)cout<<n<<endl;
else if(m==2)cout<<n-1<<endl;
if(m<=2)return 0;
for(int a,b,i=1;i<n;i++){
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
if(S[1] == S[3]){
cout<<max(redfs(0,1,0),redfs(0,1,1))<<endl;
return 0;
}
if(S[1]!=S[2] && S[2]!=S[3] && S[1]!=S[3])S[1]='a',S[2]='b',S[3]='c',ty=1;
else S[1]='a',S[2]='a',S[3]='b',ty=2;
dfs(0,1);
if(ty == 1){
cout<<max(f[1][0][1],max(f[1][1][1],f[1][2][1]))<<endl;
}else{
ll ans = 0;
int a = 0;
{
A.clear();
for(int &to:edge[1])
A.push_back(pll(f[to][0][a],f[to][1][a]));
ans = solve1();
}
a = 1;
{
ll s = 0;
for(int &to:edge[1])
s += max(f[to][0][a],f[to][1][a]);
ans = max(ans,s);
}
cout<<ans<<endl;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3676kb
input:
3 orz 1 2 2 3
output:
2
result:
wrong answer 1st numbers differ - expected: '1', found: '2'