Submission #711525

# Submission time Handle Problem Language Result Execution time Memory
711525 2023-03-17T07:43:18 Z Cyanmond Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 6652 KB
#include "catdog.h"
#include <bits/stdc++.h>

constexpr int inf = 1 << 30;

int N;
std::vector<int> A, B;
std::vector<std::vector<int>> Tree;
std::vector<int> status;
void initialize(int n, std::vector<int> a, std::vector<int> b) {
    N = n;
    A = a;
    B = b;
    Tree.resize(N);
    for (int i = 0; i < N - 1; ++i) {
        --A[i]; --B[i];
        Tree[A[i]].push_back(B[i]);
        Tree[B[i]].push_back(A[i]);
    }
    status.resize(N);
}

int calc() {
    std::vector<std::array<int, 3>> dp(N);
    for (auto &ar : dp) std::fill(ar.begin(), ar.end(), inf);
    auto dfs = [&](auto &&self, const int v, const int p) -> void {
        dp[v][0] = dp[v][1] = dp[v][2] = 0;
        for (const int t : Tree[v]) {
            if (t == p) continue;
            self(self, t, v);
            dp[v][0] = std::min({dp[v][0] + dp[t][0], dp[v][0] + dp[t][1] + 1, dp[v][0] + dp[t][2] + 1});
            dp[v][1] = std::min({dp[v][1] + dp[t][0], dp[v][1] + dp[t][1], dp[v][1] + dp[t][2] + 1});
            dp[v][2] = std::min({dp[v][2] + dp[t][0], dp[v][2] + dp[t][1] + 1, dp[v][2] + dp[t][2]});
        }
        if (status[v] == 1) {
            ++dp[v][0];
            dp[v][2] = inf;
        }
        if (status[v] == 2) {
            ++dp[v][0];
            dp[v][1] = inf;
        }
    };
    dfs(dfs, 0, -1);
    return *std::min_element(dp[0].begin(), dp[0].end());
}

int cat(int v) {
    --v;
    status[v] = 1;
    return calc();
}

int dog(int v) {
    --v;
    status[v] = 2;
    return calc();
}

int neighbor(int v) {
    --v;
    status[v] = 0;
    return calc();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 17 ms 384 KB Output is correct
18 Correct 18 ms 284 KB Output is correct
19 Correct 10 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 3 ms 212 KB Output is correct
22 Correct 3 ms 212 KB Output is correct
23 Correct 21 ms 340 KB Output is correct
24 Correct 16 ms 340 KB Output is correct
25 Correct 9 ms 340 KB Output is correct
26 Correct 5 ms 300 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 5 ms 432 KB Output is correct
29 Correct 27 ms 340 KB Output is correct
30 Correct 3 ms 212 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 17 ms 384 KB Output is correct
18 Correct 18 ms 284 KB Output is correct
19 Correct 10 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 3 ms 212 KB Output is correct
22 Correct 3 ms 212 KB Output is correct
23 Correct 21 ms 340 KB Output is correct
24 Correct 16 ms 340 KB Output is correct
25 Correct 9 ms 340 KB Output is correct
26 Correct 5 ms 300 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 5 ms 432 KB Output is correct
29 Correct 27 ms 340 KB Output is correct
30 Correct 3 ms 212 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 5 ms 340 KB Output is correct
33 Execution timed out 3033 ms 6652 KB Time limit exceeded
34 Halted 0 ms 0 KB -