Submission #937698

#TimeUsernameProblemLanguageResultExecution timeMemory
9376988pete8Cats or Dogs (JOI18_catdog)C++17
38 / 100
3036 ms11324 KiB
#include "catdog.h" #include<iostream> #include<stack> #include<map> #include<vector> #include<string> #include<unordered_map> #include <queue> #include<cstring> #include<cassert> #include<limits.h> #include<cmath> #include<set> #include<numeric> //gcd(a,b) #include<algorithm> #include<bitset> #include<stack> using namespace std; #define ll long long #define f first //#define endl "\n" #define s second #define pii pair<int,int> #define pppiiii pair<pii,pii> #define ppii pair<int,pii> #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define pb push_back //#define mp make_pair #define lb lower_bound #define ub upper_bound #define fastio ios::sync_with_stdio(false);cin.tie(NULL); #pragma GCC optimize ("03,unroll-loops") //#define int long long const int mod=1e9+7,mxn=2e5,inf=1e9,minf=-1e18,Mxn=2e6,lg=63; int root; void setIO(string name){ ios_base::sync_with_stdio(0); cin.tie(0); freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout); } vector<int>adj[mxn+10]; int have[mxn+10],dp[mxn+10][2],n; void caldp(int cur,int p){ for(auto i:adj[cur]){ if(i==p)continue; caldp(i,cur); for(int x=0;x<2;x++){ dp[cur][x]+=min(dp[i][x],dp[i][x^1]+1); } } //cout<<cur<<" "<<dp[cur][0]<<" "<<dp[cur][1]<<'\n'; } void init(){ for(int i=1;i<=n;i++){ dp[i][0]=dp[i][1]=0; if(have[i])dp[i][(have[i]-1)^1]=inf; } } void initialize(int N,vector<int>A,vector<int>B){ n=N; for(int i=0;i<n-1;i++){ adj[A[i]].pb(B[i]); adj[B[i]].pb(A[i]); } } int cat(int v){//0 in dp,1 in have have[v]=1; init(); caldp(1,-1); return min(dp[1][0],dp[1][1]); } int dog(int v){ have[v]=2; init(); caldp(1,-1); return min(dp[1][0],dp[1][1]); } int neighbor(int v){ have[v]=0; init(); caldp(1,-1); return min(dp[1][0],dp[1][1]); } /* each node can either be cat or dog dp[i][2] cost of node i being (cat or dog) by being cat(or dog) it allowed cat(or dog) to walk pass if node i has a cat then node i cant be a dog transition dp[i][x]->x=1 is cant,2 is dog dp[i][x]=sum of j in children min(dp[j][x],dp[j][x^1]+1); so we can use the other one with the cost of one this solutionn is o(n^2) can we do better? hld on dp? when will it overtake? */

Compilation message (stderr)

catdog.cpp:35:42: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
   35 | const int mod=1e9+7,mxn=2e5,inf=1e9,minf=-1e18,Mxn=2e6,lg=63;
      |                                          ^~~~~
catdog.cpp: In function 'void setIO(std::string)':
catdog.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catdog.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...