Submission #1984770


Source Code Expand

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;

//tmpを破壊する
bool check(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);
        //bool res = true;
        //function<void (int, int)> dfs = [&](int u, int prev) {
        function<bool (int, int)> dfs = [&](int u, int prev) {
                vector<int> child;
                for (auto v : g[u]) if (v != prev) {
                        //dfs(v, u);
                        if (!dfs(v, u)) return false;
                        //if (dp[v] + 1 > x) res = false;
                        if (dp[v] + 1 > x) return false;
                        child.push_back(dp[v] + 1);
                }
                if (child.empty()) {
                        dp[u] = 0;
                        //return;
                        return true;
                }
                sort(child.begin(), child.end());
                int deg = g[u].size();
                for (int i = 0; i < child.size(); i ++) {
                        if (child[i] > x) {
                                //res = false;
                                return false;
                        }
                }
                if (u == 0) {
                        if (deg & 1) {
                                child.pop_back();
                                if (!check(child, x)) {
                                        //res = false;
                                        return false;
                                }
                        } else {
                                if (!check(child, x)) {
                                        //res = false;
                                        return false;
                                }
                        }
                        //return;
                        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) { 
                                //res = false;
                                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) { 
                                //res = false;
                                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;
        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 0
Code Size 5706 Byte
Status TLE
Exec Time 2107 ms
Memory 19456 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:123:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &n);
                        ^
./Main.cpp:127: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 214 ms 6144 KB
02.txt AC 229 ms 6144 KB
03.txt AC 207 ms 6144 KB
04.txt AC 219 ms 6144 KB
05.txt AC 263 ms 17280 KB
06.txt AC 254 ms 19456 KB
07.txt AC 237 ms 10368 KB
08.txt AC 202 ms 13304 KB
09.txt AC 246 ms 11264 KB
10.txt AC 248 ms 13952 KB
11.txt AC 570 ms 6864 KB
12.txt AC 556 ms 6872 KB
13.txt AC 651 ms 6936 KB
14.txt AC 581 ms 6960 KB
15.txt AC 208 ms 6264 KB
16.txt AC 214 ms 6272 KB
17.txt AC 214 ms 6144 KB
18.txt AC 197 ms 6144 KB
19.txt AC 191 ms 6272 KB
20.txt AC 189 ms 6400 KB
21.txt AC 172 ms 6528 KB
22.txt AC 137 ms 7672 KB
23.txt AC 245 ms 17152 KB
24.txt AC 237 ms 16256 KB
25.txt AC 213 ms 6272 KB
26.txt AC 223 ms 6272 KB
27.txt AC 208 ms 6272 KB
28.txt AC 185 ms 6144 KB
29.txt AC 197 ms 6144 KB
30.txt AC 195 ms 6272 KB
31.txt AC 180 ms 6656 KB
32.txt AC 152 ms 7936 KB
33.txt AC 232 ms 16896 KB
34.txt AC 236 ms 15104 KB
35.txt AC 225 ms 6264 KB
36.txt AC 208 ms 6272 KB
37.txt AC 217 ms 6264 KB
38.txt AC 204 ms 6144 KB
39.txt AC 193 ms 6144 KB
40.txt AC 212 ms 6272 KB
41.txt AC 199 ms 6528 KB
42.txt AC 201 ms 8192 KB
43.txt AC 226 ms 12416 KB
44.txt AC 231 ms 12032 KB
45.txt AC 209 ms 6264 KB
46.txt AC 225 ms 6264 KB
47.txt AC 216 ms 6272 KB
48.txt AC 201 ms 6144 KB
49.txt AC 193 ms 6272 KB
50.txt AC 155 ms 6400 KB
51.txt AC 185 ms 6656 KB
52.txt AC 188 ms 7296 KB
53.txt AC 217 ms 10752 KB
54.txt AC 204 ms 14336 KB
55.txt TLE 2104 ms 7448 KB
56.txt TLE 2104 ms 7840 KB
57.txt TLE 2107 ms 7840 KB
58.txt AC 96 ms 7840 KB
59.txt AC 245 ms 10408 KB
60.txt AC 242 ms 9220 KB
61.txt AC 243 ms 9688 KB
62.txt AC 224 ms 10292 KB
63.txt AC 249 ms 10972 KB
64.txt AC 172 ms 13244 KB
65.txt TLE 2104 ms 14616 KB
66.txt AC 179 ms 12172 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