Submission #1453851
Source Code Expand
#include <cstdio>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }
template <typename UnaryPredicate>
ll binsearch(ll l, ll r, UnaryPredicate p) { // [l, r), p is monotone
assert (l < r);
-- l;
while (r - l > 1) {
ll m = (l + r) / 2;
(p(m) ? r : l) = m;
}
return r; // = min { x in [l, r) | p(x) }, or r
}
bool pred(vector<vector<pair<int, int> > > const & g, ll limit) {
function<vector<pair<ll, ll> > (int, int)> dfs = [&](int v, int parent) {
if (g[v].size() == 1) {
vector<pair<ll, ll> > result;
result.emplace_back(0, 0);
return result;
}
vector<pair<ll, ll> > left, right;
for (auto edge : g[v]) {
int w, value; tie(w, value) = edge;
if (w == parent) continue;
auto x = dfs(w, v);
auto & y = left.empty() ? left : right;
assert (y.empty());
y = move(x);
repeat (i, y.size()) {
y[i].first += value;
y[i].second += value;
}
}
vector<pair<ll, ll> > result;
auto foo = [&]() {
int i = 0;
int j = right.size() - 1;
while (i < left.size()) {
while (j >= 0 and left[i].second + right[j].first > limit) -- j;
if (j < 0) break;
ll a = left[i].first;
ll b = right[j].second;
if (a > b) swap(a, b);
result.emplace_back(a, b);
++ i;
}
};
foo();
left.swap(right);
foo();
repeat (i, left.size()) {
swap(left[i].first, left[i].second);
}
whole(reverse, left);
foo();
left.swap(right);
foo();
whole(sort, result);
result.erase(whole(unique, result, [&](auto x, auto y) { return x.second <= y.second; }), result.end());
return result;
};
return not dfs(0, -1).empty();
}
constexpr ll inf = 1ll << 35;
int main() {
int n; scanf("%d", &n);
vector<vector<pair<int, int> > > g(n);
repeat (i, n - 1) {
int a, v; scanf("%d%d", &a, &v);
g[a - 1].emplace_back(i + 1, v);
g[i + 1].emplace_back(a - 1, v);
}
ll result = binsearch(0, inf, [&](ll limit) {
return pred(g, limit);
});
printf("%lld\n", result);
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Shik and Travel |
User |
kimiyuki |
Language |
C++14 (Clang 3.8.0) |
Score |
0 |
Code Size |
2958 Byte |
Status |
WA |
Exec Time |
681 ms |
Memory |
33024 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 1400 |
Status |
|
|
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 |
11 ms |
384 KB |
001.txt |
AC |
11 ms |
384 KB |
002.txt |
AC |
11 ms |
384 KB |
003.txt |
AC |
11 ms |
384 KB |
004.txt |
WA |
11 ms |
384 KB |
005.txt |
AC |
11 ms |
384 KB |
006.txt |
AC |
11 ms |
384 KB |
007.txt |
AC |
11 ms |
384 KB |
008.txt |
WA |
11 ms |
384 KB |
009.txt |
AC |
11 ms |
384 KB |
010.txt |
WA |
675 ms |
8448 KB |
011.txt |
AC |
659 ms |
8448 KB |
012.txt |
AC |
652 ms |
8448 KB |
013.txt |
WA |
654 ms |
8448 KB |
014.txt |
AC |
657 ms |
8448 KB |
015.txt |
AC |
681 ms |
8448 KB |
016.txt |
AC |
655 ms |
8448 KB |
017.txt |
AC |
655 ms |
8448 KB |
018.txt |
AC |
657 ms |
8448 KB |
019.txt |
AC |
660 ms |
8448 KB |
020.txt |
WA |
659 ms |
8448 KB |
021.txt |
WA |
655 ms |
8448 KB |
022.txt |
AC |
653 ms |
8448 KB |
023.txt |
AC |
679 ms |
8448 KB |
024.txt |
AC |
658 ms |
8448 KB |
025.txt |
AC |
663 ms |
8448 KB |
026.txt |
AC |
647 ms |
8448 KB |
027.txt |
AC |
658 ms |
8448 KB |
028.txt |
AC |
650 ms |
8448 KB |
029.txt |
AC |
650 ms |
8448 KB |
030.txt |
AC |
608 ms |
8448 KB |
031.txt |
WA |
632 ms |
8448 KB |
032.txt |
AC |
651 ms |
8576 KB |
033.txt |
WA |
609 ms |
8448 KB |
034.txt |
WA |
607 ms |
8448 KB |
035.txt |
AC |
632 ms |
8448 KB |
036.txt |
AC |
605 ms |
8448 KB |
037.txt |
AC |
611 ms |
8448 KB |
038.txt |
AC |
604 ms |
8448 KB |
039.txt |
AC |
611 ms |
8448 KB |
040.txt |
AC |
597 ms |
8448 KB |
041.txt |
AC |
664 ms |
8448 KB |
042.txt |
AC |
641 ms |
8448 KB |
043.txt |
AC |
637 ms |
8448 KB |
044.txt |
AC |
554 ms |
33024 KB |
045.txt |
AC |
618 ms |
20736 KB |
046.txt |
AC |
535 ms |
24320 KB |
047.txt |
AC |
574 ms |
21760 KB |
048.txt |
AC |
545 ms |
29056 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 |