Submission #1690663
Source Code Expand
/* 神题 二分答案+分析性质 合并集合优化DP,满二叉树-->O(n*logn) O(n*logn*logw) */ #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define N 131110 #define ll long long #define top 1000000000000LL #define pa(a, b) (data){a, b} using namespace std; struct data{ll in, out;}; int n, sum[N], son[N][2], w[N], x, y; ll L, R, mid; vector<data>s[N], s1, s2; inline char gc(){ return getchar(); static char now[1<<16], *S, *T; if(S==T){T=(S=now)+fread(now, 1, 1<<16, stdin); if(S==T)return EOF;} return *S++; } inline int read(){ int x=0, f=1; char ch=gc(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=gc();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=gc();} return x*f; } inline void dp(int x, ll L){ if(!sum[x]){s[x].push_back(pa(0, 0)); return;} int l=son[x][0], r=son[x][1]; dp(l, L); dp(r, L); s[x].clear(); s1.clear(); s2.clear(); int now=-1, now1=-1; for(int i=0; i<s[l].size(); i++){ while(now+1<s[r].size()&&s[l][i].out+w[l]+w[r]+s[r][now+1].in<=L)now++; if(now==now1)continue; s1.push_back(pa(s[l][i].in+w[l], w[r]+s[r][now].out)); now1=now; } now=now1=-1; for(int i=0; i<s[r].size(); i++){ while(now+1<s[l].size()&&s[r][i].out+w[r]+w[l]+s[l][now+1].in<=L)now++; if(now==now1)continue; s2.push_back(pa(s[r][i].in+w[r], w[l]+s[l][now].out)); now1=now; } int p1=0, p2=0; while(p1<s1.size()&&p2<s2.size()){ if(s1[p1].in<s2[p2].in){s[x].push_back(s1[p1]); p1++;} else if(s1[p1].in>s2[p2].in){s[x].push_back(s2[p2]); p2++;} else{s[x].push_back(pa(s1[p1].in, min(s1[p1].out, s2[p2].out))); p1++; p2++;} } while(p1<s1.size()){s[x].push_back(s1[p1]); p1++;} while(p2<s2.size()){s[x].push_back(s2[p2]); p2++;} } inline int check(ll L){ dp(1, L); return s[1].size()?1:0; } int main(){ n=read(); memset(sum, 0, sizeof(sum)); for(int i=2; i<=n; i++){ x=read(); w[i]=read(); sum[x]++; son[x][sum[x]-1]=i; } L=0; R=top; while(L<R){ mid=(L+R)>>1; if(check(mid))R=mid; else L=mid+1; } printf("%lld", L); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Shik and Travel |
User | leoly |
Language | C++14 (GCC 5.4.1) |
Score | 1400 |
Code Size | 2093 Byte |
Status | AC |
Exec Time | 684 ms |
Memory | 93028 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1400 / 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 | AC | 9 ms | 5888 KB |
001.txt | AC | 9 ms | 5888 KB |
002.txt | AC | 9 ms | 5888 KB |
003.txt | AC | 9 ms | 5888 KB |
004.txt | AC | 9 ms | 5888 KB |
005.txt | AC | 9 ms | 5888 KB |
006.txt | AC | 9 ms | 5888 KB |
007.txt | AC | 9 ms | 5888 KB |
008.txt | AC | 9 ms | 5888 KB |
009.txt | AC | 9 ms | 5888 KB |
010.txt | AC | 547 ms | 77440 KB |
011.txt | AC | 546 ms | 77440 KB |
012.txt | AC | 556 ms | 77440 KB |
013.txt | AC | 548 ms | 77440 KB |
014.txt | AC | 526 ms | 77440 KB |
015.txt | AC | 549 ms | 77440 KB |
016.txt | AC | 554 ms | 77440 KB |
017.txt | AC | 564 ms | 77440 KB |
018.txt | AC | 560 ms | 77440 KB |
019.txt | AC | 545 ms | 77312 KB |
020.txt | AC | 548 ms | 77440 KB |
021.txt | AC | 546 ms | 77440 KB |
022.txt | AC | 547 ms | 77440 KB |
023.txt | AC | 545 ms | 77440 KB |
024.txt | AC | 541 ms | 77440 KB |
025.txt | AC | 552 ms | 77440 KB |
026.txt | AC | 541 ms | 76928 KB |
027.txt | AC | 538 ms | 77440 KB |
028.txt | AC | 545 ms | 76928 KB |
029.txt | AC | 539 ms | 76928 KB |
030.txt | AC | 488 ms | 76928 KB |
031.txt | AC | 543 ms | 77440 KB |
032.txt | AC | 516 ms | 77440 KB |
033.txt | AC | 488 ms | 76928 KB |
034.txt | AC | 484 ms | 76928 KB |
035.txt | AC | 552 ms | 77440 KB |
036.txt | AC | 484 ms | 76928 KB |
037.txt | AC | 488 ms | 76672 KB |
038.txt | AC | 487 ms | 76928 KB |
039.txt | AC | 509 ms | 76416 KB |
040.txt | AC | 558 ms | 92900 KB |
041.txt | AC | 637 ms | 93028 KB |
042.txt | AC | 643 ms | 93028 KB |
043.txt | AC | 638 ms | 93028 KB |
044.txt | AC | 558 ms | 83584 KB |
045.txt | AC | 684 ms | 79488 KB |
046.txt | AC | 682 ms | 80640 KB |
047.txt | AC | 681 ms | 79872 KB |
048.txt | AC | 674 ms | 82304 KB |
049.txt | AC | 3 ms | 4864 KB |
example0.txt | AC | 3 ms | 4864 KB |
example1.txt | AC | 3 ms | 4864 KB |
example2.txt | AC | 3 ms | 4864 KB |
example3.txt | AC | 3 ms | 4864 KB |