Submission #1820496


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;
	FOR(i, 0, SZ(A[i1]))
	{
		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;
	
	FOR(i, 0, SZ(A[i1]))
	{
		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 vjudge5
Language C++14 (Clang 3.8.0)
Score 0
Code Size 2590 Byte
Status WA
Exec Time 3483 ms
Memory 62432 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1400
Status
AC × 4
AC × 34
WA × 20
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 25 ms 9984 KB
001.txt WA 21 ms 9856 KB
002.txt WA 32 ms 10112 KB
003.txt WA 20 ms 9856 KB
004.txt WA 21 ms 9856 KB
005.txt AC 24 ms 9984 KB
006.txt AC 19 ms 9856 KB
007.txt WA 21 ms 9856 KB
008.txt WA 20 ms 9856 KB
009.txt WA 17 ms 9856 KB
010.txt WA 1753 ms 32524 KB
011.txt WA 3483 ms 62432 KB
012.txt WA 1645 ms 29816 KB
013.txt AC 1724 ms 31736 KB
014.txt WA 2037 ms 35196 KB
015.txt AC 1540 ms 29688 KB
016.txt AC 1381 ms 27652 KB
017.txt AC 1691 ms 32524 KB
018.txt AC 1766 ms 32252 KB
019.txt WA 1681 ms 31620 KB
020.txt WA 2163 ms 37908 KB
021.txt WA 1887 ms 35276 KB
022.txt WA 2047 ms 37112 KB
023.txt WA 1439 ms 28284 KB
024.txt WA 1594 ms 30820 KB
025.txt WA 1451 ms 28172 KB
026.txt AC 1665 ms 32408 KB
027.txt AC 2033 ms 37728 KB
028.txt WA 1553 ms 31104 KB
029.txt AC 1723 ms 33340 KB
030.txt AC 1482 ms 31736 KB
031.txt AC 1659 ms 31736 KB
032.txt AC 1665 ms 33780 KB
033.txt AC 1479 ms 31736 KB
034.txt AC 1492 ms 31736 KB
035.txt AC 1667 ms 31736 KB
036.txt AC 1503 ms 31736 KB
037.txt AC 1466 ms 33780 KB
038.txt AC 1504 ms 31736 KB
039.txt AC 1176 ms 31736 KB
040.txt AC 1146 ms 31736 KB
041.txt AC 1310 ms 31736 KB
042.txt AC 1321 ms 31736 KB
043.txt AC 1315 ms 31736 KB
044.txt AC 409 ms 28032 KB
045.txt AC 401 ms 22400 KB
046.txt AC 413 ms 24064 KB
047.txt AC 407 ms 22912 KB
048.txt AC 414 ms 26240 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 5 ms 9600 KB
example3.txt AC 5 ms 9600 KB