Submission #884012

# Submission time Handle Problem Language Result Execution time Memory
884012 2023-12-06T14:18:49 Z vjudge1 Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 8132 KB
#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 time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5212 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5212 KB Output is correct
9 Correct 1 ms 5208 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 1 ms 5208 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5212 KB Output is correct
15 Correct 1 ms 5208 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5212 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5212 KB Output is correct
9 Correct 1 ms 5208 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 1 ms 5208 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5212 KB Output is correct
15 Correct 1 ms 5208 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 8 ms 5212 KB Output is correct
18 Correct 11 ms 5472 KB Output is correct
19 Correct 5 ms 5212 KB Output is correct
20 Correct 1 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 10 ms 5400 KB Output is correct
24 Correct 9 ms 5212 KB Output is correct
25 Correct 6 ms 5212 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 2 ms 5360 KB Output is correct
28 Correct 3 ms 5208 KB Output is correct
29 Correct 12 ms 5212 KB Output is correct
30 Correct 2 ms 5208 KB Output is correct
31 Correct 2 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5212 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 1 ms 5212 KB Output is correct
5 Correct 1 ms 5212 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 1 ms 5212 KB Output is correct
8 Correct 1 ms 5212 KB Output is correct
9 Correct 1 ms 5208 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 1 ms 5208 KB Output is correct
12 Correct 1 ms 5212 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 1 ms 5212 KB Output is correct
15 Correct 1 ms 5208 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 8 ms 5212 KB Output is correct
18 Correct 11 ms 5472 KB Output is correct
19 Correct 5 ms 5212 KB Output is correct
20 Correct 1 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 10 ms 5400 KB Output is correct
24 Correct 9 ms 5212 KB Output is correct
25 Correct 6 ms 5212 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 2 ms 5360 KB Output is correct
28 Correct 3 ms 5208 KB Output is correct
29 Correct 12 ms 5212 KB Output is correct
30 Correct 2 ms 5208 KB Output is correct
31 Correct 2 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Execution timed out 3032 ms 8132 KB Time limit exceeded
34 Halted 0 ms 0 KB -