Submission #1824786


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <ctime>
#include<complex>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;


const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 2 * 1000 * 100 + 47;
vector<PII> g[MAX];
vector<PLL> A[MAX];
int sz;

bool cmp(PLL a, PLL b)
{
	return a.second < b.second;
}

int dfs(int v, LL T)
{
	if (SZ(g[v]) == 0)
	{
		A[sz].push_back(MP(0, 0));
		sz++;
		return sz - 1;
	}

	int i1 = dfs(g[v][0].first, T);
	int i2 = dfs(g[v][1].first, T);
	int vi = g[v][0].second;
	int vk = g[v][1].second;
	int p = 0;
	sort(ALL(A[i1]), cmp);
	sort(ALL(A[i2]));
	LL mind = LINF;
	RFOR(i, SZ(A[i1]), 0)
	{
		LL a = A[i1][i].first;
		LL b = A[i1][i].second;
		while (p != SZ(A[i2]) && b + A[i2][p].first + vi + vk <= T)
		{
			mind = min(A[i2][p].second, mind);
			p++;
		}

		if (mind == LINF) continue;
		A[sz].push_back(MP(a + vi, mind + vk));
	}

	sort(ALL(A[i1]));
	sort(ALL(A[i2]), cmp);
	LL minc = LINF;
	p = 0;
	
	RFOR(i,SZ(A[i1]), 0)
	{
		LL a = A[i1][i].first;
		LL b = A[i1][i].second;

		while (p != SZ(A[i2]) && a + A[i2][p].second + vi + vk <= T)
		{
			minc = min(minc, A[i2][p].first);
			p++;
		}

		if (minc == LINF) continue;
		A[sz].push_back(MP(minc + vk, b + vi));
	}

	sz++;
	return sz - 1;
}

bool ok(LL c)
{
	FOR(i, 0, sz) A[i].clear();
	sz = 0;
	int ind = dfs(0, c);
	return SZ(A[ind]);
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	int n, p, v;
	cin >> n;
	FOR(i, 0, n - 1)
	{
		cin >> p >> v;
		p--;
		g[p].push_back(MP(i + 1, v));
	}

	LL l = -1, r = 1000 * (LL)1000 * (LL)1000 * (LL)1000;
	while (r - l > 1)
	{
		//cout << l << " " << r << endl;
		LL c = (r + l) / 2;
		if (ok(c))
		{
			r = c;
		}
		else
		{
			l = c;
		}
	}

	cout << r << endl;
}

Submission Info

Submission Time
Task E - Shik and Travel
User vjudge2
Language C++14 (Clang 3.8.0)
Score 1400
Code Size 2591 Byte
Status AC
Exec Time 3172 ms
Memory 62432 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 24 ms 9984 KB
001.txt AC 21 ms 9856 KB
002.txt AC 31 ms 10112 KB
003.txt AC 20 ms 9856 KB
004.txt AC 20 ms 9856 KB
005.txt AC 22 ms 9984 KB
006.txt AC 18 ms 9856 KB
007.txt AC 21 ms 9856 KB
008.txt AC 20 ms 9856 KB
009.txt AC 17 ms 9856 KB
010.txt AC 1667 ms 32524 KB
011.txt AC 3172 ms 62432 KB
012.txt AC 1517 ms 29816 KB
013.txt AC 1653 ms 31736 KB
014.txt AC 1917 ms 35196 KB
015.txt AC 1552 ms 29688 KB
016.txt AC 1352 ms 27652 KB
017.txt AC 1584 ms 32524 KB
018.txt AC 1703 ms 32252 KB
019.txt AC 1615 ms 33668 KB
020.txt AC 2076 ms 37908 KB
021.txt AC 1901 ms 35276 KB
022.txt AC 1900 ms 36728 KB
023.txt AC 1412 ms 30332 KB
024.txt AC 1545 ms 30820 KB
025.txt AC 1417 ms 28172 KB
026.txt AC 1577 ms 32408 KB
027.txt AC 1926 ms 35684 KB
028.txt AC 1475 ms 33148 KB
029.txt AC 1626 ms 33340 KB
030.txt AC 1465 ms 31736 KB
031.txt AC 1579 ms 31736 KB
032.txt AC 1605 ms 31736 KB
033.txt AC 1457 ms 33780 KB
034.txt AC 1471 ms 31736 KB
035.txt AC 1614 ms 31736 KB
036.txt AC 1498 ms 31736 KB
037.txt AC 1444 ms 31736 KB
038.txt AC 1484 ms 33780 KB
039.txt AC 1116 ms 31736 KB
040.txt AC 1300 ms 31736 KB
041.txt AC 1436 ms 31736 KB
042.txt AC 1469 ms 31736 KB
043.txt AC 1487 ms 31736 KB
044.txt AC 404 ms 27008 KB
045.txt AC 402 ms 21888 KB
046.txt AC 408 ms 23424 KB
047.txt AC 406 ms 22400 KB
048.txt AC 411 ms 25472 KB
049.txt AC 5 ms 9600 KB
example0.txt AC 5 ms 9600 KB
example1.txt AC 4 ms 9600 KB
example2.txt AC 4 ms 9600 KB
example3.txt AC 4 ms 9600 KB