Submission #743931

#TimeUsernameProblemLanguageResultExecution timeMemory
743931myrcellaCats or Dogs (JOI18_catdog)C++17
38 / 100
21 ms4224 KiB
//by szh #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define debug(x) cerr<<#x<<"="<<x<<endl #define pq priority_queue #define inf 0x3f #define rep(i,a,b) for (int i=a;i<(b);i++) #define MP make_pair #define SZ(x) (int(x.size())) #define ll long long #define mod 1000000007 #define ALL(x) x.begin(),x.end() void inc(int &a,int b) {a=(a+b)%mod;} void dec(int &a,int b) {a=(a-b+mod)%mod;} int lowbit(int x) {return x&(-x);} ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;} #include "catdog.h" int x; const int maxn = 1010; int dp[3][maxn]; int state[maxn]; vector <int> edge[maxn]; void initialize(int N, std::vector<int> A, std::vector<int> B) { rep(i,0,N-1) edge[A[i]].pb(B[i]), edge[B[i]].pb(A[i]); } void dfs(int u,int fa) { if (state[u]!=0) { int &ans = dp[state[u]][u]; ans = 0; for (int v:edge[u]) { if (fa==v) continue; dfs(v,u); ans += min(dp[0][v],min(dp[state[u]][v],dp[3-state[u]][v]+1)); ans = min(ans,mod); } } else { rep(i,0,3) dp[i][u] = 0; for (int v:edge[u]) { if (fa==v) continue; dfs(v,u); dp[0][u] += min(dp[0][v],min(dp[1][v],dp[2][v])+1); dp[1][u] += min(dp[0][v],min(dp[1][v],dp[2][v]+1)); dp[2][u] += min(dp[0][v],min(dp[1][v]+1,dp[2][v])); rep(j,0,3) dp[j][u] = min(dp[j][u],mod); } } return; } int solve() { memset(dp,inf,sizeof(dp)); dfs(1,-1); return min(dp[0][1],min(dp[1][1],dp[2][1])); } int cat(int v) { state[v] = 1; return solve(); } int dog(int v) { state[v] = 2; return solve(); } int neighbor(int v) { state[v] = 0; return solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...