QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#442561 | #8518. Roars III | ucup-team2279# | WA | 3ms | 20200kb | C++20 | 2.7kb | 2024-06-15 12:45:38 | 2024-06-15 12:45:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mk make_pair
#define lowbit(x) (x&(-x))
#define pb emplace_back
#define pr pair<int,int>
#define let const auto
#define all(A) A.begin(),A.end()
void chkmin(int &x,int y){x=min(x,y);}
void chkmax(int &x,int y){x=max(x,y);}
const int N=2e5+5;
int read(){
int x=0,f=1; char c=getchar();
while(('0'>c||c>'9')&&c!='-') c=getchar();
if(c=='-') f=0,c=getchar();
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return f?x:-x;
}
int n,siz[N],a[N],dep[N];
multiset <int> s[N];
vector <int> adj[N];
ll ans[N],f[N];
void merge(multiset <int> &s,multiset <int> &t){
if(s.size()<t.size()) swap(s,t);
for(auto v:t) s.insert(v);
}
int ban[N],rt,sum,Max[N];
void calcsiz(int u,int fa){
siz[u]=1;
for(int v:adj[u]) if(v!=fa&&!ban[v]){
calcsiz(v,u);
siz[u]+=siz[v];
chkmax(Max[u],siz[v]);
}
Max[u]=max(Max[u],sum-siz[u]);
if(Max[u]<Max[rt]) rt=u;
}
void dp(int u,int fa){
f[u]=0;
for(int v:adj[u]) if(v!=fa){
if(ban[v]){
auto it=s[v].end();
int len=s[v].size();
for(int j=0; j<min(len,sum); j++)
--it,s[u].insert(*it+dep[u]+1);
f[u]+=f[v];
}
else{
dep[v]=dep[u]+1;
dp(v,u);
merge(s[u],s[v]);
s[v].clear();
f[u]+=f[v];
}
while((int)s[u].size()>sum) s[u].erase(s[u].begin());
}
if(a[u]) s[u].insert(dep[u]);
else if(!s[u].empty()){
int x=*--s[u].end();
s[u].erase(--s[u].end());
s[u].insert(dep[u]);
f[u]+=x-dep[u];
}
}
void solve(int u){
ban[u]=1; sum=siz[u];
ll ret=0;
for(auto v:adj[u]){
if(!ban[v]){
dep[v]=1;
dp(v,u);
merge(s[u],s[v]);
ret+=f[v];
}
else{
auto it=s[v].end();
int len=s[v].size();
for(int j=0; j<min(len,sum); j++)
--it,s[u].insert(*it);
ret+=f[v];
}
while((int)s[u].size()>sum) s[u].erase(s[u].begin());
}
// cerr<<u<<' '<<ret<<'\n';
if(a[u]) ans[u]=ret;
else ans[u]=ret+(s[u].empty()?0:*--s[u].end());
for(auto v:adj[u]){
if(!ban[v]){
for(auto val:s[v]) s[u].erase(s[u].find(val));
s[u].insert(0);
int x=-1;
if(!a[u]){
x=*--s[u].end();
ret+=x;
s[u].erase(--s[u].end());
}
f[u]=ret;
multiset <int> P;
swap(P,s[v]);
sum=siz[v];
rt=0;
calcsiz(v,u);
calcsiz(rt,0);
solve(rt);
if(!a[u])
ret-=x,s[u].insert(x);
s[u].erase(s[u].find(0));
for(auto val:P) s[u].insert(val);
}
}
}
int main(){
Max[0]=0x3f3f3f3f;
n=read();
for(int i=1; i<=n; i++) scanf("%1d",&a[i]);
for(int i=1; i<n; i++){
int u=read(),v=read();
adj[u].pb(v),adj[v].pb(u);
}
sum=n;
calcsiz(1,0),calcsiz(rt,0);
solve(rt);
for(int i=1; i<=n; i++) printf("%lld ",ans[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 20200kb
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: 18232kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 3ms
memory: 18452kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 18144kb
input:
2 10 1 2
output:
1 1
result:
wrong answer 1st numbers differ - expected: '0', found: '1'