Submission #1610896
Source Code Expand
#ifdef DEBUG
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <cassert>
#include <sstream>
#include <fstream>
#include <functional>
#include <set>
#include <bitset>
#include <string>
#include <utility>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#else
#include <bits/stdc++.h>
#endif
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(i, n) for (int i = 0, i##_end_ = (n); i < i##_end_; ++i)
#define per(i, n) for (int i = (n) - 1; i >= 0; --i)
#define forn(i, l, r) for (int i = (l), i##_end_ = (r); i <= i##_end_; ++i)
#define nrof(i, r, l) for (int i = (r), i##_end_ = (l); i >= i##_end_; --i)
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long LL;
template<typename T> inline bool chkmax(T &x, const T &y) {
return x < y ? x = y, 1 : 0;
}
template<typename T> inline bool chkmin(T &x, const T &y) {
return x > y ? x = y, 1 : 0;
}
#ifdef DEBUG
char *input_file, *output_file;
#endif
struct IO {
static const int maxn = (1 << 25) + 10;
char a[maxn], *s, b[maxn], *t;
void INPUT() {
s = a;
t = b;
#ifdef DEBUG
FILE *f = fopen(input_file, "r");
a[fread(a, 1, sizeof a, f)] = 0;
#else
a[fread(a, 1, sizeof a, stdin)] = 0;
#endif
}
void OUTPUT() {
#ifdef DEBUG
FILE *f = fopen(output_file, "w");
fwrite(b, 1, t - b, f);
#else
fwrite(b, 1, t - b, stdout);
#endif
}
operator int() {
int x = 0;
while(*s != '-' && (*s < '0' || *s > '9')) {
++s;
}
bool f = 0;
if(*s == '-') {
f = 1;
++s;
}
while(*s >= '0' && *s <= '9') {
(x *= 10) += *s - '0';
++s;
}
if(f) {
x = -x;
}
return x;
}
operator LL() {
LL x = 0;
while(*s != '-' && (*s < '0' || *s > '9')) {
++s;
}
bool f = 0;
if(*s == '-') {
f = 1;
++s;
}
while(*s >= '0' && *s <= '9') {
(x *= 10) += *s - '0';
++s;
}
if(f) {
x = -x;
}
return x;
}
operator char() {
while(*s <= 32) {
++s;
}
char ret = *s;
++s;
return ret;
}
inline void out(int x) {
if(!x) {
*t++ = '0';
return;
}
if(x < 0) {
*t++ = '-';
x = -x;
}
static char c[20], *i;
i = c;
while(x) {
int y = x / 10;
*i++ = x - y * 10 + '0';
x = y;
}
while(i != c) {
*t++ = *--i;
}
return;
}
inline void out(int x, char C) {
if(!x) {
*t++ = '0';
*t++ = C;
return;
}
if(x < 0) {
*t++ = '-';
x = -x;
}
static char c[20], *i;
i = c;
while(x) {
int y = x / 10;
*i++ = x - y * 10 + '0';
x = y;
}
while(i != c) {
*t++ = *--i;
}
*t++ = C;
return;
}
inline void out(LL x) {
if(!x) {
*t++ = '0';
return;
}
if(x < 0) {
*t++ = '-';
x = -x;
}
static char c[20], *i;
i = c;
while(x) {
LL y = x / 10;
*i++ = x - y * 10 + '0';
x = y;
}
while(i != c) {
*t++ = *--i;
}
return;
}
inline void out(LL x, char C) {
if(!x) {
*t++ = '0';
*t++ = C;
return;
}
if(x < 0) {
*t++ = '-';
x = -x;
}
static char c[20], *i;
i = c;
while(x) {
LL y = x / 10;
*i++ = x - y * 10 + '0';
x = y;
}
while(i != c) {
*t++ = *--i;
}
*t++ = C;
return;
}
inline void out(char c) {
*t++ = c;
return;
}
inline void out(char *s) {
while(*s >= ' ') {
*t++ = *s++;
}
return;
}
}io;
void Main();
int main(int argc, char *argv[]) {
#ifdef DEBUG
input_file = argv[1];
output_file = argv[2];
#endif
io.INPUT();
Main();
io.OUTPUT();
return 0;
}
//---------------------------------------------------------------------------------------head---------------------------------------------------------------------------------------
const LL oo = 1ll << 62;
const int maxn = 132000;
struct edge {
int to, next;
LL w;
edge() {}
edge(int to, int next, LL w): to(to), next(next), w(w) {}
}e[maxn];
int n, m;
int head[maxn];
LL bound;
vector<pair<LL, LL> > vec, s[maxn], tmp;
void add_edge(int u, int v, LL w) {
e[m] = edge(v, head[u], w);
head[u] = m++;
return;
}
void B(int u, LL delta) {
vec.clear();
for(auto x: s[u]) {
LL a = x.X, b = x.Y;
if(b + delta <= bound) {
vec.pb(mp(a, b + delta));
}
}
s[u].clear();
return;
}
void dfs(int u = 1) {
if(!~head[u]) {
s[u] = {mp(0, 0)};
return;
}
s[u].clear();
int son, chd = 0, mx = 0;
LL delta = 0, wc, ws;
for (int i = head[u]; ~i; i = e[i].next) {
int &v = e[i].to;
dfs(v);
if(!SZ(s[v])) {
return;
}
delta += e[i].w;
chd += v;
if(chkmax(mx, SZ(s[v]))) {
son = v;
ws = e[i].w;
}
}
chd -= son;
wc = delta - ws;
B(chd, delta);
LL mn = oo;
tmp.clear();
for(auto &ivec: vec) {
LL &a = ivec.X, &b = ivec.Y;
if(!chkmin(mn, b)) {
continue;
}
int lb = 0, rb = SZ(s[son]) - 1;
while(lb <= rb) {
int mid = lb + rb >> 1;
if(s[son][mid].X + b > bound) {
rb = mid - 1;
}
else {
lb = mid + 1;
}
}
int i = rb;
if(i >= 0) {
tmp.pb(mp(a + wc, s[son][i].Y + ws));
}
}
s[son].clear();
for (int sz = SZ(tmp), i = 0, j = 0; i < sz || j < sz; ) {
if(i == sz || j < sz && tmp[sz - j + 1] < tmp[i]) {
s[u].pb(tmp[sz - ++j]);
}
else {
s[u].pb(tmp[i++]);
}
}
int sz = 0;
mn = oo;
rep(i, SZ(s[u])) {
if(chkmin(mn, s[u][i].Y)) {
s[u][sz++] = s[u][i];
}
}
s[u].resize(sz);
return;
}
bool check(LL mid) {
bound = mid;
dfs();
return SZ(s[1]);
}
void Main() {
n = io;
LL lb = 0, rb = 0;
memset(head, -1, sizeof(head[0]) * (n + 1));
forn(i, 2, n) {
int u = io;
LL w = io;
rb += w;
add_edge(u, i, w);
}
while(lb <= rb) {
LL mid = lb + rb >> 1;
if(check(mid)) {
rb = mid - 1;
}
else {
lb = mid + 1;
}
}
io.out(lb, '\n');
return;
}
Submission Info
Submission Time |
|
Task |
E - Shik and Travel |
User |
OMTWOCZWEIXVI |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
6235 Byte |
Status |
WA |
Exec Time |
162 ms |
Memory |
23808 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 |
WA |
6 ms |
8448 KB |
001.txt |
WA |
5 ms |
8448 KB |
002.txt |
WA |
6 ms |
8448 KB |
003.txt |
WA |
5 ms |
8448 KB |
004.txt |
WA |
6 ms |
8448 KB |
005.txt |
WA |
6 ms |
8448 KB |
006.txt |
WA |
6 ms |
8448 KB |
007.txt |
WA |
6 ms |
8448 KB |
008.txt |
WA |
6 ms |
8448 KB |
009.txt |
WA |
6 ms |
8448 KB |
010.txt |
WA |
129 ms |
15616 KB |
011.txt |
WA |
135 ms |
15616 KB |
012.txt |
WA |
135 ms |
15616 KB |
013.txt |
WA |
120 ms |
15616 KB |
014.txt |
WA |
119 ms |
15616 KB |
015.txt |
WA |
108 ms |
15616 KB |
016.txt |
WA |
114 ms |
15616 KB |
017.txt |
WA |
153 ms |
15616 KB |
018.txt |
WA |
136 ms |
15616 KB |
019.txt |
WA |
159 ms |
15616 KB |
020.txt |
WA |
158 ms |
15616 KB |
021.txt |
WA |
162 ms |
15616 KB |
022.txt |
WA |
151 ms |
15616 KB |
023.txt |
WA |
140 ms |
15616 KB |
024.txt |
WA |
149 ms |
15616 KB |
025.txt |
WA |
146 ms |
15616 KB |
026.txt |
WA |
155 ms |
15616 KB |
027.txt |
WA |
149 ms |
15616 KB |
028.txt |
WA |
127 ms |
15616 KB |
029.txt |
WA |
145 ms |
15616 KB |
030.txt |
WA |
119 ms |
15616 KB |
031.txt |
WA |
127 ms |
15616 KB |
032.txt |
WA |
136 ms |
15616 KB |
033.txt |
WA |
108 ms |
15616 KB |
034.txt |
WA |
115 ms |
15616 KB |
035.txt |
WA |
149 ms |
15616 KB |
036.txt |
WA |
125 ms |
15616 KB |
037.txt |
WA |
130 ms |
15616 KB |
038.txt |
WA |
116 ms |
15616 KB |
039.txt |
AC |
149 ms |
15616 KB |
040.txt |
AC |
109 ms |
15616 KB |
041.txt |
WA |
127 ms |
15616 KB |
042.txt |
WA |
126 ms |
15616 KB |
043.txt |
WA |
119 ms |
15616 KB |
044.txt |
AC |
150 ms |
23808 KB |
045.txt |
WA |
154 ms |
19712 KB |
046.txt |
WA |
155 ms |
20864 KB |
047.txt |
WA |
157 ms |
20096 KB |
048.txt |
WA |
158 ms |
22528 KB |
049.txt |
WA |
4 ms |
8448 KB |
example0.txt |
AC |
4 ms |
8448 KB |
example1.txt |
AC |
4 ms |
8448 KB |
example2.txt |
WA |
4 ms |
8448 KB |
example3.txt |
AC |
4 ms |
8448 KB |