답안 #966982

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966982 2024-04-20T19:27:49 Z blackslex 수도 (JOI20_capital_city) C++17
11 / 100
3000 ms 123304 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>()), v2(k + 5, vector<int>()), rev(k + 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;
    vector<int> indeg(k + 5), outdeg(k + 5);
    for (int i = 1; i <= k; i++) {
        cnt.assign(k + 5, 0);
        dfs(st[i], 0, i);
        for (int j = 1; j <= k; j++) if (i != j && cnt[j]) v2[i].emplace_back(j), indeg[i]++, outdeg[j]++, rev[j].emplace_back(i);
    }
    queue<int> q;
    for (int i = 1; i <= k; i++) if (!outdeg[i]) q.emplace(i);
    while (!q.empty()) {
        int cur = q.front(); q.pop();
        for (auto &e: v2[cur]) if (!(--outdeg[e])) q.emplace(e);
    }
    stack<int> cst;
    int csz = 0;
    vector<bool> f(k + 5), f2(k + 5);
    function<void(int, int)> dfs2 = [&] (int cur, int par) {
        if (f[cur]) return; f[cur] = 1;
        for (auto &e: v2[cur]) {
            if (par == e) continue;
            dfs2(e, cur);
        }
        cst.emplace(cur);
    };
    function<void(int, int)> dfs3 = [&] (int cur, int par) {
        if (f2[cur]) return; f2[cur] = 1; csz++;
        for (auto &e: rev[cur]) {
            if (par == e) continue;
            dfs3(e, cur);
        }
    };
    for (int i = 1; i <= k; i++) if (!indeg[i]) ans = 0;
    for (int i = 1; i <= k; i++) if (!outdeg[i]) f[i] = f2[i] = 1;
    for (int i = 1; i <= k; i++) if (!f[i]) dfs2(i, 0);
    while (!cst.empty()) {
        int cur = cst.top(); cst.pop();
        if (f2[cur]) continue; csz = 0;
        dfs3(cur, 0);
        ans = min(ans, csz - 1);
    }
    printf("%d", ans == 1e9 ? 0 : ans);
}

Compilation message

capital_city.cpp: In lambda function:
capital_city.cpp:40:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   40 |         if (f[cur]) return; f[cur] = 1;
      |         ^~
capital_city.cpp:40:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   40 |         if (f[cur]) return; f[cur] = 1;
      |                             ^
capital_city.cpp: In lambda function:
capital_city.cpp:48:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   48 |         if (f2[cur]) return; f2[cur] = 1; csz++;
      |         ^~
capital_city.cpp:48:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   48 |         if (f2[cur]) return; f2[cur] = 1; csz++;
      |                              ^~
capital_city.cpp: In function 'int main()':
capital_city.cpp:59:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   59 |         if (f2[cur]) continue; csz = 0;
      |         ^~
capital_city.cpp:59:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   59 |         if (f2[cur]) continue; csz = 0;
      |                                ^~~
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;
      |                                  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 7 ms 604 KB Output is correct
12 Correct 7 ms 604 KB Output is correct
13 Correct 7 ms 604 KB Output is correct
14 Correct 6 ms 604 KB Output is correct
15 Correct 15 ms 860 KB Output is correct
16 Correct 20 ms 1564 KB Output is correct
17 Correct 11 ms 600 KB Output is correct
18 Correct 11 ms 600 KB Output is correct
19 Correct 11 ms 604 KB Output is correct
20 Correct 11 ms 604 KB Output is correct
21 Correct 12 ms 720 KB Output is correct
22 Correct 29 ms 5980 KB Output is correct
23 Correct 35 ms 5968 KB Output is correct
24 Correct 20 ms 704 KB Output is correct
25 Correct 14 ms 856 KB Output is correct
26 Correct 13 ms 604 KB Output is correct
27 Correct 14 ms 860 KB Output is correct
28 Correct 12 ms 860 KB Output is correct
29 Correct 14 ms 808 KB Output is correct
30 Correct 12 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3057 ms 123304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 7 ms 604 KB Output is correct
12 Correct 7 ms 604 KB Output is correct
13 Correct 7 ms 604 KB Output is correct
14 Correct 6 ms 604 KB Output is correct
15 Correct 15 ms 860 KB Output is correct
16 Correct 20 ms 1564 KB Output is correct
17 Correct 11 ms 600 KB Output is correct
18 Correct 11 ms 600 KB Output is correct
19 Correct 11 ms 604 KB Output is correct
20 Correct 11 ms 604 KB Output is correct
21 Correct 12 ms 720 KB Output is correct
22 Correct 29 ms 5980 KB Output is correct
23 Correct 35 ms 5968 KB Output is correct
24 Correct 20 ms 704 KB Output is correct
25 Correct 14 ms 856 KB Output is correct
26 Correct 13 ms 604 KB Output is correct
27 Correct 14 ms 860 KB Output is correct
28 Correct 12 ms 860 KB Output is correct
29 Correct 14 ms 808 KB Output is correct
30 Correct 12 ms 604 KB Output is correct
31 Execution timed out 3057 ms 123304 KB Time limit exceeded
32 Halted 0 ms 0 KB -