QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#740353 | #9630. 沙堆 | huaxiamengjin | TL | 4ms | 38080kb | C++14 | 2.6kb | 2024-11-13 09:18:33 | 2024-11-13 09:18:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int>g[1001001];
int a[1001001],dep[1001001];
set<int>s;
int tot,dfn[1001001],sz[1001001],idfn[1001001];
int st[1001001][21];
int lowbit(int x){return x&(-x);}
void dfs(int x,int pa){
dfn[x]=++tot,sz[x]=1;
idfn[tot]=x;dep[x]=dep[pa]+1;
st[x][0]=pa;
for (auto y:g[x])if(y!=pa){
dfs(y,x);sz[x]+=sz[y];
}
}
void del(int x){
if(a[x]<(int)g[x].size()-1)
s.erase(dfn[x]);
}
void ins(int x){
if(a[x]<(int)g[x].size()-1)
s.insert(dfn[x]);
}
void init(){
for (int i=1;i<=20;i++)
for (int j=1;j<=n;j++)
st[j][i]=st[st[j][i-1]][i-1];
}
int _lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
int t=dep[x]-dep[y];
for (;t;t-=lowbit(t))x=st[x][__lg(t)];
if(x==y)return x;
for (int i=20;~i;i--)
if(st[x][i]!=st[y][i])x=st[x][i],y=st[y][i];
return st[x][0];
}
int find(int x,int k){
for (;k;k-=lowbit(k))x=st[x][__lg(k)];
return x;
}
void dfs2(int x,int pa){
for (auto y:g[x])if(y!=pa)dfs2(y,x);
while(a[x]>=g[x].size()){
// cout<<a[x]<<"!!!!"<<x<<"\n";
// a[x]--;a[fa[x]]++;
int now=dfn[x]+1,pre=x,mn=a[x]-g[x].size()+1;
vector<int>v;
while(now<dfn[x]+sz[x]){
auto it=s.lower_bound(now);
if(it==s.end())break;
if((*it)>=dfn[x]+sz[x])break;
int itt=idfn[*it];
if(pre==x)mn=dep[itt]-dep[pre];
else mn=min(mn,min(dep[pre],dep[itt])-dep[_lca(pre,itt)]);
// cout<<pre<<"######################"<<itt<<"\n";
v.push_back(itt);
now=dfn[itt]+sz[itt];pre=itt;
}
// cout<<mn<<"#######\n";if(mn==1e9)exit(0);
a[x]-=mn,a[st[x][0]]+=mn;
for (auto itt:v){
int fa=find(itt,mn);
del(itt);del(fa);
a[itt]++,a[fa]--;
ins(itt);ins(fa);
}
}
// cout<<x<<"@@@@@@\n";
// for (int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<"\n";
// for (int i=1;i<=n;i++)cout<<g[i].size()<<" ";cout<<"\n";
// for (auto i:s)cout<<idfn[i]<<" ";cout<<"\n";
ins(x);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for (int i=1,x,y;i<n;i++)
cin>>x>>y,g[x].push_back(y),g[y].push_back(x);
long long sum=0;
for (int i=1;i<=n;i++)
cin>>a[i],sum+=(int)g[i].size()-1-a[i];
if(sum<0)return cout<<-1<<"\n",0;
dfs(1,1);init();dfs2(1,1);
for (int i=1;i<n;i++)
cout<<a[i]<<" ";
cout<<a[n]<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 38080kb
input:
6 1 2 2 3 2 4 1 5 4 6 1 1 0 0 1 1
output:
1 2 0 1 0 0
result:
ok single line: '1 2 0 1 0 0'
Test #2:
score: 0
Accepted
time: 4ms
memory: 37976kb
input:
12 1 2 1 3 2 4 3 5 5 6 2 7 7 8 4 9 8 10 5 11 3 12 2 0 0 0 1 0 1 0 1 1 0 1
output:
0 1 2 1 1 0 1 1 0 0 0 0
result:
ok single line: '0 1 2 1 1 0 1 1 0 0 0 0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 37880kb
input:
40 1 2 2 3 1 4 3 5 5 6 6 7 4 8 6 9 8 10 6 11 6 12 9 13 10 14 7 15 9 16 15 17 15 18 12 19 18 20 16 21 18 22 22 23 5 24 22 25 2 26 24 27 14 28 27 29 20 30 29 31 30 32 20 33 26 34 26 35 19 36 11 37 34 38 37 39 29 40 3 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 0 1 1 3 0 1 0 3 0 0 0 1 0 0 0 0 0 0 0 0 0 2 1
output:
1 1 0 1 0 4 1 0 2 0 1 0 0 0 0 1 0 2 1 1 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0
result:
ok single line: '1 1 0 1 0 4 1 0 2 0 1 0 0 0 0 ...0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0'
Test #4:
score: -100
Time Limit Exceeded
input:
5000 1 2 1 3 2 4 4 5 3 6 5 7 7 8 6 9 8 10 9 11 10 12 11 13 13 14 14 15 12 16 16 17 15 18 17 19 16 20 18 21 16 22 15 23 20 24 24 25 25 26 22 27 23 28 19 29 27 30 29 31 27 32 28 33 32 34 31 35 26 36 34 37 35 38 35 39 33 40 38 41 40 42 42 43 30 44 41 45 37 46 39 47 47 48 36 49 48 50 46 51 44 52 51 53 4...