# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1092443 | 2024-09-24T06:17:51 Z | alexander707070 | Fireworks (APIO16_fireworks) | C++14 | 989 ms | 2780 KB |
#include<bits/stdc++.h> #define MAXN 100007 using namespace std; const long long inf=1e17; int n,m; int p[MAXN],c[MAXN]; vector< pair<int,long long> > v[MAXN]; vector<long long> dists; long long sum[MAXN],sz[MAXN],ans; void dfs(int x,long long d){ sz[x]=(v[x].empty()); if(sz[x]==1)dists.push_back(d); for(int i=0;i<v[x].size();i++){ dfs(v[x][i].first,d+v[x][i].second); sz[x]+=sz[v[x][i].first]; sum[x]+=sum[v[x][i].first]+v[x][i].second*sz[v[x][i].first]; } } long long solve(int x,long long len,long long e){ long long res=0; long long diff=sum[x]+sz[x]*e-sz[x]*len; while(diff<-sz[x]/2){diff+=sz[x]; e++; res++;} while(diff>sz[x]/2 and e>0){diff-=sz[x]; e--; res++;} for(int i=0;i<v[x].size();i++){ res+=solve(v[x][i].first,len-e,v[x][i].second); } return res; } int main(){ cin>>n>>m; n+=m; for(int i=2;i<=n;i++){ cin>>p[i]>>c[i]; v[p[i]].push_back({i,c[i]}); } dfs(1,0); ans=inf; for(long long len:dists){ ans=min(ans,solve(2,len,v[1][0].second)); } cout<<ans<<"\n"; return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2648 KB | Output is correct |
2 | Incorrect | 989 ms | 2780 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 2652 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2648 KB | Output is correct |
2 | Incorrect | 989 ms | 2780 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2648 KB | Output is correct |
2 | Incorrect | 989 ms | 2780 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |