Submission #884012

#TimeUsernameProblemLanguageResultExecution timeMemory
884012vjudge1Cats or Dogs (JOI18_catdog)C++17
38 / 100
3032 ms8132 KiB
#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#include "catdog.h"

using namespace std;
using ll = long long;
const int NMAX = 1e5;
const ll INF = 1e5;

int x , n;

vector<int> adj[NMAX+1];
int stat[NMAX+1];
ll dp[3][NMAX+1];

void dfs (int nod = 1, int par = 0)
{
    dp[0][nod] = 0;
    dp[1][nod] = 0;
    dp[2][nod] = 0;
    for (int &to : adj[nod])
    {
        if (to != par)
        {
            dfs(to, nod);
            if (stat[nod] != 2)
                dp[stat[nod]][nod] += min(min(dp[2][to] , dp[stat[nod]][to]) , dp[stat[nod]^1][to]+1);
            else {
                dp[2][nod] += min({dp[0][to] + 1 , dp[1][to] + 1, dp[2][to]});
                dp[1][nod] += min({dp[1][to] , dp[2][to] , dp[0][to]+1});
                dp[0][nod] += min({dp[0][to] , dp[2][to] , dp[1][to]+1});
            }
        }
    }
    if (stat[nod] != 2)
    {
        dp[2][nod] = dp[stat[nod]^1][nod] = INF;
    }
 //   cout << nod << ' ' << dp[0][nod] << ' ' << dp[1][nod] << ' ' << dp[2][nod] << '\n';
}

void initialize(int N, std::vector<int> A, std::vector<int> B) {
  n = N;
  for (int i = 0; i < n-1; i++)
  {
      adj[A[i]].push_back(B[i]);
      adj[B[i]].push_back(A[i]);
  }
  for (int i = 1; i <= n; i++)
  {
      stat[i] = 2;
  }
}

int cat(int v) {
  stat[v] = 1;
  dfs();
  return min({dp[0][1], dp[1][1], dp[2][1]});
}

int dog(int v) {
  stat[v] = 0;
  dfs();
  return min({dp[0][1], dp[1][1], dp[2][1]});
}

int neighbor(int v) {
  stat[v] = 2;
  dfs();
  return min({dp[0][1], dp[1][1], dp[2][1]});
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...