Submission #1090041

#TimeUsernameProblemLanguageResultExecution timeMemory
1090041VMaksimoski008Cats or Dogs (JOI18_catdog)C++17
38 / 100
3036 ms7368 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...