Submission #1884018


Source Code Expand

// Shik and Travel
// * frank_c1
// * 2017 / 12 / 18
 
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

const int maxn = (int)(1e5) + 5;
int wi[maxn], ai[maxn], bi[maxn];
int st[maxn], su[maxn];
pii ci[maxn * 22], ta[maxn], tb[maxn];
int n, idx;

bool chk(int x) {
	idx = 0;
	for (int i = n; i >= 1; --i) {
		st[i] = idx + 1;
		if (!ai[i]) ci[++idx] = make_pair(0, 0);
		else {
			int u = ai[i], v = bi[i];
			if (su[u] > su[v]) swap(u, v);
			int mx = x - wi[u] - wi[v];
			int ca = 0, cb = 0, ww = (int)(1e9), ps = 1;
			for (int j = st[u]; j < st[u] + su[u]; ++j) ta[++ca] = make_pair(ci[j].first, ci[j].second);
			for (int j = st[v]; j < st[v] + su[v]; ++j) tb[++cb] = make_pair(ci[j].second, ci[j].first);
			sort(ta + 1, ta + ca + 1); sort(tb + 1, tb + cb + 1);
			for (int j = ca; j >= 1; --j) {
				while (ps <= cb && ta[j].first + tb[ps].first <= mx) ww = min(ww, tb[ps++].second);	
				if (ps > 1) ci[++idx] = make_pair(ww + wi[v], ta[j].second + wi[u]);
			}
			ca = cb = 0; ww = (int)(1e9); ps = 1;
			for (int j = st[u]; j < st[u] + su[u]; ++j) ta[++ca] = make_pair(ci[j].second, ci[j].first);
			for (int j = st[v]; j < st[v] + su[v]; ++j) tb[++cb] = make_pair(ci[j].first, ci[j].second);
			sort(ta + 1, ta + ca + 1); sort(tb + 1, tb + cb + 1);
			for (int j = ca; j >= 1; --j) {
				while (ps <= cb && ta[j].first + tb[ps].first <= mx) ww = min(ww, tb[ps++].second);
				if (ps > 1) ci[++idx] = make_pair(ta[j].second + wi[u], ww + wi[v]);
			}
		}
		su[i] = idx - st[i] + 1;
	}
	return su[1] > 0;
}

int main() {
	scanf("%d", &n);
	for (int i = 2; i <= n; ++i) {
		int f; scanf("%d%d", &f, &wi[i]);
		if (!ai[f]) ai[f] = i; else bi[f] = i;
	}
	int l = 0, r = 20 * (1 << 17);
	while (l < r) {
		int mid = (l + r) >> 1;
		if (chk(mid)) r = mid; else l = mid + 1;
	}
	return printf("%d\n", l), 0;
}

Submission Info

Submission Time
Task E - Shik and Travel
User frank_c1
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1896 Byte
Status RE
Exec Time 294 ms
Memory 2560 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:47:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
./Main.cpp:49:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   int f; scanf("%d%d", &f, &wi[i]);
                                   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1400
Status
AC × 4
AC × 15
RE × 39
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 2304 KB
001.txt AC 4 ms 2304 KB
002.txt AC 4 ms 2304 KB
003.txt AC 4 ms 2304 KB
004.txt AC 4 ms 2304 KB
005.txt AC 4 ms 2304 KB
006.txt AC 4 ms 2304 KB
007.txt AC 4 ms 2304 KB
008.txt AC 4 ms 2304 KB
009.txt AC 4 ms 2304 KB
010.txt RE 294 ms 2560 KB
011.txt RE 114 ms 2560 KB
012.txt RE 114 ms 2560 KB
013.txt RE 114 ms 2560 KB
014.txt RE 116 ms 2560 KB
015.txt RE 114 ms 2560 KB
016.txt RE 116 ms 2560 KB
017.txt RE 116 ms 2560 KB
018.txt RE 114 ms 2560 KB
019.txt RE 115 ms 2560 KB
020.txt RE 114 ms 2560 KB
021.txt RE 115 ms 2560 KB
022.txt RE 116 ms 2560 KB
023.txt RE 116 ms 2560 KB
024.txt RE 116 ms 2560 KB
025.txt RE 115 ms 2560 KB
026.txt RE 115 ms 2560 KB
027.txt RE 114 ms 2560 KB
028.txt RE 115 ms 2560 KB
029.txt RE 115 ms 2560 KB
030.txt RE 115 ms 2560 KB
031.txt RE 114 ms 2560 KB
032.txt RE 115 ms 2560 KB
033.txt RE 114 ms 2560 KB
034.txt RE 114 ms 2560 KB
035.txt RE 115 ms 2560 KB
036.txt RE 115 ms 2560 KB
037.txt RE 115 ms 2560 KB
038.txt RE 114 ms 2560 KB
039.txt RE 116 ms 2560 KB
040.txt RE 113 ms 2560 KB
041.txt RE 114 ms 2560 KB
042.txt RE 114 ms 2560 KB
043.txt RE 113 ms 2560 KB
044.txt RE 114 ms 2560 KB
045.txt RE 115 ms 2560 KB
046.txt RE 114 ms 2560 KB
047.txt RE 114 ms 2560 KB
048.txt RE 115 ms 2560 KB
049.txt AC 2 ms 2304 KB
example0.txt AC 2 ms 2304 KB
example1.txt AC 2 ms 2304 KB
example2.txt AC 2 ms 2304 KB
example3.txt AC 1 ms 2304 KB