Submission #392191

# Submission time Handle Problem Language Result Execution time Memory
392191 2021-04-20T15:46:36 Z parsabahrami Capital City (JOI20_capital_city) C++17
100 / 100
615 ms 43132 KB
/* There's someone in my head but it's not me */ 
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 2e5 + 10, MOD = 1e9 + 7;
int ret = MOD, c, n, k, hide[N], H[N], S[N], P[N], M[N], dead[N], C[N]; vector<int> adj[N], col[N];

void preDFS(int v, int p = -1) {
    S[v] = 1;
    for (int u : adj[v])
        if (!M[u] && u != p) preDFS(u, v), S[v] += S[u];
}

int centroid(int v, int _n, int p = -1) {
    for (int u : adj[v]) {
        if (!M[u] && u != p && S[u] * 2 > _n) 
            return centroid(u, _n, v);
    }
    return v;
}

void DFS(int v, int p = -1) {
    H[v] = c, P[v] = (~p ? p : v);
    for (int u : adj[v]) {
        if (!M[u] && u != p && !dead[C[u]]) DFS(u, v);
    }
}

void decompose(int v) {
    preDFS(v); 
    v = centroid(v, S[v]);
    c = C[v];
    if (!dead[c]) {
        DFS(v);
        queue<int> Q; vector<int> hist;
        Q.push(c); 
        int now = 0, f = 1;
        while (SZ(Q)) {
            int _c = Q.front(); Q.pop();
            if (hide[_c] || !f) continue;
            hide[_c] = 1; hist.push_back(_c);
            now++;
            for (int u : col[_c]) {
                if (H[u] != c || dead[_c]) f = 0;
                if (f && C[P[u]] != C[u] && !hide[C[P[u]]]) Q.push(C[P[u]]);
            }
        }
        while (SZ(hist)) {
            int u = hist.back(); hist.pop_back();
            hide[u] = 0;
        }
        dead[c] = 1;
        if (f) ret = min(ret, now - 1);
    }
    M[v] = 1;
    for (int u : adj[v])
       if (!M[u]) decompose(u);
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i < n; i++) {
        int u, v; scanf("%d%d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &C[i]); col[C[i]].push_back(i);
    }
    decompose(1);
    printf("%d\n", ret);
    return 0;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:71:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |         scanf("%d", &C[i]); col[C[i]].push_back(i);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 8 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 8 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 8 ms 9804 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9832 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9860 KB Output is correct
16 Correct 8 ms 9804 KB Output is correct
17 Correct 8 ms 9804 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 8 ms 9932 KB Output is correct
20 Correct 8 ms 9932 KB Output is correct
21 Correct 8 ms 9932 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9792 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9936 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9856 KB Output is correct
30 Correct 9 ms 9904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 518 ms 38932 KB Output is correct
2 Correct 205 ms 43044 KB Output is correct
3 Correct 519 ms 42272 KB Output is correct
4 Correct 221 ms 42988 KB Output is correct
5 Correct 504 ms 39364 KB Output is correct
6 Correct 206 ms 43072 KB Output is correct
7 Correct 515 ms 39676 KB Output is correct
8 Correct 201 ms 42616 KB Output is correct
9 Correct 502 ms 37788 KB Output is correct
10 Correct 486 ms 35136 KB Output is correct
11 Correct 500 ms 38188 KB Output is correct
12 Correct 509 ms 40800 KB Output is correct
13 Correct 497 ms 34652 KB Output is correct
14 Correct 513 ms 41056 KB Output is correct
15 Correct 514 ms 40760 KB Output is correct
16 Correct 494 ms 35724 KB Output is correct
17 Correct 506 ms 36292 KB Output is correct
18 Correct 495 ms 36576 KB Output is correct
19 Correct 519 ms 39604 KB Output is correct
20 Correct 519 ms 41948 KB Output is correct
21 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 8 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 8 ms 9804 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9832 KB Output is correct
14 Correct 8 ms 9804 KB Output is correct
15 Correct 8 ms 9860 KB Output is correct
16 Correct 8 ms 9804 KB Output is correct
17 Correct 8 ms 9804 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 8 ms 9932 KB Output is correct
20 Correct 8 ms 9932 KB Output is correct
21 Correct 8 ms 9932 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9792 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9936 KB Output is correct
28 Correct 8 ms 9932 KB Output is correct
29 Correct 8 ms 9856 KB Output is correct
30 Correct 9 ms 9904 KB Output is correct
31 Correct 518 ms 38932 KB Output is correct
32 Correct 205 ms 43044 KB Output is correct
33 Correct 519 ms 42272 KB Output is correct
34 Correct 221 ms 42988 KB Output is correct
35 Correct 504 ms 39364 KB Output is correct
36 Correct 206 ms 43072 KB Output is correct
37 Correct 515 ms 39676 KB Output is correct
38 Correct 201 ms 42616 KB Output is correct
39 Correct 502 ms 37788 KB Output is correct
40 Correct 486 ms 35136 KB Output is correct
41 Correct 500 ms 38188 KB Output is correct
42 Correct 509 ms 40800 KB Output is correct
43 Correct 497 ms 34652 KB Output is correct
44 Correct 513 ms 41056 KB Output is correct
45 Correct 514 ms 40760 KB Output is correct
46 Correct 494 ms 35724 KB Output is correct
47 Correct 506 ms 36292 KB Output is correct
48 Correct 495 ms 36576 KB Output is correct
49 Correct 519 ms 39604 KB Output is correct
50 Correct 519 ms 41948 KB Output is correct
51 Correct 6 ms 9676 KB Output is correct
52 Correct 353 ms 25656 KB Output is correct
53 Correct 390 ms 25548 KB Output is correct
54 Correct 375 ms 25568 KB Output is correct
55 Correct 392 ms 25592 KB Output is correct
56 Correct 366 ms 25552 KB Output is correct
57 Correct 366 ms 25704 KB Output is correct
58 Correct 471 ms 29516 KB Output is correct
59 Correct 445 ms 29820 KB Output is correct
60 Correct 531 ms 28996 KB Output is correct
61 Correct 550 ms 28740 KB Output is correct
62 Correct 207 ms 43100 KB Output is correct
63 Correct 204 ms 43036 KB Output is correct
64 Correct 228 ms 42820 KB Output is correct
65 Correct 207 ms 43132 KB Output is correct
66 Correct 295 ms 34236 KB Output is correct
67 Correct 301 ms 34132 KB Output is correct
68 Correct 298 ms 34116 KB Output is correct
69 Correct 293 ms 34292 KB Output is correct
70 Correct 304 ms 34244 KB Output is correct
71 Correct 287 ms 34100 KB Output is correct
72 Correct 297 ms 34232 KB Output is correct
73 Correct 298 ms 33320 KB Output is correct
74 Correct 287 ms 34364 KB Output is correct
75 Correct 291 ms 34280 KB Output is correct
76 Correct 615 ms 33296 KB Output is correct
77 Correct 585 ms 31444 KB Output is correct
78 Correct 531 ms 36296 KB Output is correct
79 Correct 513 ms 34244 KB Output is correct
80 Correct 502 ms 41380 KB Output is correct
81 Correct 501 ms 37820 KB Output is correct
82 Correct 521 ms 38024 KB Output is correct
83 Correct 507 ms 34468 KB Output is correct
84 Correct 504 ms 40672 KB Output is correct
85 Correct 495 ms 38948 KB Output is correct
86 Correct 539 ms 34196 KB Output is correct
87 Correct 494 ms 35984 KB Output is correct
88 Correct 445 ms 36996 KB Output is correct
89 Correct 460 ms 33040 KB Output is correct
90 Correct 427 ms 32960 KB Output is correct
91 Correct 426 ms 35268 KB Output is correct
92 Correct 428 ms 33988 KB Output is correct
93 Correct 433 ms 33612 KB Output is correct
94 Correct 434 ms 32928 KB Output is correct
95 Correct 419 ms 34408 KB Output is correct
96 Correct 457 ms 33140 KB Output is correct
97 Correct 460 ms 35092 KB Output is correct