Submission #1825705
Source Code Expand
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn = 2e5; int n, son[maxn][2]; long long len[maxn], lim; typedef pair<long long, long long> Pair; vector<Pair> set[maxn]; void Combine(vector<Pair> &s, int u, int v) { s.clear(); if (set[u].empty()) { return; } // u -> v int j = 0; for (int i = 0; i < set[u].size(); ++i) { if (set[u][i].second + len[u] + set[v][j].first + len[v] > lim) { continue; } while (j + 1 < set[v].size() && set[u][i].second + len[u] + set[v][j + 1].first + len[v] <= lim) { ++j; } s.push_back({set[u][i].first + len[u], set[v][j].second + len[v]}); } int mid = s.size(); // v -> u j = set[v].size() - 1; for (int i = set[u].size() - 1; i >= 0; --i) { if (set[v][j].second + len[v] + set[u][i].first + len[u] > lim) { continue; } while (j - 1 >= 0 && set[v][j - 1].second + len[v] + set[u][i].first + len[u] <= lim) { --j; } s.push_back({set[v][j].first + len[v], set[u][i].second + len[u]}); } reverse(s.begin() + mid, s.end()); inplace_merge(s.begin(), s.begin() + mid, s.end()); mid = 0; for (int i = 0; i < s.size(); ++i) { if (i == 0 || s[i].second < s[i - 1].second) { s[mid++] = s[i]; } } s.resize(mid); } bool Check(void) { for (int i = n - 1; i >= 0; --i) { if (son[i][0] == -1) { set[i] = {{0, 0}}; } else { int &u = son[i][0], &v = son[i][1]; if (set[u].size() > set[v].size()) { swap(u, v); } Combine(set[i], u, v); } } return set[0].size() > 0; } int main(void) { scanf("%d", &n); memset(son, -1, sizeof son); for (int i = 1; i < n; ++i) { int par; scanf("%d%lld", &par, len + i); --par; swap(son[par][0], son[par][1]); son[par][0] = i; } long long low = 0, high = (long long)maxn * maxn; while (low < high) { lim = low + high >> 1; if (Check()) { high = lim; } else { low = lim + 1; } } printf("%lld\n", low); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Shik and Travel |
User | orbitingflea |
Language | C++14 (GCC 5.4.1) |
Score | 1400 |
Code Size | 2185 Byte |
Status | AC |
Exec Time | 394 ms |
Memory | 30980 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:74:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ ./Main.cpp:78:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%lld", &par, len + i); ^
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1400 / 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 | 8 ms | 8064 KB |
001.txt | AC | 8 ms | 8064 KB |
002.txt | AC | 8 ms | 8064 KB |
003.txt | AC | 8 ms | 8064 KB |
004.txt | AC | 8 ms | 8064 KB |
005.txt | AC | 8 ms | 8064 KB |
006.txt | AC | 8 ms | 8064 KB |
007.txt | AC | 8 ms | 8064 KB |
008.txt | AC | 8 ms | 8064 KB |
009.txt | AC | 8 ms | 8064 KB |
010.txt | AC | 272 ms | 13952 KB |
011.txt | AC | 288 ms | 16000 KB |
012.txt | AC | 277 ms | 13952 KB |
013.txt | AC | 279 ms | 13952 KB |
014.txt | AC | 274 ms | 13952 KB |
015.txt | AC | 288 ms | 13952 KB |
016.txt | AC | 277 ms | 13952 KB |
017.txt | AC | 274 ms | 13952 KB |
018.txt | AC | 277 ms | 13952 KB |
019.txt | AC | 274 ms | 13824 KB |
020.txt | AC | 269 ms | 13952 KB |
021.txt | AC | 279 ms | 13824 KB |
022.txt | AC | 278 ms | 13952 KB |
023.txt | AC | 284 ms | 13952 KB |
024.txt | AC | 279 ms | 13952 KB |
025.txt | AC | 278 ms | 13824 KB |
026.txt | AC | 245 ms | 13312 KB |
027.txt | AC | 268 ms | 13952 KB |
028.txt | AC | 246 ms | 13312 KB |
029.txt | AC | 246 ms | 13312 KB |
030.txt | AC | 272 ms | 14848 KB |
031.txt | AC | 310 ms | 14848 KB |
032.txt | AC | 394 ms | 14848 KB |
033.txt | AC | 268 ms | 14848 KB |
034.txt | AC | 273 ms | 14848 KB |
035.txt | AC | 296 ms | 14848 KB |
036.txt | AC | 261 ms | 14720 KB |
037.txt | AC | 259 ms | 14080 KB |
038.txt | AC | 267 ms | 14848 KB |
039.txt | AC | 219 ms | 13056 KB |
040.txt | AC | 266 ms | 29436 KB |
041.txt | AC | 299 ms | 30716 KB |
042.txt | AC | 299 ms | 30844 KB |
043.txt | AC | 297 ms | 30980 KB |
044.txt | AC | 172 ms | 13056 KB |
045.txt | AC | 209 ms | 13056 KB |
046.txt | AC | 203 ms | 13056 KB |
047.txt | AC | 203 ms | 13056 KB |
048.txt | AC | 203 ms | 13056 KB |
049.txt | AC | 4 ms | 7936 KB |
example0.txt | AC | 4 ms | 7936 KB |
example1.txt | AC | 4 ms | 7936 KB |
example2.txt | AC | 4 ms | 7936 KB |
example3.txt | AC | 4 ms | 7936 KB |