Submission #388129

#TimeUsernameProblemLanguageResultExecution timeMemory
388129mohamedsobhi777Cats or Dogs (JOI18_catdog)C++14
38 / 100
40 ms3212 KiB
#include <bits/stdc++.h> #include "catdog.h" using namespace std; int x; const int MX = 1000 + 7; int val[MX], par[MX]; vector<int> adj[MX]; int dp[MX][2]; void pull(int u) { dp[u][0] = dp[u][1] = 0 ; if (val[u] != 2) dp[u][!val[u]] = MX; for(auto v : adj[u]){ if(v == par[u])continue; for(int j = 0 ;j <2; ++ j){ dp[u][j] += min(dp[v][j] , dp[v][!j] + 1) ; } } } void dfz(int x, int p) { if (val[x] != 2) dp[x][!val[x]] = MX; par[x] = p; for (auto u : adj[x]) { if (u == p) continue; dfz(u, x); } pull(x) ; } void initialize(int N, std::vector<int> A, std::vector<int> B) { x = N; for (int i = 0; i + 1 < N; ++i) { adj[A[i]].push_back(B[i]); adj[B[i]].push_back(A[i]); } fill(val + 1, val + N + 1, 2); dfz(1, 1); } void touch(int x) { while (x != 1) { pull(x); x = par[x]; } pull(1) ; } int solve() { return min(dp[1][0], dp[1][1]); } int cat(int v) { val[v] = 0; touch(v); return solve(); } int dog(int v) { val[v] = 1; touch(v); return solve(); } int neighbor(int v) { val[v] = 2; touch(v); return solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...