Submission #900614

# Submission time Handle Problem Language Result Execution time Memory
900614 2024-01-08T16:21:08 Z LucaIlie Capital City (JOI20_capital_city) C++17
100 / 100
589 ms 43680 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 1;
const int MAX_K = 2e5 + 1;
bool isCentroid[MAX_N + 1], cityIn[MAX_N + 1];
int city[MAX_N + 1], sizee[MAX_N + 1], frecv[MAX_N + 1], frecvC[MAX_K + 1], parent[MAX_N + 1];
vector<int> adj[MAX_N + 1], towns[MAX_K + 1];

void calcSizes( int u, int p ) {
    sizee[u] = 1;
    for ( int v: adj[u] ) {
        if ( v == p || isCentroid[v] )
            continue;
        calcSizes( v, u );
        sizee[u] += sizee[v];
    }
}

int centroid, sizetot;
void findCentroid( int u, int p ) {
    int maxSize = sizetot - sizee[u];
    for ( int v: adj[u] ) {
        if ( v == p || isCentroid[v] )
            continue;
        findCentroid( v, u );
        maxSize = max( maxSize, sizee[v] );
    }
    if ( maxSize <= sizetot / 2 )
        centroid = u;
}

vector<int> vert;
void dfs( int u, int p ) {
    parent[u] = p;
    vert.push_back( u );
    frecvC[city[u]]++;
    for ( int v: adj[u] ) {
        if ( v == p || isCentroid[v] )
            continue;
        dfs( v, u );
    }
}

int minMerges;
void decomp( int r ) {
    calcSizes( r, 0 );
    sizetot = sizee[r];
    findCentroid( r, 0 );
    int c = centroid;

    vert.clear();
    dfs( c, 0 );

    queue<int> q;
    bool sePoate = true;
    int m = 0;
    if ( frecvC[city[c]] == frecv[city[c]] ) {
        cityIn[city[c]] = true;
        for ( int v: towns[city[c]] )
            q.push( v );
        while ( !q.empty() ) {
            int v = q.front();
            q.pop();

            int x = city[parent[v]];
            if ( x != 0 && !cityIn[x] ) {
                cityIn[x] = true;
                m++;
                if ( frecvC[x] != frecv[x] ) {
                    sePoate = false;
                    break;
                }
                for ( int v: towns[x] )
                    q.push( v );
            }
        }
    } else
        sePoate = false;

    if ( sePoate )
        minMerges = min( minMerges, m );

    for ( int v: vert ) {
        frecvC[city[v]] = 0;
        cityIn[city[v]] = false;
    }

    isCentroid[c] = true;
    for ( int v: adj[c] ) {
        if ( !isCentroid[v] )
            decomp( v );
    }
}

int main() {
    int n, k;

    cin >> n >> k;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back( v );
        adj[v].push_back( u );
    }
    for ( int v = 1; v <= n; v++ ) {
        cin >> city[v];
        frecv[city[v]]++;
        towns[city[v]].push_back( v );
    }

    minMerges = k - 1;
    decomp( 1 );

    cout << minMerges;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 2 ms 12924 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 2 ms 12924 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 5 ms 12892 KB Output is correct
12 Correct 5 ms 12940 KB Output is correct
13 Correct 6 ms 12880 KB Output is correct
14 Correct 4 ms 13080 KB Output is correct
15 Correct 4 ms 12888 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13052 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 13148 KB Output is correct
21 Correct 4 ms 13156 KB Output is correct
22 Correct 4 ms 13236 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
25 Correct 5 ms 13184 KB Output is correct
26 Correct 6 ms 13148 KB Output is correct
27 Correct 5 ms 13192 KB Output is correct
28 Correct 6 ms 13148 KB Output is correct
29 Correct 5 ms 13148 KB Output is correct
30 Correct 5 ms 13016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 427 ms 42968 KB Output is correct
2 Correct 271 ms 43468 KB Output is correct
3 Correct 488 ms 42640 KB Output is correct
4 Correct 265 ms 43496 KB Output is correct
5 Correct 380 ms 39632 KB Output is correct
6 Correct 264 ms 43680 KB Output is correct
7 Correct 454 ms 39988 KB Output is correct
8 Correct 260 ms 43140 KB Output is correct
9 Correct 497 ms 38348 KB Output is correct
10 Correct 589 ms 36120 KB Output is correct
11 Correct 471 ms 38800 KB Output is correct
12 Correct 540 ms 41412 KB Output is correct
13 Correct 506 ms 35376 KB Output is correct
14 Correct 529 ms 41744 KB Output is correct
15 Correct 529 ms 41232 KB Output is correct
16 Correct 527 ms 36492 KB Output is correct
17 Correct 526 ms 36812 KB Output is correct
18 Correct 506 ms 36956 KB Output is correct
19 Correct 501 ms 40224 KB Output is correct
20 Correct 498 ms 42616 KB Output is correct
21 Correct 3 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 2 ms 12892 KB Output is correct
8 Correct 2 ms 12888 KB Output is correct
9 Correct 2 ms 12924 KB Output is correct
10 Correct 2 ms 12892 KB Output is correct
11 Correct 5 ms 12892 KB Output is correct
12 Correct 5 ms 12940 KB Output is correct
13 Correct 6 ms 12880 KB Output is correct
14 Correct 4 ms 13080 KB Output is correct
15 Correct 4 ms 12888 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13052 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 13148 KB Output is correct
21 Correct 4 ms 13156 KB Output is correct
22 Correct 4 ms 13236 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
25 Correct 5 ms 13184 KB Output is correct
26 Correct 6 ms 13148 KB Output is correct
27 Correct 5 ms 13192 KB Output is correct
28 Correct 6 ms 13148 KB Output is correct
29 Correct 5 ms 13148 KB Output is correct
30 Correct 5 ms 13016 KB Output is correct
31 Correct 427 ms 42968 KB Output is correct
32 Correct 271 ms 43468 KB Output is correct
33 Correct 488 ms 42640 KB Output is correct
34 Correct 265 ms 43496 KB Output is correct
35 Correct 380 ms 39632 KB Output is correct
36 Correct 264 ms 43680 KB Output is correct
37 Correct 454 ms 39988 KB Output is correct
38 Correct 260 ms 43140 KB Output is correct
39 Correct 497 ms 38348 KB Output is correct
40 Correct 589 ms 36120 KB Output is correct
41 Correct 471 ms 38800 KB Output is correct
42 Correct 540 ms 41412 KB Output is correct
43 Correct 506 ms 35376 KB Output is correct
44 Correct 529 ms 41744 KB Output is correct
45 Correct 529 ms 41232 KB Output is correct
46 Correct 527 ms 36492 KB Output is correct
47 Correct 526 ms 36812 KB Output is correct
48 Correct 506 ms 36956 KB Output is correct
49 Correct 501 ms 40224 KB Output is correct
50 Correct 498 ms 42616 KB Output is correct
51 Correct 3 ms 12892 KB Output is correct
52 Correct 347 ms 26652 KB Output is correct
53 Correct 351 ms 26624 KB Output is correct
54 Correct 389 ms 26644 KB Output is correct
55 Correct 393 ms 26808 KB Output is correct
56 Correct 399 ms 26572 KB Output is correct
57 Correct 359 ms 26572 KB Output is correct
58 Correct 386 ms 29888 KB Output is correct
59 Correct 342 ms 30408 KB Output is correct
60 Correct 421 ms 29608 KB Output is correct
61 Correct 455 ms 29232 KB Output is correct
62 Correct 261 ms 43468 KB Output is correct
63 Correct 278 ms 43408 KB Output is correct
64 Correct 268 ms 43196 KB Output is correct
65 Correct 273 ms 43616 KB Output is correct
66 Correct 319 ms 35288 KB Output is correct
67 Correct 367 ms 35160 KB Output is correct
68 Correct 302 ms 35212 KB Output is correct
69 Correct 308 ms 35112 KB Output is correct
70 Correct 315 ms 35124 KB Output is correct
71 Correct 317 ms 35208 KB Output is correct
72 Correct 299 ms 35208 KB Output is correct
73 Correct 340 ms 34228 KB Output is correct
74 Correct 298 ms 35152 KB Output is correct
75 Correct 330 ms 35284 KB Output is correct
76 Correct 425 ms 33536 KB Output is correct
77 Correct 390 ms 31676 KB Output is correct
78 Correct 551 ms 36812 KB Output is correct
79 Correct 517 ms 34768 KB Output is correct
80 Correct 543 ms 42184 KB Output is correct
81 Correct 553 ms 38580 KB Output is correct
82 Correct 535 ms 38604 KB Output is correct
83 Correct 547 ms 35272 KB Output is correct
84 Correct 559 ms 40968 KB Output is correct
85 Correct 489 ms 39428 KB Output is correct
86 Correct 553 ms 34628 KB Output is correct
87 Correct 569 ms 36792 KB Output is correct
88 Correct 397 ms 37792 KB Output is correct
89 Correct 436 ms 33708 KB Output is correct
90 Correct 459 ms 33552 KB Output is correct
91 Correct 431 ms 35788 KB Output is correct
92 Correct 468 ms 34636 KB Output is correct
93 Correct 412 ms 34260 KB Output is correct
94 Correct 411 ms 33732 KB Output is correct
95 Correct 442 ms 35032 KB Output is correct
96 Correct 443 ms 33772 KB Output is correct
97 Correct 492 ms 35788 KB Output is correct