# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
937698 | 8pete8 | Cats or Dogs (JOI18_catdog) | C++17 | 3036 ms | 11324 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |