Submission #1627846
Source Code Expand
#include <cstdio> #include <cstring> #include <algorithm> #define LL long long #define MAXN 200010 using namespace std; int n; int pre[MAXN],ch[MAXN][2]; int size[MAXN],sf[MAXN]; LL v[MAXN],lim; pair<LL,LL> *f[MAXN]; void dfs(int x){ if(ch[x][0]){ dfs(ch[x][0]); dfs(ch[x][1]); if(size[ch[x][0]]>size[ch[x][1]]) swap(ch[x][0],ch[x][1]); size[x]=size[ch[x][0]]<<1; }else{ size[x]=1; } f[x]=new pair<LL,LL>[size[x]+1]; } int join(pair<LL,LL> *fs,pair<LL,LL> *f0,pair<LL,LL> *f1,int sf0,int sf1){ int now0=1,now1=1; int sfs=0; while(now0<=sf0 || now1<=sf1){ if(now1>sf1){ if(!sfs || fs[sfs].second>f0[now0].second) fs[++sfs]=f0[now0]; now0++; }else if(now0>sf0){ if(!sfs || fs[sfs].second>f1[now1].second) fs[++sfs]=f1[now1]; now1++; }else if(f0[now0].first<f1[now1].first){ if(!sfs || fs[sfs].second>f0[now0].second) fs[++sfs]=f0[now0]; now0++; }else{ if(!sfs || fs[sfs].second>f1[now1].second) fs[++sfs]=f1[now1]; now1++; } } return sfs; } bool gao(int x){ if(!ch[x][0]){ sf[x]=1; f[x][1]=make_pair(0,0); return 1; } int c0=ch[x][0],c1=ch[x][1]; if(!gao(c0)) return 0; if(!gao(c1)) return 0; LL delta=v[c0]+v[c1]; static pair<LL,LL> f0[MAXN],f1[MAXN]; int sf0=0,sf1=0; int now=sf[c1]; for(int i=1;i<=sf[c0];i++){ while(now && f[c0][i].second+f[c1][now].first+delta>lim) now--; if(!now) break; f0[++sf0]=make_pair(f[c0][i].first+v[c0],f[c1][now].second+v[c1]); } now=1; for(int i=1;i<=sf[c0];i++){ while(now<=sf[c1] && f[c0][i].first+f[c1][now].second+delta>lim) now++; if(now>sf[c1]) break; f1[++sf1]=make_pair(f[c1][now].first+v[c1],f[c0][i].second+v[c0]); } sf[x]=join(f[x],f0,f1,sf0,sf1); return sf[x]; } bool check(LL _lim){ lim=_lim; return gao(1); } int main(){ #ifdef DEBUG freopen("E.in","r",stdin); #endif scanf("%d",&n); for(int i=2;i<=n;i++){ scanf("%d%lld",pre+i,v+i); if(!ch[pre[i]][0]) ch[pre[i]][0]=i; else ch[pre[i]][1]=i; } dfs(1); LL l=0,r=131072LL*131072LL; while(l<r){ LL mid=(l+r)>>1; if(check(mid)) r=mid; else l=mid+1; } printf("%lld\n",l); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Shik and Travel |
User | ez_zjt |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 2212 Byte |
Status | WA |
Exec Time | 189 ms |
Memory | 31488 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:88:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); ^ ./Main.cpp:90:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%lld",pre+i,v+i); ^
Judge Result
Set Name | Sample | All | ||||||
---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 1400 | ||||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | example0.txt, example1.txt, example2.txt, example3.txt |
All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, example0.txt, example1.txt, example2.txt, example3.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
000.txt | WA | 6 ms | 8576 KB |
001.txt | WA | 5 ms | 8576 KB |
002.txt | AC | 5 ms | 8576 KB |
003.txt | WA | 5 ms | 8576 KB |
004.txt | WA | 5 ms | 8576 KB |
005.txt | WA | 5 ms | 8576 KB |
006.txt | AC | 5 ms | 8576 KB |
007.txt | WA | 5 ms | 8576 KB |
008.txt | WA | 5 ms | 8576 KB |
009.txt | AC | 5 ms | 8576 KB |
010.txt | WA | 163 ms | 18304 KB |
011.txt | WA | 133 ms | 18304 KB |
012.txt | WA | 140 ms | 18304 KB |
013.txt | WA | 152 ms | 18304 KB |
014.txt | WA | 135 ms | 18304 KB |
015.txt | WA | 136 ms | 18304 KB |
016.txt | AC | 142 ms | 18304 KB |
017.txt | AC | 141 ms | 18304 KB |
018.txt | AC | 162 ms | 18304 KB |
019.txt | WA | 145 ms | 18304 KB |
020.txt | WA | 146 ms | 18304 KB |
021.txt | WA | 160 ms | 18304 KB |
022.txt | AC | 150 ms | 18304 KB |
023.txt | AC | 131 ms | 18304 KB |
024.txt | AC | 129 ms | 18304 KB |
025.txt | WA | 136 ms | 18304 KB |
026.txt | AC | 143 ms | 18304 KB |
027.txt | WA | 152 ms | 18304 KB |
028.txt | AC | 151 ms | 18304 KB |
029.txt | AC | 157 ms | 18432 KB |
030.txt | AC | 152 ms | 31488 KB |
031.txt | WA | 183 ms | 31488 KB |
032.txt | AC | 189 ms | 31488 KB |
033.txt | WA | 158 ms | 31488 KB |
034.txt | WA | 143 ms | 31488 KB |
035.txt | AC | 164 ms | 31488 KB |
036.txt | AC | 139 ms | 31488 KB |
037.txt | AC | 149 ms | 31488 KB |
038.txt | AC | 144 ms | 31488 KB |
039.txt | AC | 130 ms | 31488 KB |
040.txt | AC | 130 ms | 31488 KB |
041.txt | AC | 187 ms | 31488 KB |
042.txt | AC | 167 ms | 31488 KB |
043.txt | AC | 183 ms | 31488 KB |
044.txt | AC | 102 ms | 23296 KB |
045.txt | AC | 126 ms | 20224 KB |
046.txt | AC | 126 ms | 21120 KB |
047.txt | AC | 127 ms | 20480 KB |
048.txt | AC | 129 ms | 22272 KB |
049.txt | AC | 4 ms | 8448 KB |
example0.txt | AC | 3 ms | 8448 KB |
example1.txt | AC | 3 ms | 8448 KB |
example2.txt | AC | 3 ms | 8448 KB |
example3.txt | AC | 4 ms | 8448 KB |