답안 #332604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332604 2020-12-03T00:59:08 Z 12tqian 수도 (JOI20_capital_city) C++17
11 / 100
3000 ms 52204 KB
#include <bits/stdc++.h>

int main() {
    using namespace std;
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<int> c(n);
    vector<int> count(n);
    for (int i = 0; i < n; i++)
        cin >> c[i], c[i]--, count[c[i]]++;
    vector<bool> vis(n);
    vector<int> sub(n);
    function<void(int, int)> dfs_size = [&](int src, int par) {
        sub[src] = 1;
        for (int nxt : adj[src]) {
            if (nxt == par || vis[nxt]) 
                continue;
            dfs_size(nxt, src);
            sub[src] += sub[nxt];
        }
    };
    auto get_centroid = [&](int src) -> int { 
        dfs_size(src, -1);
        int num = sub[src];
        int par = -1;
        do {
            int go = -1;
            for (int nxt : adj[src]) {
                if (nxt == par || vis[nxt]) 
                    continue;
                if (sub[nxt] * 2 > num)
                    go = nxt;
            }
            par = src;
            src = go;
        } while (src != -1);
        return par;
    };
    const int INF = 1e9;
    int ans = INF;
    map<int, vector<int>> colors;
    map<int, int> parent;
    set<int> done;
    list<int> que;
    function<void(int)> solve = [&](int vert) {
        colors.clear();
        parent.clear();
        done.clear();
        que.clear();
        int root = get_centroid(vert);
        function<void(int, int)> dfs_precompute = [&](int src, int par) {
            parent[src] = par;
            colors[c[src]].push_back(src);
            for (int nxt : adj[src]) {
                if (nxt == par || vis[nxt]) 
                    continue;
                dfs_precompute(nxt, src);
            }
        };
        dfs_precompute(root, -1);
        int tot = 0;
        auto add_color = [&](int col) {
            if (done.count(col))
                return;
            tot++;
            done.insert(col);
            for (int v : colors[col]) {
                que.push_back(v);
            }
        };
        add_color(c[root]);
        while (!que.empty()) {
            int top = que.front();
            que.pop_front();
            int par = parent[top];
            if (par != -1)
                add_color(c[par]);
        }
        bool ok = true;
        for (int x : done) 
            if ((int) colors[x].size() != count[x]) 
                ok = false;
        if (ok)
            ans = min(ans, tot - 1);
        vis[root] = true;
        for (int nxt : adj[root]) {
            if (vis[nxt]) 
                continue;
            solve(nxt);
        }
    };
    solve(0);
    cout << ans << '\n';
    return 0;
}  
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 7 ms 620 KB Output is correct
12 Correct 7 ms 620 KB Output is correct
13 Correct 7 ms 620 KB Output is correct
14 Correct 7 ms 620 KB Output is correct
15 Correct 8 ms 748 KB Output is correct
16 Correct 8 ms 748 KB Output is correct
17 Correct 5 ms 748 KB Output is correct
18 Correct 5 ms 748 KB Output is correct
19 Correct 6 ms 748 KB Output is correct
20 Correct 6 ms 748 KB Output is correct
21 Correct 6 ms 748 KB Output is correct
22 Correct 6 ms 876 KB Output is correct
23 Correct 8 ms 748 KB Output is correct
24 Correct 9 ms 748 KB Output is correct
25 Correct 12 ms 876 KB Output is correct
26 Correct 11 ms 876 KB Output is correct
27 Correct 11 ms 748 KB Output is correct
28 Correct 10 ms 748 KB Output is correct
29 Correct 10 ms 748 KB Output is correct
30 Correct 10 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3097 ms 52204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 7 ms 620 KB Output is correct
12 Correct 7 ms 620 KB Output is correct
13 Correct 7 ms 620 KB Output is correct
14 Correct 7 ms 620 KB Output is correct
15 Correct 8 ms 748 KB Output is correct
16 Correct 8 ms 748 KB Output is correct
17 Correct 5 ms 748 KB Output is correct
18 Correct 5 ms 748 KB Output is correct
19 Correct 6 ms 748 KB Output is correct
20 Correct 6 ms 748 KB Output is correct
21 Correct 6 ms 748 KB Output is correct
22 Correct 6 ms 876 KB Output is correct
23 Correct 8 ms 748 KB Output is correct
24 Correct 9 ms 748 KB Output is correct
25 Correct 12 ms 876 KB Output is correct
26 Correct 11 ms 876 KB Output is correct
27 Correct 11 ms 748 KB Output is correct
28 Correct 10 ms 748 KB Output is correct
29 Correct 10 ms 748 KB Output is correct
30 Correct 10 ms 748 KB Output is correct
31 Execution timed out 3097 ms 52204 KB Time limit exceeded
32 Halted 0 ms 0 KB -