QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#442530 | #8518. Roars III | ucup-team2279# | WA | 4ms | 18124kb | C++20 | 2.7kb | 2024-06-15 12:37:58 | 2024-06-15 12:37:59 |
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;
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:s[v]) 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;
for(int i=1; i<=n; i++) dp(i,0),printf("%lld ",f[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18100kb
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: 2ms
memory: 16152kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 4ms
memory: 18124kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 16164kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 16184kb
input:
3 100 2 3 2 1
output:
0 1 2
result:
ok 3 number(s): "0 1 2"
Test #6:
score: -100
Wrong Answer
time: 3ms
memory: 16168kb
input:
4 1100 4 1 4 3 4 2
output:
1 1 3 0
result:
wrong answer 4th numbers differ - expected: '1', found: '0'