Submission #894063

# Submission time Handle Problem Language Result Execution time Memory
894063 2023-12-27T23:14:12 Z vovik Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 7876 KB
#include <bits/stdc++.h>

void initialize(int N, std::vector<int> A, std::vector<int> B);

int cat(int v);

int dog(int v);

int neighbor(int v);

std::vector<int> g[100005];
int dp[100005][2];
int tp[100005];

void initialize(int N, std::vector<int> A, std::vector<int> B) {
    for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
}

void dfs(int v, int p = -1) {
    int s0 = 0, s1 = 0;
    for (auto u: g[v]) {
        if (u == p) continue;
        dfs(u, v), s0 += dp[u][0], s1 += dp[u][1];
    }
    if (!tp[v]) dp[v][0] = std::min(s0, s1 + 1), dp[v][1] = std::min(s1, s0 + 1);
    else if (tp[v] == 1) dp[v][1] = (dp[v][0] = s0) + 1;
    else dp[v][0] = (dp[v][1] = s1) + 1;
}

int cat(int v) {
    return tp[v] = 1, dfs(1), std::min(dp[1][0], dp[1][1]);
}
int dog(int v) { return tp[v] = 2, dfs(1), std::min(dp[1][0], dp[1][1]); }
int neighbor(int v) { return tp[v] = 0, dfs(1), std::min(dp[1][0], dp[1][1]); }


#ifdef __APPLE__
#include <cstdio>
#include <cassert>
#include <vector>
#include <cstdlib>

int readInt() {
    int i;
    if (scanf("%d", &i) != 1) {
        fprintf(stderr, "Error while reading input\n");
        exit(1);
    }
    return i;
}

int main() {
    int N = readInt();

    std::vector<int> A(N - 1), B(N - 1);
    for (int i = 0; i < N - 1; i++) {
        A[i] = readInt();
        B[i] = readInt();
    }
    int Q;
    assert(scanf("%d",&Q)==1);
    std::vector<int> T(Q), V(Q);
    for (int i = 0; i < Q; i++) {
        T[i] = readInt();
        V[i] = readInt();
    }

    initialize(N, A, B);

    std::vector<int> res(Q);
    for (int j = 0; j < Q; j++) {
        if (T[j] == 1) res[j] = cat(V[j]);
        else if (T[j] == 2) res[j] = dog(V[j]);
        else res[j] = neighbor(V[j]);
    }
    for (int j = 0; j < Q; j++)
        printf("%d\n", res[j]);
    return 0;
}

#endif

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < A.size(); ++i) g[A[i]].push_back(B[i]), g[B[i]].push_back(A[i]);
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3928 KB Output is correct
2 Correct 1 ms 3672 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3780 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 2 ms 3780 KB Output is correct
10 Correct 1 ms 3672 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3928 KB Output is correct
2 Correct 1 ms 3672 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3780 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 2 ms 3780 KB Output is correct
10 Correct 1 ms 3672 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 6 ms 3672 KB Output is correct
18 Correct 7 ms 3676 KB Output is correct
19 Correct 4 ms 3676 KB Output is correct
20 Correct 2 ms 3756 KB Output is correct
21 Correct 2 ms 3676 KB Output is correct
22 Correct 2 ms 3672 KB Output is correct
23 Correct 8 ms 3676 KB Output is correct
24 Correct 7 ms 3676 KB Output is correct
25 Correct 4 ms 3676 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 2 ms 3676 KB Output is correct
28 Correct 3 ms 3676 KB Output is correct
29 Correct 10 ms 3676 KB Output is correct
30 Correct 2 ms 3676 KB Output is correct
31 Correct 2 ms 3676 KB Output is correct
32 Correct 3 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3928 KB Output is correct
2 Correct 1 ms 3672 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3780 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 2 ms 3780 KB Output is correct
10 Correct 1 ms 3672 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 6 ms 3672 KB Output is correct
18 Correct 7 ms 3676 KB Output is correct
19 Correct 4 ms 3676 KB Output is correct
20 Correct 2 ms 3756 KB Output is correct
21 Correct 2 ms 3676 KB Output is correct
22 Correct 2 ms 3672 KB Output is correct
23 Correct 8 ms 3676 KB Output is correct
24 Correct 7 ms 3676 KB Output is correct
25 Correct 4 ms 3676 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 2 ms 3676 KB Output is correct
28 Correct 3 ms 3676 KB Output is correct
29 Correct 10 ms 3676 KB Output is correct
30 Correct 2 ms 3676 KB Output is correct
31 Correct 2 ms 3676 KB Output is correct
32 Correct 3 ms 3676 KB Output is correct
33 Execution timed out 3022 ms 7876 KB Time limit exceeded
34 Halted 0 ms 0 KB -