QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#360346 | #8213. Graffiti | installb | WA | 1ms | 3952kb | C++23 | 4.8kb | 2024-03-21 17:41:23 | 2024-03-21 17:41:24 |
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+1)*((int)p.size()-i-1))+s);
}
return ret;
}
ll solve3(){//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+1)*i>>1)+s);
}
return ret;
}
using tll = tuple<ll,ll,ll>;
vector<tll>B;
const ll inf = 1e16;
ll solve2(){
if(B.empty()) return 0ll;
vector<ll>p;
ll ret = 0, sumz = 0,v = -inf;
for(auto [x,y,z]: B){
sumz += y;
if(v == -inf) v = x - z;
else assert(v == x-z);
p.push_back(z - y);
}
sort(p.begin(),p.end());
reverse(p.begin(),p.end());
static ll pre[M];
pre[0]=0;
for(int i=0;i<p.size();i++)pre[i+1] = pre[i] + p[i];
ret = sumz;
// cout<<pre[n]<<' '<<v<<endl;
for(int i=0;i<p.size();i++){
int n = (int)p.size() - i;
int p = max(0,(n-1)>>1);
ret = max(ret,1ll*p*(n-p) + sumz + max(pre[n] + v*(n-p),pre[n] + v*p));
// cout<<p<<endl;
p++;
if(p<=n)ret = max(ret,1ll*p*(n-p) + sumz + max(pre[n] + v*(n-p),pre[n] + v*p));
}
// for(auto [x,y,z]: B)cout<<x<<','<<y<<','<<z<<endl;
// cout<<ret<<endl;
return ret;
}
void dfs(int F,int now){
for(int &to:edge[now])if(to!=F)dfs(now,to);
if(ty == 3){
for(int a=0;a<2;a++)
for(int b=0;b<2;b++){
if(a == 1){
A.clear();
for(int &to:edge[now])if(to!=F)
A.push_back(pll(f[to][0][a]+(b==0),f[to][1][a]));
f[now][a][b] = solve3();
}else {
for(int &to:edge[now])if(to!=F)
f[now][a][b] += max(f[to][0][a],f[to][1][a]);
}
}
}else if(ty == 2){
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]+(b==1),f[to][1][a]+(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][2][a],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)*2<<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[2] && S[1] == S[3]){
ll ans = 0;
for(int i=1;i<=n;i++)
ans += 1ll*edge[i].size()*((int)edge[i].size()-1)>>1;
return cout<<ans*2<<endl,0;
}else if(S[1] == S[3] && S[1] != S[2]) S[1]='a',S[2]='b',S[3]='a',ty=3;
else 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 if(ty == 2){
ll ans = 0;
A.clear();
for(int &to:edge[1])
A.push_back(pll(f[to][0][0],f[to][1][0]));
ans = solve1();
ll s = 0;
for(int &to:edge[1])
s += max(f[to][0][1],f[to][1][1]);
ans = max(ans,s);
cout<<ans<<endl;
}else{
cout<<max(f[1][0][1],f[1][1][1])*2<<endl;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3900kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3952kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3740kb
input:
2 ab 1 2
output:
2
result:
wrong answer 1st numbers differ - expected: '1', found: '2'