Submission #379508

# Submission time Handle Problem Language Result Execution time Memory
379508 2021-03-18T11:15:12 Z qwerty234 Cats or Dogs (JOI18_catdog) C++14
38 / 100
3000 ms 9948 KB
#include "catdog.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

const int MAXN = 2e5 + 10;

int dp[MAXN][3], col[MAXN], inf = 1e9;
vector <int> g[MAXN];

void initialize(int n, vector <int> a, vector <int> b) {
  for (int i = 1; i <= n; i++)
    g[i].clear();
  for (int i = 0; i < n - 1; i++) {
    g[a[i]].pb(b[i]);
    g[b[i]].pb(a[i]);
  }
  for (int i = 1; i <= n; i++)
    col[i] = 0;
}

void dfs(int u, int p) {
  for (int to : g[u]) {
    if (p == to)
      continue;
    dfs(to, u);
  }
  if (col[u] == 0) {
    dp[u][0] = dp[u][1] = dp[u][2] = 0;
    for (int to : g[u]) {
      if (to == p)
        continue;
      dp[u][0] += min(min(inf, dp[to][0]), min(dp[to][1] + 1, dp[to][2] + 1));
      dp[u][1] += min(min(inf, dp[to][0]), min(dp[to][1], dp[to][2] + 1));
      dp[u][2] += min(min(inf, dp[to][0]), min(dp[to][1] + 1, dp[to][2]));
      dp[u][0] = min(dp[u][0], inf);
      dp[u][1] = min(dp[u][1], inf);
      dp[u][2] = min(dp[u][2], inf);
    }
  } else if (col[u] == 1) {
    dp[u][0] = dp[u][2] = inf; dp[u][1] = 0;
    for (int to : g[u]) {
      if (to == p)
        continue;
      dp[u][1] += min(min(inf, dp[to][0]), min(dp[to][1], dp[to][2] + 1));
      dp[u][1] = min(dp[u][1], inf);
    }
  } else {
    dp[u][0] = dp[u][1] = inf; dp[u][2] = 0;
    for (int to : g[u]) {
      if (to == p)
        continue;
      dp[u][2] += min(min(inf, dp[to][0]), min(dp[to][1] + 1, dp[to][2]));
      dp[u][2] = min(dp[u][2], inf);
    }
  }
}

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

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

int neighbor(int v) {
  col[v] = 0;
  dfs(1, -1);
  return min(dp[1][0], min(dp[1][1], dp[1][2]));
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 4 ms 4972 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 4 ms 4972 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 4 ms 4972 KB Output is correct
12 Correct 4 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 4 ms 4972 KB Output is correct
15 Correct 3 ms 4972 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 4 ms 4972 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 4 ms 4972 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 4 ms 4972 KB Output is correct
12 Correct 4 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 4 ms 4972 KB Output is correct
15 Correct 3 ms 4972 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 19 ms 5228 KB Output is correct
18 Correct 22 ms 5100 KB Output is correct
19 Correct 13 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5100 KB Output is correct
22 Correct 7 ms 5100 KB Output is correct
23 Correct 25 ms 5100 KB Output is correct
24 Correct 21 ms 5100 KB Output is correct
25 Correct 11 ms 5100 KB Output is correct
26 Correct 9 ms 5100 KB Output is correct
27 Correct 6 ms 5100 KB Output is correct
28 Correct 8 ms 5100 KB Output is correct
29 Correct 26 ms 5100 KB Output is correct
30 Correct 6 ms 5100 KB Output is correct
31 Correct 5 ms 5100 KB Output is correct
32 Correct 8 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 4 ms 4972 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 4 ms 4972 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 4 ms 4972 KB Output is correct
12 Correct 4 ms 4972 KB Output is correct
13 Correct 3 ms 4972 KB Output is correct
14 Correct 4 ms 4972 KB Output is correct
15 Correct 3 ms 4972 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 19 ms 5228 KB Output is correct
18 Correct 22 ms 5100 KB Output is correct
19 Correct 13 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5100 KB Output is correct
22 Correct 7 ms 5100 KB Output is correct
23 Correct 25 ms 5100 KB Output is correct
24 Correct 21 ms 5100 KB Output is correct
25 Correct 11 ms 5100 KB Output is correct
26 Correct 9 ms 5100 KB Output is correct
27 Correct 6 ms 5100 KB Output is correct
28 Correct 8 ms 5100 KB Output is correct
29 Correct 26 ms 5100 KB Output is correct
30 Correct 6 ms 5100 KB Output is correct
31 Correct 5 ms 5100 KB Output is correct
32 Correct 8 ms 5100 KB Output is correct
33 Execution timed out 3059 ms 9948 KB Time limit exceeded
34 Halted 0 ms 0 KB -