Submission #1036775

# Submission time Handle Problem Language Result Execution time Memory
1036775 2024-07-27T16:58:07 Z juicy Capital City (JOI20_capital_city) C++17
11 / 100
1066 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 = 3600005;

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]) {
        graph[lab[i]].push_back(lab[j]);
      }
    }
  }
  int res = MAX;
  for (int i = 1; i <= scc; ++i) {
    bool out = 0;
    for (int j : graph[i]) {
      if (cnt[j]) {
        out = 1;
        cnt[i] = 1;
      }
    }
    if (!out && cnt[i]) {
      res = min(res, cnt[i]);
    }
  }
  cout << res - 1;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 70 ms 174344 KB Output is correct
2 Correct 66 ms 174420 KB Output is correct
3 Correct 66 ms 174420 KB Output is correct
4 Correct 75 ms 174420 KB Output is correct
5 Correct 75 ms 174420 KB Output is correct
6 Correct 70 ms 174672 KB Output is correct
7 Correct 65 ms 174244 KB Output is correct
8 Correct 65 ms 174280 KB Output is correct
9 Correct 66 ms 174424 KB Output is correct
10 Correct 66 ms 174164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 174344 KB Output is correct
2 Correct 66 ms 174420 KB Output is correct
3 Correct 66 ms 174420 KB Output is correct
4 Correct 75 ms 174420 KB Output is correct
5 Correct 75 ms 174420 KB Output is correct
6 Correct 70 ms 174672 KB Output is correct
7 Correct 65 ms 174244 KB Output is correct
8 Correct 65 ms 174280 KB Output is correct
9 Correct 66 ms 174424 KB Output is correct
10 Correct 66 ms 174164 KB Output is correct
11 Correct 74 ms 177240 KB Output is correct
12 Correct 75 ms 177240 KB Output is correct
13 Correct 73 ms 177240 KB Output is correct
14 Correct 76 ms 177236 KB Output is correct
15 Correct 74 ms 177488 KB Output is correct
16 Correct 76 ms 177392 KB Output is correct
17 Correct 71 ms 177236 KB Output is correct
18 Correct 74 ms 177492 KB Output is correct
19 Correct 71 ms 177232 KB Output is correct
20 Correct 75 ms 177232 KB Output is correct
21 Correct 73 ms 177432 KB Output is correct
22 Correct 72 ms 177744 KB Output is correct
23 Correct 72 ms 177744 KB Output is correct
24 Correct 75 ms 177524 KB Output is correct
25 Correct 70 ms 177212 KB Output is correct
26 Correct 74 ms 177136 KB Output is correct
27 Correct 78 ms 177308 KB Output is correct
28 Correct 71 ms 177316 KB Output is correct
29 Correct 71 ms 177236 KB Output is correct
30 Correct 72 ms 177132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1066 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 70 ms 174344 KB Output is correct
2 Correct 66 ms 174420 KB Output is correct
3 Correct 66 ms 174420 KB Output is correct
4 Correct 75 ms 174420 KB Output is correct
5 Correct 75 ms 174420 KB Output is correct
6 Correct 70 ms 174672 KB Output is correct
7 Correct 65 ms 174244 KB Output is correct
8 Correct 65 ms 174280 KB Output is correct
9 Correct 66 ms 174424 KB Output is correct
10 Correct 66 ms 174164 KB Output is correct
11 Correct 74 ms 177240 KB Output is correct
12 Correct 75 ms 177240 KB Output is correct
13 Correct 73 ms 177240 KB Output is correct
14 Correct 76 ms 177236 KB Output is correct
15 Correct 74 ms 177488 KB Output is correct
16 Correct 76 ms 177392 KB Output is correct
17 Correct 71 ms 177236 KB Output is correct
18 Correct 74 ms 177492 KB Output is correct
19 Correct 71 ms 177232 KB Output is correct
20 Correct 75 ms 177232 KB Output is correct
21 Correct 73 ms 177432 KB Output is correct
22 Correct 72 ms 177744 KB Output is correct
23 Correct 72 ms 177744 KB Output is correct
24 Correct 75 ms 177524 KB Output is correct
25 Correct 70 ms 177212 KB Output is correct
26 Correct 74 ms 177136 KB Output is correct
27 Correct 78 ms 177308 KB Output is correct
28 Correct 71 ms 177316 KB Output is correct
29 Correct 71 ms 177236 KB Output is correct
30 Correct 72 ms 177132 KB Output is correct
31 Runtime error 1066 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -