Submission #1984831
Source Code Expand
#include <cstdio> #include <algorithm> #include <vector> #include <cassert> #include <functional> #include <iostream> using namespace std; bool check(const vector<int> &tmp, int x) { assert((int) tmp.size() % 2 == 0); int bg = 0, ed = tmp.size() - 1; bool res = true; while (bg < ed) { if (tmp[bg] + tmp[ed] > x) { res = false; } bg ++, ed --; } return res; } bool ok(int x, int n, const vector<vector<int>> &g) { vector<int> dp(n); function<bool (int, int)> dfs = [&](int u, int prev) { vector<int> child; for (auto v : g[u]) if (v != prev) { if (!dfs(v, u)) return false; if (dp[v] + 1 > x) return false; child.push_back(dp[v] + 1); } if (child.empty()) { dp[u] = 0; return true; } sort(child.begin(), child.end()); int deg = g[u].size(); for (int i = 0; i < child.size(); i ++) { if (child[i] > x) { return false; } } if (u == 0) { if (deg & 1) { child.pop_back(); if (!check(child, x)) { return false; } } else { if (!check(child, x)) { return false; } } return true; } if (deg & 1) { vector<int> tmp; tmp = child; if (check(tmp, x)) { dp[u] = 0; } else { int lb = 0, ub = child.size(); while (ub - lb > 0) { tmp.clear(); int mid = (lb + ub) / 2; for (int i = 0; i < child.size(); i ++) { if (i != mid) { tmp.push_back(child[i]); } } tmp.pop_back(); if (check(tmp, x)) { ub = mid; } else { lb = mid + 1; } } if (ub == child.size()) { return false; } dp[u] = child[ub]; } } else { vector<int> tmp; int lb = 0, ub = child.size(); while (ub - lb > 0) { tmp.clear(); int mid = (lb + ub) / 2; for (int i = 0; i < child.size(); i ++) { if (i != mid) { tmp.push_back(child[i]); } } if (check(tmp, x)) { ub = mid; } else { lb = mid + 1; } } if (ub == child.size()) { return false; } dp[u] = child[ub]; } return true; }; bool res = dfs(0, -1); return res; } int main() { int n; scanf("%d", &n); vector<vector<int>> g(n); for (int i = 0; i < n - 1; i ++) { int a, b; scanf("%d%d", &a, &b); a --, b --; g[a].push_back(b); g[b].push_back(a); } int a = 0; for (int i = 0; i < n; i ++) { if ((int) g[i].size() & 1) a ++; } assert(a % 2 == 0); a /= 2; int lb = 0, ub = n; while (ub - lb > 1) { int mid = (lb + ub) / 2; if (ok(mid, n, g)) { ub = mid; } else { lb = mid; } } printf("%d %d\n", a, ub); return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Christmas Tree |
User | KokiYmgch |
Language | C++14 (GCC 5.4.1) |
Score | 900 |
Code Size | 5189 Byte |
Status | AC |
Exec Time | 313 ms |
Memory | 22016 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:112:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ ./Main.cpp:116:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d", &a, &b); ^
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 900 / 900 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | s1.txt, s2.txt, s3.txt |
All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, s1.txt, s2.txt, s3.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 211 ms | 6264 KB |
02.txt | AC | 208 ms | 6264 KB |
03.txt | AC | 212 ms | 6144 KB |
04.txt | AC | 226 ms | 6272 KB |
05.txt | AC | 250 ms | 19584 KB |
06.txt | AC | 281 ms | 22016 KB |
07.txt | AC | 225 ms | 11264 KB |
08.txt | AC | 219 ms | 14664 KB |
09.txt | AC | 250 ms | 12288 KB |
10.txt | AC | 224 ms | 17528 KB |
11.txt | AC | 129 ms | 6896 KB |
12.txt | AC | 115 ms | 6880 KB |
13.txt | AC | 128 ms | 6952 KB |
14.txt | AC | 125 ms | 6972 KB |
15.txt | AC | 213 ms | 6272 KB |
16.txt | AC | 218 ms | 6272 KB |
17.txt | AC | 221 ms | 6272 KB |
18.txt | AC | 203 ms | 6272 KB |
19.txt | AC | 198 ms | 6272 KB |
20.txt | AC | 198 ms | 6400 KB |
21.txt | AC | 179 ms | 6656 KB |
22.txt | AC | 132 ms | 7936 KB |
23.txt | AC | 257 ms | 19328 KB |
24.txt | AC | 252 ms | 18304 KB |
25.txt | AC | 200 ms | 6272 KB |
26.txt | AC | 223 ms | 6272 KB |
27.txt | AC | 213 ms | 6272 KB |
28.txt | AC | 192 ms | 6144 KB |
29.txt | AC | 204 ms | 6272 KB |
30.txt | AC | 216 ms | 6400 KB |
31.txt | AC | 189 ms | 6784 KB |
32.txt | AC | 158 ms | 8320 KB |
33.txt | AC | 286 ms | 19072 KB |
34.txt | AC | 260 ms | 17024 KB |
35.txt | AC | 234 ms | 6272 KB |
36.txt | AC | 218 ms | 6272 KB |
37.txt | AC | 218 ms | 6272 KB |
38.txt | AC | 229 ms | 6144 KB |
39.txt | AC | 223 ms | 6272 KB |
40.txt | AC | 267 ms | 6400 KB |
41.txt | AC | 223 ms | 6656 KB |
42.txt | AC | 215 ms | 8576 KB |
43.txt | AC | 247 ms | 13696 KB |
44.txt | AC | 313 ms | 13312 KB |
45.txt | AC | 215 ms | 6272 KB |
46.txt | AC | 229 ms | 6272 KB |
47.txt | AC | 222 ms | 6272 KB |
48.txt | AC | 194 ms | 6144 KB |
49.txt | AC | 202 ms | 6272 KB |
50.txt | AC | 161 ms | 6400 KB |
51.txt | AC | 181 ms | 6784 KB |
52.txt | AC | 197 ms | 7552 KB |
53.txt | AC | 251 ms | 11776 KB |
54.txt | AC | 233 ms | 16000 KB |
55.txt | AC | 93 ms | 7448 KB |
56.txt | AC | 175 ms | 7856 KB |
57.txt | AC | 169 ms | 7856 KB |
58.txt | AC | 169 ms | 7856 KB |
59.txt | AC | 250 ms | 11096 KB |
60.txt | AC | 252 ms | 9768 KB |
61.txt | AC | 252 ms | 10312 KB |
62.txt | AC | 254 ms | 11032 KB |
63.txt | AC | 260 ms | 11836 KB |
64.txt | AC | 174 ms | 14524 KB |
65.txt | AC | 166 ms | 13712 KB |
66.txt | AC | 228 ms | 13244 KB |
67.txt | AC | 1 ms | 256 KB |
68.txt | AC | 1 ms | 256 KB |
69.txt | AC | 1 ms | 256 KB |
70.txt | AC | 1 ms | 256 KB |
71.txt | AC | 1 ms | 256 KB |
72.txt | AC | 1 ms | 256 KB |
s1.txt | AC | 1 ms | 256 KB |
s2.txt | AC | 1 ms | 256 KB |
s3.txt | AC | 1 ms | 256 KB |