Submission #532248

#TimeUsernameProblemLanguageResultExecution timeMemory
532248Alex_tz307Cats or Dogs (JOI18_catdog)C++17
38 / 100
3015 ms7068 KiB
#include <bits/stdc++.h> #include "catdog.h" #define INF 0x3f3f3f3f using namespace std; const int kN = 1e5; vector<int> g[1 + kN]; short col[1 + kN]; int dp[1 + kN][2]; void minSelf(int &x, int y) { if (y < x) { x = y; } } void dfs(int u, int par) { dp[u][0] = dp[u][1] = 0; for (int v : g[u]) { if (v != par) { dfs(v, u); for (int i = 0; i < 2; ++i) { dp[u][i] += min(dp[v][i], dp[v][i ^ 1] + 1); } } } if (col[u] < 2) { dp[u][col[u]] = INF; } } void initialize(int N, vector<int> A, vector<int> B) { for (int i = 0; i < N - 1; ++i) { g[A[i]].emplace_back(B[i]); g[B[i]].emplace_back(A[i]); } for (int v = 1; v <= N; ++v) { col[v] = 2; } } int cat(int v) { col[v] = 0; dfs(1, 0); return min(dp[1][0], dp[1][1]); } int dog(int v) { col[v] = 1; dfs(1, 0); return min(dp[1][0], dp[1][1]); } int neighbor(int v) { col[v] = 2; dfs(1, 0); return min(dp[1][0], dp[1][1]); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...