Submission #817572

# Submission time Handle Problem Language Result Execution time Memory
817572 2023-08-09T13:39:40 Z LucaIlie Mergers (JOI19_mergers) C++17
70 / 100
161 ms 29644 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;
int state[MAX_N + 1], depth[MAX_N + 1], parent[MAX_N + 1], preord[MAX_N + 1];
vector<int> edges[MAX_N + 1], cities[MAX_N + 1];
set<int> stateEdges[MAX_N + 1];

struct DSU {
    int comp;
    int p[MAX_N + 1];

    void init( int k ) {
        for ( int u = 1; u <= k; u++ )
            p[u] = u;
        comp = k;
    }

    int find( int u ) {
        if ( p[u] == u )
            return u;
        p[u] = find( p[u] );
        return p[u];
    }

    void merge( int u, int v ) {
        u = find( u );
        v = find( v );
        if ( u == v )
            return;

        comp--;
        p[u] = v;
    }
} dsu;

int pre = 0;
void dfs( int u, int p ) {
    parent[u] = p;
    depth[u] = depth[p] + 1;
    preord[u] = pre++;
    for ( int v: edges[u] ) {
        if ( v == p )
            continue;
        dfs( v, u );
    }
}

int main() {
    int n, k;

    cin >> n >> k;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        edges[u].push_back( v );
        edges[v].push_back( u );
    }
    for ( int u = 1; u <= n; u++ ) {
        cin >> state[u];
        cities[state[u]].push_back( u );
    }

    dfs( 1, 0 );

    dsu.init( k );
    for ( int s = 1; s <= k; s++ ) {
        sort( cities[s].begin(), cities[s].end(), []( int u, int v ) {
            return preord[u] < preord[v];
        } );
        for ( int i = 0; i < cities[s].size() - 1; i++ ) {
            int u = cities[s][i], v = cities[s][i + 1];
            while ( depth[u] > depth[v] ) {
                dsu.merge( state[u], state[parent[u]] );
                u = parent[u];
            }
            while ( depth[v] > depth[u] ) {
                dsu.merge( state[v], state[parent[v]] );
                v = parent[v];
            }
            while ( u != v ) {
                dsu.merge( state[u], state[parent[u]] );
                u = parent[u];
                dsu.merge( state[v], state[parent[v]] );
                v = parent[v];
            }
        }
    }

    for ( int u = 1; u <= n; u++ )
        state[u] = dsu.find( state[u] );

    for ( int u = 1; u <= n; u++ ) {
        for ( int v: edges[u] ) {
            if ( state[u] == state[v] )
                continue;
            stateEdges[state[u]].insert( state[v] );
            stateEdges[state[v]].insert( state[u] );
        }
    }

    int leafs = 0;
    for ( int s = 1; s <= k; s++ ) {
        if ( stateEdges[s].size() == 1 )
            leafs++;
    }

    cout << (leafs + 1) / 2;

    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:72:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         for ( int i = 0; i < cities[s].size() - 1; i++ ) {
      |                          ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 7 ms 9652 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9688 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9620 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 7 ms 9636 KB Output is correct
22 Correct 4 ms 9684 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 4 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 7 ms 9652 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9688 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9620 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 7 ms 9636 KB Output is correct
22 Correct 4 ms 9684 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 4 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 7 ms 10068 KB Output is correct
27 Correct 8 ms 9880 KB Output is correct
28 Correct 9 ms 10068 KB Output is correct
29 Correct 9 ms 10196 KB Output is correct
30 Correct 8 ms 9876 KB Output is correct
31 Correct 6 ms 9724 KB Output is correct
32 Correct 9 ms 10112 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 8 ms 9800 KB Output is correct
35 Correct 7 ms 10196 KB Output is correct
36 Correct 6 ms 9796 KB Output is correct
37 Correct 7 ms 9940 KB Output is correct
38 Correct 4 ms 9684 KB Output is correct
39 Correct 9 ms 9812 KB Output is correct
40 Correct 9 ms 9812 KB Output is correct
41 Correct 6 ms 9828 KB Output is correct
42 Correct 8 ms 9976 KB Output is correct
43 Correct 6 ms 9940 KB Output is correct
44 Correct 4 ms 9684 KB Output is correct
45 Correct 8 ms 9940 KB Output is correct
46 Correct 9 ms 9888 KB Output is correct
47 Correct 5 ms 9684 KB Output is correct
48 Correct 7 ms 9812 KB Output is correct
49 Correct 8 ms 10196 KB Output is correct
50 Correct 10 ms 10280 KB Output is correct
51 Correct 7 ms 9828 KB Output is correct
52 Correct 9 ms 9812 KB Output is correct
53 Correct 9 ms 9940 KB Output is correct
54 Correct 6 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 7 ms 9652 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9688 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9620 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 7 ms 9636 KB Output is correct
22 Correct 4 ms 9684 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 4 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 77 ms 15288 KB Output is correct
27 Correct 126 ms 14912 KB Output is correct
28 Correct 7 ms 9812 KB Output is correct
29 Correct 4 ms 9684 KB Output is correct
30 Correct 4 ms 9684 KB Output is correct
31 Correct 87 ms 14940 KB Output is correct
32 Correct 7 ms 9844 KB Output is correct
33 Correct 152 ms 19180 KB Output is correct
34 Correct 93 ms 16492 KB Output is correct
35 Correct 6 ms 9848 KB Output is correct
36 Correct 86 ms 16748 KB Output is correct
37 Correct 8 ms 9812 KB Output is correct
38 Correct 7 ms 9832 KB Output is correct
39 Correct 75 ms 16664 KB Output is correct
40 Correct 9 ms 9940 KB Output is correct
41 Correct 78 ms 16280 KB Output is correct
42 Correct 153 ms 17780 KB Output is correct
43 Correct 5 ms 9704 KB Output is correct
44 Correct 104 ms 20776 KB Output is correct
45 Correct 146 ms 18680 KB Output is correct
46 Correct 7 ms 9940 KB Output is correct
47 Correct 7 ms 9844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 15260 KB Output is correct
2 Correct 161 ms 27664 KB Output is correct
3 Correct 7 ms 10068 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 101 ms 17200 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 88 ms 15060 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 81 ms 14964 KB Output is correct
13 Correct 100 ms 18036 KB Output is correct
14 Correct 158 ms 27368 KB Output is correct
15 Correct 73 ms 15260 KB Output is correct
16 Correct 7 ms 9864 KB Output is correct
17 Correct 4 ms 9720 KB Output is correct
18 Correct 139 ms 25460 KB Output is correct
19 Correct 131 ms 29644 KB Output is correct
20 Correct 7 ms 9936 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 106 ms 18288 KB Output is correct
23 Correct 8 ms 9812 KB Output is correct
24 Correct 99 ms 16176 KB Output is correct
25 Correct 127 ms 28804 KB Output is correct
26 Correct 7 ms 10164 KB Output is correct
27 Correct 7 ms 10196 KB Output is correct
28 Correct 7 ms 9816 KB Output is correct
29 Correct 6 ms 9904 KB Output is correct
30 Correct 7 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 7 ms 9652 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9688 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9620 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 7 ms 9636 KB Output is correct
22 Correct 4 ms 9684 KB Output is correct
23 Correct 5 ms 9688 KB Output is correct
24 Correct 4 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 7 ms 10068 KB Output is correct
27 Correct 8 ms 9880 KB Output is correct
28 Correct 9 ms 10068 KB Output is correct
29 Correct 9 ms 10196 KB Output is correct
30 Correct 8 ms 9876 KB Output is correct
31 Correct 6 ms 9724 KB Output is correct
32 Correct 9 ms 10112 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 8 ms 9800 KB Output is correct
35 Correct 7 ms 10196 KB Output is correct
36 Correct 6 ms 9796 KB Output is correct
37 Correct 7 ms 9940 KB Output is correct
38 Correct 4 ms 9684 KB Output is correct
39 Correct 9 ms 9812 KB Output is correct
40 Correct 9 ms 9812 KB Output is correct
41 Correct 6 ms 9828 KB Output is correct
42 Correct 8 ms 9976 KB Output is correct
43 Correct 6 ms 9940 KB Output is correct
44 Correct 4 ms 9684 KB Output is correct
45 Correct 8 ms 9940 KB Output is correct
46 Correct 9 ms 9888 KB Output is correct
47 Correct 5 ms 9684 KB Output is correct
48 Correct 7 ms 9812 KB Output is correct
49 Correct 8 ms 10196 KB Output is correct
50 Correct 10 ms 10280 KB Output is correct
51 Correct 7 ms 9828 KB Output is correct
52 Correct 9 ms 9812 KB Output is correct
53 Correct 9 ms 9940 KB Output is correct
54 Correct 6 ms 9812 KB Output is correct
55 Correct 5 ms 9684 KB Output is correct
56 Correct 77 ms 15288 KB Output is correct
57 Correct 126 ms 14912 KB Output is correct
58 Correct 7 ms 9812 KB Output is correct
59 Correct 4 ms 9684 KB Output is correct
60 Correct 4 ms 9684 KB Output is correct
61 Correct 87 ms 14940 KB Output is correct
62 Correct 7 ms 9844 KB Output is correct
63 Correct 152 ms 19180 KB Output is correct
64 Correct 93 ms 16492 KB Output is correct
65 Correct 6 ms 9848 KB Output is correct
66 Correct 86 ms 16748 KB Output is correct
67 Correct 8 ms 9812 KB Output is correct
68 Correct 7 ms 9832 KB Output is correct
69 Correct 75 ms 16664 KB Output is correct
70 Correct 9 ms 9940 KB Output is correct
71 Correct 78 ms 16280 KB Output is correct
72 Correct 153 ms 17780 KB Output is correct
73 Correct 5 ms 9704 KB Output is correct
74 Correct 104 ms 20776 KB Output is correct
75 Correct 146 ms 18680 KB Output is correct
76 Correct 7 ms 9940 KB Output is correct
77 Correct 7 ms 9844 KB Output is correct
78 Correct 78 ms 15260 KB Output is correct
79 Correct 161 ms 27664 KB Output is correct
80 Correct 7 ms 10068 KB Output is correct
81 Correct 6 ms 9812 KB Output is correct
82 Correct 4 ms 9684 KB Output is correct
83 Correct 4 ms 9684 KB Output is correct
84 Correct 6 ms 9812 KB Output is correct
85 Correct 101 ms 17200 KB Output is correct
86 Correct 7 ms 9812 KB Output is correct
87 Correct 88 ms 15060 KB Output is correct
88 Correct 4 ms 9684 KB Output is correct
89 Correct 81 ms 14964 KB Output is correct
90 Correct 100 ms 18036 KB Output is correct
91 Correct 158 ms 27368 KB Output is correct
92 Correct 73 ms 15260 KB Output is correct
93 Correct 7 ms 9864 KB Output is correct
94 Correct 4 ms 9720 KB Output is correct
95 Correct 139 ms 25460 KB Output is correct
96 Correct 131 ms 29644 KB Output is correct
97 Correct 7 ms 9936 KB Output is correct
98 Correct 5 ms 9684 KB Output is correct
99 Correct 106 ms 18288 KB Output is correct
100 Correct 8 ms 9812 KB Output is correct
101 Correct 99 ms 16176 KB Output is correct
102 Correct 127 ms 28804 KB Output is correct
103 Correct 7 ms 10164 KB Output is correct
104 Correct 7 ms 10196 KB Output is correct
105 Correct 7 ms 9816 KB Output is correct
106 Correct 6 ms 9904 KB Output is correct
107 Correct 7 ms 9940 KB Output is correct
108 Runtime error 12 ms 19524 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -