QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#797082 | #8213. Graffiti | zxcen | WA | 0ms | 12804kb | C++14 | 1.6kb | 2024-12-02 15:59:28 | 2024-12-02 15:59:28 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5;
int n;
string str;
vector<int>g[N+10];
ll f[N+10][2][2];
int fl;
vector<ll>tmp;
inline void dfs(int u,int fat){
for(auto v:g[u]){
if(v!=fat){
dfs(v,u);
}
}
for(int i=0;i<=1;++i){
ll sum=0;
tmp.clear();
for(auto v:g[u]){
if(v!=fat){
sum+=f[v][0][i];
tmp.push_back(f[v][1][i]-f[v][0][i]);
}
}
tmp.push_back(0);
sort(tmp.begin(),tmp.end());
reverse(tmp.begin(),tmp.end());
for(int j=0;j<=1-(u==1);++j){
for(int k=0;k<(int)tmp.size();++k){
ll val;
if(fl==1){
val=1ll*(k+j)*(k+j-1);
}
else if(fl==2){
val=1ll*(k+j)*((int)g[u].size()-k-j);
}
else{
val=1ll*((k+j)/2)*((k+j+1)/2);
}
f[u][i][j]=max(f[u][i][j],sum+val*(i==0));
sum+=tmp[k];
}
}
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
cin>>str;
for(int i=1;i<n;++i){
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
if((int)str.size()==1){
cout<<n;
}
else if((int)str.size()==2){
if(str[0]==str[1]){
cout<<2*(n-1);
}
else{
cout<<n-1;
}
}
else if((int)str.size()==3){
if(str[0]==str[1]&&str[1]==str[2]){
ll ans=0;
for(int i=1;i<=n;++i){
ans+=(ll)g[i].size()*((ll)g[i].size()-1)/2;
}
cout<<2*ans;
}
else{
if(str[0]==str[2]){
fl=1;
}
else if(str[0]==str[1]||str[1]==str[2]){
fl=2;
}
else{
fl=3;
}
dfs(1,0);
cout<<max(f[1][0][0],f[1][1][0]);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12804kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 11712kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 11804kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 10772kb
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: 11960kb
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: 0ms
memory: 12208kb
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:
50
result:
wrong answer 1st numbers differ - expected: '37', found: '50'