답안 #332603

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332603 2020-12-03T00:57:23 Z 12tqian 수도 (JOI20_capital_city) C++17
11 / 100
3000 ms 87360 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;
    function<void(int)> solve = [&](int vert) {
        int root = get_centroid(vert);
        map<int, vector<int>> colors;
        map<int, int> parent;
        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);
        set<int> done;
        list<int> que;
        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 1 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 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 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 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 7 ms 748 KB Output is correct
12 Correct 7 ms 748 KB Output is correct
13 Correct 7 ms 748 KB Output is correct
14 Correct 7 ms 748 KB Output is correct
15 Correct 8 ms 876 KB Output is correct
16 Correct 8 ms 1004 KB Output is correct
17 Correct 6 ms 876 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
19 Correct 6 ms 876 KB Output is correct
20 Correct 6 ms 876 KB Output is correct
21 Correct 6 ms 876 KB Output is correct
22 Correct 6 ms 1132 KB Output is correct
23 Correct 9 ms 1132 KB Output is correct
24 Correct 9 ms 1004 KB Output is correct
25 Correct 11 ms 1004 KB Output is correct
26 Correct 11 ms 1004 KB Output is correct
27 Correct 11 ms 1004 KB Output is correct
28 Correct 10 ms 1004 KB Output is correct
29 Correct 10 ms 876 KB Output is correct
30 Correct 10 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3086 ms 87360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 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 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 7 ms 748 KB Output is correct
12 Correct 7 ms 748 KB Output is correct
13 Correct 7 ms 748 KB Output is correct
14 Correct 7 ms 748 KB Output is correct
15 Correct 8 ms 876 KB Output is correct
16 Correct 8 ms 1004 KB Output is correct
17 Correct 6 ms 876 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
19 Correct 6 ms 876 KB Output is correct
20 Correct 6 ms 876 KB Output is correct
21 Correct 6 ms 876 KB Output is correct
22 Correct 6 ms 1132 KB Output is correct
23 Correct 9 ms 1132 KB Output is correct
24 Correct 9 ms 1004 KB Output is correct
25 Correct 11 ms 1004 KB Output is correct
26 Correct 11 ms 1004 KB Output is correct
27 Correct 11 ms 1004 KB Output is correct
28 Correct 10 ms 1004 KB Output is correct
29 Correct 10 ms 876 KB Output is correct
30 Correct 10 ms 896 KB Output is correct
31 Execution timed out 3086 ms 87360 KB Time limit exceeded
32 Halted 0 ms 0 KB -