QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354541 | #8213. Graffiti | Graphcity | WA | 1ms | 3744kb | C++20 | 1.7kb | 2024-03-15 16:09:01 | 2024-03-15 16:09:02 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=3e5;
int n,m,deg[Maxn+5],tp; char ch[4];
ll f[Maxn+5][2][2];
vector<int> v[Maxn+5];
inline void dfs(int x,int fa)
{
for(auto y:v[x]) if(y!=fa) dfs(y,x);
For(o,0,1)
{
ll res=0; vector<ll> w; w.push_back(0);
for(auto y:v[x]) if(y!=fa)
w.push_back(f[y][1][o]-f[y][0][o]),res+=f[y][0][o];
sort(w.begin(),w.end()),reverse(w.begin(),w.end());
For(p,0,1-(x==1))
{
ll now=res; for(int i=0;i<w.size();++i)
{
int cnt=i+p; ll all=0;
if(tp==1) all=1ll*cnt*(deg[i]-cnt);
if(tp==2) all=1ll*cnt*(cnt-1);
if(tp==3) all=1ll*(cnt/2)*((cnt+1)/2);
// cerr<<x<<' '<<cnt<<' '<<all<<endl;
f[x][o][p]=max(f[x][o][p],now+all*(o==0));
now+=w[i];
}
}
}
}
int main()
{
// freopen("1.in","r",stdin);
scanf("%d%s",&n,ch+1),m=strlen(ch+1);
For(i,1,n-1)
{
int a,b; cin>>a>>b; deg[a]++,deg[b]++;
v[a].push_back(b),v[b].push_back(a);
}
if(m==1) cout<<n<<endl;
if(m==2 && ch[1]==ch[2]) cout<<2*(n-1)<<endl;
if(m==2 && ch[1]!=ch[2]) cout<<n-1<<endl;
if(m<3) return 0;
if(ch[1]==ch[2] && ch[2]==ch[3])
{
ll ans=0;
For(i,1,n) ans=ans+1ll*deg[i]*(deg[i]-1);
cout<<ans<<endl; return 0;
}
if(ch[1]==ch[2] || ch[2]==ch[3]) tp=1;
else if(ch[1]==ch[3]) tp=2; else tp=3;
dfs(1,0); cout<<max(f[1][0][0],f[1][1][0])<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3680kb
input:
1 a
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
3 orz 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
2 ab 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3688kb
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: 1ms
memory: 3744kb
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:
42
result:
wrong answer 1st numbers differ - expected: '37', found: '42'