QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#323514 | #8213. Graffiti | arnold518 | WA | 2ms | 12412kb | C++17 | 3.4kb | 2024-02-09 23:30:03 | 2024-02-09 23:30:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
int N, M;
char S[4];
vector<int> adj[MAXN+10];
ll ans;
ll dp[MAXN+10][2][2];
ll f(int x) { return (ll)(x/2)*(x-x/2); }
void dfs(int now, int bef, int flag)
{
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, flag);
}
for(auto p : {0, 1})
{
vector<ll> V;
V.push_back(0);
for(int nxt : adj[now])
{
if(nxt==bef) continue;
V[0]+=dp[nxt][0][p];
V.push_back(dp[nxt][1][p]-dp[nxt][0][p]);
}
sort(V.begin()+1, V.end());
for(int i=1; i<V.size(); i++) V[i]+=V[i-1];
int sz=V.size()-1;
for(auto q : {0, 1})
{
for(int i=0; i<V.size(); i++)
{
ll val=0;
if(flag==1 && p==0) val=(ll)(i+(q==1))*(sz-i+(q==0));
if(flag==2 && p==1) val=(ll)(sz-i+(q==0))*(sz-i+(q==0)-1);
if(flag==3 && p==1) val=f(sz-i+(q==0));
dp[now][p][q]=max(dp[now][p][q], val+V[i]);
}
}
}
//printf("%d : %lld %lld %lld %lld\n", now, dp[now][0][0], dp[now][0][1], dp[now][1][0], dp[now][1][1]);
}
int main()
{
scanf("%d %s", &N, S);
for(int i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
M=strlen(S);
if(M==1) return !printf("%d\n", N);
if(M==2)
{
if(S[0]==S[1]) return !printf("%d\n", 2*(N-1));
else return !printf("%d\n", N-1);
}
if(S[0]==S[1] && S[0]==S[2])
{
for(int i=1; i<=N; i++)
{
ans+=(ll)adj[i].size()*(adj[i].size()-1);
}
return !printf("%lld\n", ans);
}
if(S[0]==S[1] || S[1]==S[2])
{
dfs(1, 1, 1);
vector<ll> V;
V.push_back(0);
for(int nxt : adj[1])
{
V[0]+=dp[nxt][0][0];
V.push_back(dp[nxt][1][0]-dp[nxt][0][0]);
}
sort(V.begin()+1, V.end());
for(int i=1; i<V.size(); i++) V[i]+=V[i-1];
for(int i=0; i<V.size(); i++)
{
ll val=0;
val=(ll)i*(V.size()-1-i);
ans=max(ans, val+V[i]);
}
return !printf("%lld\n", ans);
}
if(S[0]==S[2])
{
dfs(1, 1, 2);
vector<ll> V;
V.push_back(0);
for(int nxt : adj[1])
{
V[0]+=dp[nxt][0][1];
V.push_back(dp[nxt][1][1]-dp[nxt][0][1]);
}
sort(V.begin()+1, V.end());
for(int i=1; i<V.size(); i++) V[i]+=V[i-1];
for(int i=0; i<V.size(); i++)
{
ll val=0;
val=(ll)(V.size()-1-i)*(V.size()-1-i-1);
ans=max(ans, val+V[i]);
}
return !printf("%lld\n", ans);
}
dfs(1, 1, 3);
vector<ll> V;
V.push_back(0);
for(int nxt : adj[1])
{
V[0]+=dp[nxt][0][1];
V.push_back(dp[nxt][1][1]-dp[nxt][0][1]);
}
sort(V.begin()+1, V.end());
for(int i=1; i<V.size(); i++) V[i]+=V[i-1];
for(int i=0; i<V.size(); i++)
{
ll val=0;
val=f(V.size()-1-i);
ans=max(ans, val+V[i]);
}
return !printf("%lld\n", ans);
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 12412kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 11732kb
input:
3 orz 1 2 2 3
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'