Submission #1090041

# Submission time Handle Problem Language Result Execution time Memory
1090041 2024-09-17T16:00:16 Z VMaksimoski008 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 7368 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;

int n, dp[maxn][2];
vector<int> graph[maxn], val(maxn);

void reset() {
    for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 1e6;
}

int query() {
    if(val[1] == 1) return dp[1][1];
    if(val[1] == 2) return dp[1][2];
    return min(dp[1][1], dp[1][2]);
}

void dfs(int u, int p) {
    if(val[u] != 1) dp[u][2] = 0;
    if(val[u] != 2) dp[u][1] = 0;

    for(int &v : graph[u]) {
        if(v == p) continue;
        dfs(v, u);
        if(val[u] != 1) dp[u][2] += min(dp[v][2], dp[v][1] + 1);
        if(val[u] != 2) dp[u][1] += min(dp[v][1], dp[v][2] + 1);
    }
}

void initialize(int N, vector<int> A, vector<int> B) {
    n = N;
    for(int i=0; i<n-1; i++) {
        graph[A[i]].push_back(B[i]);
        graph[B[i]].push_back(A[i]);
    }
}

int cat(int v) {
    val[v] = 1;
    reset(); dfs(1, 1);
    return query();
}

int dog(int v) {
    val[v] = 2;
    reset(); dfs(1, 1);
    return query();
}

int neighbor(int v) {
    val[v] = 0;
    reset(); dfs(1, 1);
    return query();
}

Compilation message

catdog.cpp: In function 'void reset()':
catdog.cpp:11:58: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   11 |     for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 1e6;
      |                                                   ~~~~~~~^
catdog.cpp: In function 'int query()':
catdog.cpp:16:35: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   16 |     if(val[1] == 2) return dp[1][2];
      |                            ~~~~~~~^
catdog.cpp: In function 'void dfs(int, int)':
catdog.cpp:21:28: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   21 |     if(val[u] != 1) dp[u][2] = 0;
      |                     ~~~~~~~^
catdog.cpp:27:48: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   27 |         if(val[u] != 1) dp[u][2] += min(dp[v][2], dp[v][1] + 1);
      |                                         ~~~~~~~^
catdog.cpp:27:34: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   27 |         if(val[u] != 1) dp[u][2] += min(dp[v][2], dp[v][1] + 1);
      |                         ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:27:34: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
catdog.cpp:28:58: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   28 |         if(val[u] != 2) dp[u][1] += min(dp[v][1], dp[v][2] + 1);
      |                                                   ~~~~~~~^
catdog.cpp: In function 'int cat(int)':
catdog.cpp:11:58: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   11 |     for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 1e6;
      |                                                   ~~~~~~~^
catdog.cpp:16:35: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   16 |     if(val[1] == 2) return dp[1][2];
      |                            ~~~~~~~^
catdog.cpp: In function 'int dog(int)':
catdog.cpp:11:58: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   11 |     for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 1e6;
      |                                                   ~~~~~~~^
catdog.cpp:16:35: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   16 |     if(val[1] == 2) return dp[1][2];
      |                            ~~~~~~~^
catdog.cpp: In function 'int neighbor(int)':
catdog.cpp:11:58: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   11 |     for(int i=1; i<=n; i++) dp[i][0] = dp[i][1] = dp[i][2] = 1e6;
      |                                                   ~~~~~~~^
catdog.cpp:16:35: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
   16 |     if(val[1] == 2) return dp[1][2];
      |                            ~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3192 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 1 ms 3196 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 1 ms 3164 KB Output is correct
13 Correct 1 ms 3164 KB Output is correct
14 Correct 1 ms 3164 KB Output is correct
15 Correct 1 ms 3164 KB Output is correct
16 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3192 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 1 ms 3196 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 1 ms 3164 KB Output is correct
13 Correct 1 ms 3164 KB Output is correct
14 Correct 1 ms 3164 KB Output is correct
15 Correct 1 ms 3164 KB Output is correct
16 Correct 1 ms 3164 KB Output is correct
17 Correct 6 ms 3248 KB Output is correct
18 Correct 8 ms 3104 KB Output is correct
19 Correct 5 ms 3204 KB Output is correct
20 Correct 2 ms 3160 KB Output is correct
21 Correct 2 ms 3160 KB Output is correct
22 Correct 2 ms 3164 KB Output is correct
23 Correct 9 ms 3256 KB Output is correct
24 Correct 8 ms 3160 KB Output is correct
25 Correct 5 ms 3164 KB Output is correct
26 Correct 3 ms 3164 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 3 ms 3164 KB Output is correct
29 Correct 11 ms 3164 KB Output is correct
30 Correct 3 ms 3164 KB Output is correct
31 Correct 2 ms 3164 KB Output is correct
32 Correct 4 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3192 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 1 ms 3196 KB Output is correct
11 Correct 1 ms 3164 KB Output is correct
12 Correct 1 ms 3164 KB Output is correct
13 Correct 1 ms 3164 KB Output is correct
14 Correct 1 ms 3164 KB Output is correct
15 Correct 1 ms 3164 KB Output is correct
16 Correct 1 ms 3164 KB Output is correct
17 Correct 6 ms 3248 KB Output is correct
18 Correct 8 ms 3104 KB Output is correct
19 Correct 5 ms 3204 KB Output is correct
20 Correct 2 ms 3160 KB Output is correct
21 Correct 2 ms 3160 KB Output is correct
22 Correct 2 ms 3164 KB Output is correct
23 Correct 9 ms 3256 KB Output is correct
24 Correct 8 ms 3160 KB Output is correct
25 Correct 5 ms 3164 KB Output is correct
26 Correct 3 ms 3164 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 3 ms 3164 KB Output is correct
29 Correct 11 ms 3164 KB Output is correct
30 Correct 3 ms 3164 KB Output is correct
31 Correct 2 ms 3164 KB Output is correct
32 Correct 4 ms 3164 KB Output is correct
33 Execution timed out 3036 ms 7368 KB Time limit exceeded
34 Halted 0 ms 0 KB -