Submission #1036770

# Submission time Handle Problem Language Result Execution time Memory
1036770 2024-07-27T16:47:11 Z juicy Capital City (JOI20_capital_city) C++17
1 / 100
1111 ms 524288 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5, LG = 18, MAX = 3400005;

int n, k, m;
int lst[N], c[N], dep[N], pr[LG][N], id[LG][N], low[MAX], num[MAX], cnt[MAX], lab[MAX];
vector<int> tree[N], g[MAX], graph[MAX];

void dfs(int u) {
  for (int v : tree[u]) {
    if (v != pr[0][u]) {
      pr[0][v] = u;
      id[0][v] = c[u];
      for (int i = 1; i < LG; ++i) {
        id[i][v] = ++m;
        pr[i][v] = pr[i - 1][pr[i - 1][v]];
        g[m].push_back(id[i - 1][v]);
        g[m].push_back(id[i - 1][pr[i - 1][v]]);
      }
      dep[v] = dep[u] + 1;
      dfs(v);
    }
  }
}

int lca(int u, int v) {
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  for (int j = LG - 1; ~j; --j) {
    if (dep[u] - (1 << j) >= dep[v]) {
      u = pr[j][u];
    }
  }
  if (u == v) {
    return u;
  }
  for (int j = LG - 1; ~j; --j) {
    if (pr[j][u] != pr[j][v]) {
      u = pr[j][u], v = pr[j][v];
    }
  }
  return pr[0][u];
}

void add(int u, int p) {
  int col = c[u];
  for (int i = LG - 1; ~i; --i) {
    if (dep[u] - (1 << i) >= dep[p]) {
      g[col].push_back(id[i][u]);
      u = pr[i][u];
    }
  }
}

vector<int> st;
int timer, scc;

void DFS(int u) {
  st.push_back(u);
  low[u] = num[u] = ++timer;
  for (int v : g[u]) {
    if (!num[v]) {
      DFS(v);
      low[u] = min(low[u], low[v]);
    } else {
      low[u] = min(low[u], num[v]);
    }
  }
  if (low[u] == num[u]) {
    ++scc;
    while (1) {
      int v = st.back(); st.pop_back();
      num[v] = MAX;
      lab[v] = scc;
      if (v == u) {
        break;
      }
    }
  } 
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> k; m = k;
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }  
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  dfs(1);
  for (int i = 1; i <= n; ++i) {
    if (lst[c[i]]) {
      int x = lca(i, lst[c[i]]);
      add(i, x);
      add(lst[c[i]], x);
    }
    lst[c[i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    if (!num[i]) {
      DFS(i);
    }
  }
  for (int i = 1; i <= k; ++i) {
    ++cnt[lab[i]];
  }
  for (int i = 1; i <= m; ++i) {
    for (int j : g[i]) {
      if (lab[i] != lab[j]) {
        assert(lab[i] > lab[j]);
        graph[lab[i]].push_back(lab[j]);
      }
    }
  }
  int res = MAX;
  for (int i = 1; i <= scc; ++i) {
    for (int j : graph[i]) {
      cnt[i] += cnt[j];
    }
    if (cnt[i]) {
      res = min(res, cnt[i]);
    }
  }
  cout << res - 1;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 60 ms 164944 KB Output is correct
2 Correct 63 ms 164944 KB Output is correct
3 Correct 64 ms 165028 KB Output is correct
4 Correct 80 ms 164868 KB Output is correct
5 Correct 64 ms 165100 KB Output is correct
6 Correct 65 ms 164948 KB Output is correct
7 Correct 67 ms 165292 KB Output is correct
8 Correct 63 ms 164944 KB Output is correct
9 Correct 63 ms 164992 KB Output is correct
10 Correct 67 ms 164624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 164944 KB Output is correct
2 Correct 63 ms 164944 KB Output is correct
3 Correct 64 ms 165028 KB Output is correct
4 Correct 80 ms 164868 KB Output is correct
5 Correct 64 ms 165100 KB Output is correct
6 Correct 65 ms 164948 KB Output is correct
7 Correct 67 ms 165292 KB Output is correct
8 Correct 63 ms 164944 KB Output is correct
9 Correct 63 ms 164992 KB Output is correct
10 Correct 67 ms 164624 KB Output is correct
11 Correct 66 ms 168016 KB Output is correct
12 Correct 69 ms 168020 KB Output is correct
13 Correct 69 ms 168036 KB Output is correct
14 Correct 68 ms 167916 KB Output is correct
15 Incorrect 70 ms 167952 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1111 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 164944 KB Output is correct
2 Correct 63 ms 164944 KB Output is correct
3 Correct 64 ms 165028 KB Output is correct
4 Correct 80 ms 164868 KB Output is correct
5 Correct 64 ms 165100 KB Output is correct
6 Correct 65 ms 164948 KB Output is correct
7 Correct 67 ms 165292 KB Output is correct
8 Correct 63 ms 164944 KB Output is correct
9 Correct 63 ms 164992 KB Output is correct
10 Correct 67 ms 164624 KB Output is correct
11 Correct 66 ms 168016 KB Output is correct
12 Correct 69 ms 168020 KB Output is correct
13 Correct 69 ms 168036 KB Output is correct
14 Correct 68 ms 167916 KB Output is correct
15 Incorrect 70 ms 167952 KB Output isn't correct
16 Halted 0 ms 0 KB -