QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#668097 | #8213. Graffiti | xlwang | WA | 3ms | 14244kb | C++14 | 2.9kb | 2024-10-23 11:19:40 | 2024-10-23 11:19:47 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
// int t=read();
// while(t--) work();
//}
const int Maxn=3e5+10;
ll f[Maxn][2][2];
vector<int> vc[Maxn];
int n;
char c[Maxn];
int ln;
inline void init(){
n=read();scanf("%s",c+1);ln=strlen(c+1);
fr(i,1,n-1){
int x,y;
x=read();y=read();
vc[x].pb(y);vc[y].pb(x);
}
}
int d[Maxn];
int flag;
inline bool cmp(ll a,ll b){return a>b;}
inline void chkmin(ll &x,ll y){if(x>y) x=y;}
inline void chkmax(ll &x,ll y){if(x<y) x=y;}
inline void dfs(int x,int fa){
for(auto y:vc[x]) if(y!=fa) dfs(y,x);
fr(nowtp,0,1){
vector<ll> now;vector<ll> ().swap(now);
ll s=0;
for(auto y:vc[x]) if(y!=fa) now.pb(f[y][1][nowtp]-f[y][0][nowtp]),s=f[y][0][nowtp];
sort(now.begin(),now.end());reverse(now.begin(),now.end());now.pb(0);
fr(fatp,0,1-(!fa)){
ll nows=s;
for(int j=0;j<now.size();++j){
int id=fatp+j,add=0;
if(flag==1) add=1ll*id*(id-1);
if(flag==2) add=1ll*id*(d[x]-id);
if(flag==3) add=1ll*(id/2)*((id+1)/2);
// cout<<nows<<' '<<nowtp<<endl;
chkmax(f[x][nowtp][fatp],nows+add*(nowtp==0));
nows+=now[j];
}
}
}
}
inline void work(){
fr(i,1,n) d[i]=vc[i].size();
if(ln==1){
writeln(n);
return;
}
if(ln==2){
if(c[1]==c[2]) writeln(2*(n-1));
else writeln(n-1);
return;
}
if(c[1]==c[2] && c[2]==c[3]){
ll ans=0;
fr(i,1,n) ans+=1ll*d[i]*(d[i]-1);
printf("%lld\n",ans);
return;
}
if(c[1]==c[3]) flag=1;
else if(c[1]==c[2]) flag=2;
else if(c[2]==c[3]) flag=2;
else flag=3;
dfs(1,0);
printf("%lld\n",max(f[1][0][0],f[1][1][0]));
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();work();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 11980kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 14048kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 11864kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 14184kb
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: 14244kb
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:
17
result:
wrong answer 1st numbers differ - expected: '37', found: '17'