Submission #966964

# Submission time Handle Problem Language Result Execution time Memory
966964 2024-04-20T17:55:58 Z blackslex Capital City (JOI20_capital_city) C++17
0 / 100
3000 ms 38024 KB
#include<bits/stdc++.h>

using namespace std;

int n, k, x, y;

int main() {
    scanf("%d %d", &n, &k);
    vector<int> c(n + 5), st(k + 5);
    vector<vector<int>> v(n + 5, vector<int>());
    for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]), st[c[i]] = i;
    vector<bool> cnt(k + 5);
    function<bool(int, int, int)> dfs = [&] (int cur, int par, int col) {
        bool add = (c[cur] == col);
        for (auto &e: v[cur]) {
            if (par == e) continue;
            add |= dfs(e, cur, col);
        }
        if (add) cnt[c[cur]] = 1;
        return add;
    };
    int ans = 1e9;
    for (int i = 1; i <= k; i++) {
        cnt.assign(n + 5, 0);
        dfs(st[i], 0, i);
        int cur = 0;
        for (int j = 1; j <= k; j++) if (cnt[j]) cur++;
        ans = min(ans, cur - 1);
    }
    printf("%d", ans);
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:8:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:11:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |     for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:12:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     for (int i = 1; i <= n; i++) scanf("%d", &c[i]), st[c[i]] = i;
      |                                  ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3060 ms 38024 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -