Submission #1772574


Source Code Expand

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Edge {
	int dest, len;
};

Edge children[131075][2];

long long lim;

vector<pair<long long, long long>> g(const vector<pair<long long, long long>> &r0, const vector<pair<long long, long long>> &r1) {
	vector<pair<long long, long long>> result;
	for (auto il = r0.cbegin(), ir = r1.cbegin(); il != r0.cend() || ir != r1.cend(); ) {
		if (il != r0.cend() && (ir == r1.cend() || il->first < ir->first || (il->first == ir->first && il->second == ir->second))) {
			if (result.empty() || il->second < result.back().second)
				result.push_back(*il);
			++il;
		} else {
			if (result.empty() || ir->second < result.back().second)
				result.push_back(*ir);
			++ir;
		}
	}

	return result;
}

vector<pair<long long, long long>> f(int x) {
	if (children[x][0].dest == 0) return {{0, 0}};
	
	vector<pair<long long, long long>> ls = f(children[x][0].dest), rs = f(children[x][1].dest), result0, result1;

	auto check = [=](vector<pair<long long, long long>>::const_iterator i1, vector<pair<long long, long long>>::const_iterator i2) {
		return i1->second + i2->first <= lim - (children[x][0].len + children[x][1].len);
	};

	for (auto il = ls.cbegin(), ir = rs.cbegin(); il != ls.cend(); ++il) {
		while (ir != rs.cend() && check(il, ir)) ++ir;
		if (ir != rs.cbegin()) result0.emplace_back(il->first + children[x][0].len, (ir - 1)->second + children[x][1].len);
	}

	for (auto ir = rs.cbegin(), il = ls.cbegin(); ir != rs.cend(); ++ir) {
		while (il != ls.cend() && check(ir, il)) ++il;
		if (il != ls.cbegin()) result1.emplace_back(ir->first + children[x][1].len, (il - 1)->second + children[x][0].len);
	}

	auto result = g(result0, result1);
	// cerr << '[' << lim << ']' << ' ' << x << endl;
	// cerr << '0' << '\t';
	// for (auto p: result0) cerr << '(' << p.first << ',' << ' ' << p.second << ')' << '\t';
	// cerr << endl << '1' << '\t';
	// for (auto p: result1) cerr << '(' << p.first << ',' << ' ' << p.second << ')' << '\t';
	// cerr << endl << 'g' << '\t';
	// for (auto p: result) cerr << '(' << p.first << ',' << ' ' << p.second << ')' << '\t';
	// cerr << endl;

	return result;
}

int main() {
	int n;
	cin >> n;

	for (int i = 2; i <= n; ++i) {
		int a, v;
		cin >> a >> v;
		if (children[a][0].dest == 0) {
			children[a][0] = (Edge){i, v};
		} else {
			children[a][1] = (Edge){i, v};
		}
	}

	long long low = 0, high = 131072LL * (n - 1);
	while (low < high) {
		long long mid = (low + high) / 2;
		lim = mid;
		if (!f(1).empty()) high = mid; else low = mid + 1;
	}

	cout << low << endl;

	return 0;
}

Submission Info

Submission Time
Task E - Shik and Travel
User TRCYX
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 2692 Byte
Status AC
Exec Time 848 ms
Memory 19712 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 10 ms 256 KB
001.txt AC 9 ms 256 KB
002.txt AC 10 ms 256 KB
003.txt AC 10 ms 256 KB
004.txt AC 10 ms 256 KB
005.txt AC 10 ms 256 KB
006.txt AC 10 ms 256 KB
007.txt AC 10 ms 256 KB
008.txt AC 10 ms 256 KB
009.txt AC 10 ms 256 KB
010.txt AC 666 ms 2304 KB
011.txt AC 677 ms 2304 KB
012.txt AC 676 ms 2304 KB
013.txt AC 671 ms 2304 KB
014.txt AC 679 ms 2304 KB
015.txt AC 680 ms 2304 KB
016.txt AC 666 ms 2304 KB
017.txt AC 666 ms 2304 KB
018.txt AC 676 ms 2304 KB
019.txt AC 697 ms 2304 KB
020.txt AC 703 ms 2304 KB
021.txt AC 675 ms 2304 KB
022.txt AC 682 ms 2304 KB
023.txt AC 673 ms 2304 KB
024.txt AC 675 ms 2304 KB
025.txt AC 672 ms 2304 KB
026.txt AC 582 ms 2304 KB
027.txt AC 678 ms 2304 KB
028.txt AC 577 ms 2304 KB
029.txt AC 577 ms 2304 KB
030.txt AC 658 ms 1280 KB
031.txt AC 656 ms 2304 KB
032.txt AC 671 ms 2304 KB
033.txt AC 661 ms 1280 KB
034.txt AC 650 ms 1280 KB
035.txt AC 682 ms 2304 KB
036.txt AC 655 ms 1280 KB
037.txt AC 629 ms 1280 KB
038.txt AC 659 ms 1280 KB
039.txt AC 476 ms 2304 KB
040.txt AC 800 ms 5324 KB
041.txt AC 848 ms 6348 KB
042.txt AC 844 ms 6348 KB
043.txt AC 821 ms 6348 KB
044.txt AC 586 ms 19712 KB
045.txt AC 643 ms 11008 KB
046.txt AC 594 ms 13568 KB
047.txt AC 597 ms 11776 KB
048.txt AC 610 ms 16896 KB
049.txt AC 1 ms 256 KB
example0.txt AC 1 ms 256 KB
example1.txt AC 1 ms 256 KB
example2.txt AC 1 ms 256 KB
example3.txt AC 1 ms 256 KB