Submission #985715

#TimeUsernameProblemLanguageResultExecution timeMemory
985715CookieCats or Dogs (JOI18_catdog)C++14
38 / 100
3100 ms8560 KiB
#include "catdog.h" #include<bits/stdc++.h> #include<fstream> using namespace std; //#pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("avx2,tune=native") //huhu #define sz(a) (int)a.size() #define ALL(v) v.begin(), v.end() #define ALLR(v) v.rbegin(), v.rend() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> #define mpp make_pair #define ull unsigned long long const int mxn = 1e5 + 5, inf = 1e9 + 5; int n; vt<int>adj[mxn + 1]; int dp[mxn + 1][3], sign[mxn + 1]; void initialize(int N, std::vector<int> A, std::vector<int> B) { n = N; for(int i = 0; i < n; i++){ int u = A[i], v = B[i]; adj[u].pb(v); adj[v].pb(u); } } void dfs(int s, int pre){ if(sign[s]){ dp[s][sign[s]] = 0; for(auto i: adj[s]){ if(i != pre){ dfs(i, s); int cand = inf; for(int j = 0; j < 3; j++){ if(!j || j == sign[s])cand = min(cand, dp[i][j]); else cand = min(cand, dp[i][j] + 1); } dp[s][sign[s]] += cand; } } }else{ dp[s][0] = dp[s][1] = dp[s][2] = 0; for(auto i: adj[s]){ if(i != pre){ dfs(i, s); for(int j = 0; j < 3; j++){ int cand = inf; for(int k = 0; k < 3; k++){ if(!k || k == j)cand = min(cand, dp[i][k]); else cand = min(cand, dp[i][k] + 1); } dp[s][j] += cand; } } } } } int solve(){ for(int i = 1; i <= n; i++){ dp[i][0] = dp[i][1] = dp[i][2] = inf; } dfs(1, -1); return(min(min(dp[1][0], dp[1][1]), dp[1][2])); } int cat(int v) { sign[v] = 1; return(solve()); } int dog(int v) { sign[v] = 2; return(solve()); } int neighbor(int v) { sign[v] = 0; return(solve()); } /* #include <cstdio> #include <cassert> #include <vector> #include <stdlib.h> int readInt(){ int i; if(scanf("%d",&i)!=1){ fprintf(stderr,"Error while reading input\n"); exit(1); } return i; } int main(){ int N=readInt(); std::vector<int> A(N-1),B(N-1); for(int i=0;i<N-1;i++) { A[i]=readInt(); B[i]=readInt(); } int Q; assert(scanf("%d",&Q)==1); std::vector <int> T(Q),V(Q); for(int i=0;i<Q;i++) { T[i]=readInt(); V[i]=readInt(); } initialize(N,A,B); std::vector<int> res(Q); for(int j=0;j<Q;j++) { if(T[j]==1) res[j]=cat(V[j]); else if(T[j]==2) res[j]=dog(V[j]); else res[j]=neighbor(V[j]); } for(int j=0;j<Q;j++) printf("%d\n",res[j]); return 0; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...