Submission #7964497


Source Code Expand

#include<bits/stdc++.h>
#define pr std::pair<ll,ll>
#define fir first
#define sec second
typedef long long ll;
ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
ll mid;
std::vector<pr>f[131113];
int ls[131113],lw[131113],rs[131113],rw[131113];
void Merge(int A,int B,std::vector<pr>&F){
	int i=0;
	for(pr _:f[A]){
		ll a=_.fir,b=_.sec;
		while(i<f[B].size()-1&&b+f[B][i+1].fir<=mid)++i;
		if(i<f[B].size()&&b+f[B][i].fir<=mid){
			ll d=f[B][i].sec;
			F.emplace_back(a,d);
			F.emplace_back(d,a);
		}
	}
}
void dfs(int x){
	f[x].clear();
	if(!ls[x]){f[x].emplace_back(0,0);return;}
	dfs(ls[x]),dfs(rs[x]);
	if(f[ls[x]].size()>f[rs[x]].size())std::swap(ls[x],rs[x]),std::swap(lw[x],rw[x]);
	for(pr&i:f[ls[x]])i.fir+=lw[x],i.sec+=lw[x];
	for(pr&i:f[rs[x]])i.fir+=rw[x],i.sec+=rw[x];
	Merge(ls[x],rs[x],f[x]);
	std::sort(f[x].begin(),f[x].end());
	pr lst(-1,9e18);
	for(pr&i:f[x])
		if(i.sec>=lst.sec)i.fir=9e18;
		else lst=i;
	std::sort(f[x].begin(),f[x].end());
	while(!f[x].empty()&&f[x].back().fir==9e18)f[x].pop_back();
}
int main(){
#ifdef XZZSB
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n=gi();
	for(int i=2;i<=n;++i){
		int fa=gi(),w=gi();
		if(ls[fa])rs[fa]=i,rw[fa]=w;
		else ls[fa]=i,lw[fa]=w;
	}
	ll l=0,r=n*131072ll;
	while(l<r){
		mid=(l+r)>>1;
		dfs(1);
		if(!f[1].empty())r=mid;
		else l=mid+1;
	}
	printf("%lld\n",l);
	return 0;
}

Submission Info

Submission Time
Task E - Shik and Travel
User test12345
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 1542 Byte
Status AC
Exec Time 659 ms
Memory 28656 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1400 / 1400
Status
AC × 4
AC × 54
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 4 ms 3456 KB
001.txt AC 4 ms 3456 KB
002.txt AC 5 ms 3456 KB
003.txt AC 4 ms 3456 KB
004.txt AC 4 ms 3456 KB
005.txt AC 4 ms 3456 KB
006.txt AC 4 ms 3456 KB
007.txt AC 4 ms 3456 KB
008.txt AC 4 ms 3456 KB
009.txt AC 4 ms 3456 KB
010.txt AC 187 ms 11136 KB
011.txt AC 186 ms 11136 KB
012.txt AC 199 ms 11136 KB
013.txt AC 191 ms 11136 KB
014.txt AC 192 ms 11136 KB
015.txt AC 187 ms 11136 KB
016.txt AC 188 ms 11136 KB
017.txt AC 181 ms 11136 KB
018.txt AC 183 ms 11136 KB
019.txt AC 183 ms 11136 KB
020.txt AC 181 ms 11136 KB
021.txt AC 183 ms 11136 KB
022.txt AC 182 ms 11136 KB
023.txt AC 183 ms 11136 KB
024.txt AC 181 ms 11136 KB
025.txt AC 186 ms 11136 KB
026.txt AC 169 ms 10752 KB
027.txt AC 181 ms 11136 KB
028.txt AC 165 ms 10752 KB
029.txt AC 163 ms 10752 KB
030.txt AC 175 ms 10496 KB
031.txt AC 188 ms 11520 KB
032.txt AC 187 ms 11520 KB
033.txt AC 174 ms 10496 KB
034.txt AC 173 ms 10496 KB
035.txt AC 190 ms 11520 KB
036.txt AC 174 ms 10496 KB
037.txt AC 170 ms 10240 KB
038.txt AC 174 ms 10496 KB
039.txt AC 140 ms 10496 KB
040.txt AC 570 ms 27632 KB
041.txt AC 651 ms 28656 KB
042.txt AC 659 ms 28656 KB
043.txt AC 632 ms 28656 KB
044.txt AC 155 ms 14592 KB
045.txt AC 154 ms 12544 KB
046.txt AC 157 ms 13184 KB
047.txt AC 154 ms 12672 KB
048.txt AC 151 ms 13952 KB
049.txt AC 2 ms 3328 KB
example0.txt AC 2 ms 3328 KB
example1.txt AC 2 ms 3328 KB
example2.txt AC 2 ms 3328 KB
example3.txt AC 2 ms 3328 KB