# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
124023 | ksmzzang2003 | Cats or Dogs (JOI18_catdog) | C++14 | 4 ms | 2680 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int MAXQ = 1e5;
const int INF = 987654321;
int N, A[MAXN+10];
int dp[MAXN+10][3];
vector<int> adj[MAXN+10];
int parents[MAXN+10];
void dfs(int,int);
void initialize(int n, vector<int> A, vector<int> B)
{
int i, j;
N=n;
for(i=0; i<N-1; i++)
{
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
dfs(1,-1);
}
void dfs(int now, int par)
{
int i, j;
if(parents[now]==0) parents[now] = par;
for(int nxt : adj[now])
{
if(nxt==par) continue;
dfs(nxt, now);
if(A[now]!=2) dp[now][1]+=min(dp[nxt][1], dp[nxt][2]+1);
if(A[now]!=1) dp[now][2]+=min(dp[nxt][1]+1, dp[nxt][2]);
}
if(A[now]==1) dp[now][2]=INF;
if(A[now]==2) dp[now][1]=INF;
}
void updfs(int now)
{
if(now==-1) return ;
for(int nxt : adj[now])
{
if(nxt==parents[now]) continue;
if(A[now]!=2) dp[now][1]+=min(dp[nxt][1], dp[nxt][2]+1);
if(A[now]!=1) dp[now][2]+=min(dp[nxt][1]+1, dp[nxt][2]);
}
if(A[now]==1) dp[now][2]=INF;
if(A[now]==2) dp[now][1]=INF;
updfs(parents[now]);
}
int solve(int v)
{
updfs(v);
return min(dp[1][2], dp[1][1]);
}
int cat(int v)
{
A[v]=1;
return solve(v);
}
int dog(int v)
{
A[v]=2;
return solve(v);
}
int neighbor(int v)
{
A[v]=0;
return solve(v);
}
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... |