QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#449127 | #8518. Roars III | Graphcity | WA | 1ms | 4092kb | C++20 | 3.0kb | 2024-06-20 18:10:46 | 2024-06-20 18:10:47 |
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=2e5;
int n,h[Maxn+5],anc[Maxn+5]; ll sf[Maxn+5],sg[Maxn+5];
struct Node{int mx,se;} f[Maxn+5],g[Maxn+5];
inline Node operator+(Node a,Node b)
{
if(a.mx>=b.mx) return Node{a.mx,max(a.se,b.mx)};
else return Node{b.mx,max(b.se,a.mx)};
}
vector<int> v[Maxn+5];
inline void dfs1(int x,int fa)
{
anc[x]=fa;
for(auto y:v[x]) if(y!=fa) dfs1(y,x),sf[x]=sf[x]+sf[y];
if(h[x])
{
for(auto y:v[x]) if(y!=fa) f[x]=f[x]+f[y];
f[x].mx++; if(f[x].se) f[x].se++;
}
else
{
vector<int> w; for(auto y:v[x]) if(y!=fa)
{
if(f[y].mx) w.push_back(f[y].mx);
if(f[y].se) w.push_back(f[y].se);
} sort(w.begin(),w.end());
if(!w.empty()) sf[x]+=w.back(),w.back()--;
sort(w.begin(),w.end());
if(!w.empty()) f[x].mx=w.back()+1,w.pop_back();
if(!w.empty()) f[x].se=w.back()+1;
}
}
inline void dfs2(int x,int fa)
{
static Node pre[Maxn+5],suf[Maxn+5]; Node cur=g[x]; ll sum=sg[x];
for(auto y:v[x]) if(y!=fa) pre[y]=cur,cur=cur+f[y],sum+=sf[y];
reverse(v[x].begin(),v[x].end()),cur=g[x];
for(auto y:v[x]) if(y!=fa) sg[y]+=(sum-sf[y]),suf[y]=cur,cur=cur+f[y];
reverse(v[x].begin(),v[x].end());
if(h[x])
{
for(auto y:v[x]) if(y!=fa)
{g[y]=pre[y]+suf[y],g[y].mx++; if(g[y].se) g[y].se++;}
}
else
{
multiset<int> st;
if(g[x].mx) st.insert(g[x].mx); if(g[x].se) st.insert(g[x].se);
for(auto y:v[x]) if(y!=fa) {if(f[y].mx) st.insert(f[y].mx); if(f[y].se) st.insert(f[y].se);}
for(auto y:v[x]) if(y!=fa)
{
if(f[y].mx) st.erase(st.find(f[y].mx)); if(f[y].se) st.erase(st.find(f[y].se));
int res=-1; if(!st.empty()) res=*prev(st.end()),st.erase(prev(st.end())),st.insert(res-1),sg[y]+=res;
int s1=-1; if(!st.empty()) s1=(*prev(st.end()))+1,st.erase(prev(st.end())),g[y].mx=s1;
if(!st.empty()) g[y].se=*prev(st.end())+1;
if(s1>=0) st.insert(s1-1); if(res>=0) st.erase(st.find(res-1)),st.insert(res);
if(f[y].mx) st.insert(f[y].mx); if(f[y].se) st.insert(f[y].se);
}
}
for(auto y:v[x]) if(y!=fa) dfs2(y,x);
}
int main()
{
// freopen("1.in","r",stdin);
cin>>n; For(i,1,n) scanf("%1d",&h[i]);
For(i,1,n-1)
{
int a,b; cin>>a>>b;
v[a].push_back(b),v[b].push_back(a);
} dfs1(1,0),dfs2(1,0);
For(i,1,n)
{
if(i==1) {printf("%lld ",sf[1]); continue;}
if(h[i]) {printf("%lld ",sf[i]+sg[i]); continue;}
ll ans=sg[i]; int mx=g[i].mx;
for(auto j:v[i]) if(j!=anc[i]) ans+=sf[j],mx=max(mx,f[j].mx);
if(mx) ans+=mx;
printf("%lld ",ans);
}
return 0;
}
/*
5
10101
1 2
2 3
2 4
4 5
QOJ8518
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3796kb
input:
5 10101 1 2 2 3 2 4 4 5
output:
2 2 2 3 3
result:
ok 5 number(s): "2 2 2 3 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3796kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3876kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3796kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 1ms
memory: 4092kb
input:
3 100 2 3 2 1
output:
0 1 2
result:
ok 3 number(s): "0 1 2"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3836kb
input:
4 1100 4 1 4 3 4 2
output:
1 1 3 1
result:
ok 4 number(s): "1 1 3 1"
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 4092kb
input:
5 10100 4 5 1 3 2 1 3 5
output:
0 2 0 5 2
result:
wrong answer 4th numbers differ - expected: '4', found: '5'