Submission #783863

#TimeUsernameProblemLanguageResultExecution timeMemory
783863tvladm2009Cats or Dogs (JOI18_catdog)C++17
0 / 100
1 ms2644 KiB
#include <bits/stdc++.h> #include "catdog.h" using namespace std; const int N = (int) 1e5 + 7; const int INF = (int) 1e9; int n; int par[N]; int dp1[N]; int dp2[N]; int dp3[N]; int dp4[N]; int cost1[N]; int cost2[N]; vector<int> g[N]; void build(int a, int p = 0) { par[a] = p; vector<int> kids; for (auto &b : g[a]) { if (b != p) { kids.push_back(b); build(b, a); } } g[a] = kids; } void dfs(int a) { dp1[a] = 0; dp2[a] = 0; for (auto &b : g[a]) { dfs(b); dp1[a] += dp3[b]; dp2[a] += dp4[b]; } dp1[a] += cost1[a]; dp2[a] += cost2[a]; dp3[a] = min(dp1[a], dp2[a] + 1); dp4[a] = min(dp2[a], dp1[a] + 1); } void initialize(int nn, vector<int> a, vector<int> b) { n = nn; for (int i = 1; i < n; i++) { g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } build(1); } void change(int v, int c1, int c2) { cost1[v] = c1; cost2[v] = c2; dfs(1); } int cat(int v) { change(v, INF, 0); return min(dp1[1], dp2[1]); } int dog(int v) { change(v, 0, INF); return min(dp1[1], dp2[1]); } int neighbor(int v) { change(v, 0, 0); return min(dp1[1], dp2[1]); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...