Submission #1984772


Source Code Expand

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>
#include <functional>
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;
                        bool ng = true;
                        for (int i = -1; i < (int) child.size(); i ++) {
                                tmp.clear();
                                if (i == -1) {
                                        tmp = child;
                                        if (check(tmp, x)) {
                                                dp[u] = 0;
                                                ng = false;
                                                break;
                                        }
                                } else {
                                        for (int j = 0; j < child.size(); j ++) {
                                                if (i != j) {
                                                        tmp.push_back(child[j]);
                                                }
                                        }
                                        tmp.pop_back();
                                        if (check(tmp, x)) {
                                                dp[u] = child[i];
                                                ng = false;
                                                break;
                                        }
                                }
                        }
                        if (ng) { 
                                return false;
                        }
                } else {
                        vector<int> tmp;
                        bool ng = true;
                        for (int i = 0; i < child.size(); i ++) {
                                tmp.clear();
                                for (int j = 0; j < child.size(); j ++) {
                                        if (i != j) {
                                                tmp.push_back(child[j]);
                                        }
                                }
                                if (check(tmp, x)) {
                                        dp[u] = child[i];
                                        ng = false;
                                        break;
                                }
                        }
                        if (ng) { 
                                return false;
                        }
                }
                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;
        int prevlb = -1, prevub = -1;
        while (ub - lb > 0) {
                int mid = (lb + ub) / 2;
                if (ok(mid, n, g)) {
                        ub = mid;
                } else {
                        lb = mid;
                }
                if (lb == prevlb && ub == prevub) break;
                prevlb = lb, prevub = ub;
        }
        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 0
Code Size 5311 Byte
Status TLE
Exec Time 2104 ms
Memory 19456 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:111:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &n);
                        ^
./Main.cpp:115: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 0 / 900
Status
AC × 3
AC × 71
TLE × 4
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 192 ms 6144 KB
02.txt AC 228 ms 6144 KB
03.txt AC 207 ms 6144 KB
04.txt AC 225 ms 6144 KB
05.txt AC 235 ms 17280 KB
06.txt AC 282 ms 19456 KB
07.txt AC 239 ms 10368 KB
08.txt AC 214 ms 13312 KB
09.txt AC 252 ms 11384 KB
10.txt AC 253 ms 13952 KB
11.txt AC 869 ms 6864 KB
12.txt AC 838 ms 6872 KB
13.txt AC 931 ms 6936 KB
14.txt AC 870 ms 8948 KB
15.txt AC 196 ms 6264 KB
16.txt AC 232 ms 6272 KB
17.txt AC 214 ms 6144 KB
18.txt AC 194 ms 6144 KB
19.txt AC 177 ms 6272 KB
20.txt AC 203 ms 6400 KB
21.txt AC 172 ms 6528 KB
22.txt AC 138 ms 7672 KB
23.txt AC 254 ms 17152 KB
24.txt AC 226 ms 16256 KB
25.txt AC 209 ms 6272 KB
26.txt AC 225 ms 6272 KB
27.txt AC 217 ms 6264 KB
28.txt AC 193 ms 6144 KB
29.txt AC 211 ms 8184 KB
30.txt AC 238 ms 6272 KB
31.txt AC 217 ms 6656 KB
32.txt AC 190 ms 7936 KB
33.txt AC 328 ms 16896 KB
34.txt AC 312 ms 15224 KB
35.txt AC 257 ms 6264 KB
36.txt AC 188 ms 6272 KB
37.txt AC 214 ms 6264 KB
38.txt AC 187 ms 6144 KB
39.txt AC 179 ms 6264 KB
40.txt AC 219 ms 6272 KB
41.txt AC 203 ms 6528 KB
42.txt AC 210 ms 8192 KB
43.txt AC 235 ms 12416 KB
44.txt AC 219 ms 12032 KB
45.txt AC 209 ms 6264 KB
46.txt AC 228 ms 6264 KB
47.txt AC 215 ms 6272 KB
48.txt AC 199 ms 6144 KB
49.txt AC 193 ms 6272 KB
50.txt AC 145 ms 6400 KB
51.txt AC 191 ms 6656 KB
52.txt AC 193 ms 7296 KB
53.txt AC 227 ms 10752 KB
54.txt AC 209 ms 14336 KB
55.txt TLE 2104 ms 7448 KB
56.txt TLE 2104 ms 7840 KB
57.txt TLE 2104 ms 7848 KB
58.txt AC 92 ms 7840 KB
59.txt AC 242 ms 10336 KB
60.txt AC 242 ms 9216 KB
61.txt AC 273 ms 9688 KB
62.txt AC 245 ms 10292 KB
63.txt AC 252 ms 10972 KB
64.txt AC 175 ms 13244 KB
65.txt TLE 2104 ms 12640 KB
66.txt AC 275 ms 12156 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