답안 #817562

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
817562 2023-08-09T13:36:12 Z LucaIlie Mergers (JOI19_mergers) C++17
56 / 100
3000 ms 30956 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];
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;

void dfs( int u, int p ) {
    parent[u] = p;
    depth[u] = depth[p] + 1;
    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++ ) {
        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:67:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for ( int i = 0; i < cities[s].size() - 1; i++ ) {
      |                          ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9644 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 4 ms 9712 KB Output is correct
11 Correct 6 ms 9700 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9656 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 4 ms 9684 KB Output is correct
22 Correct 5 ms 9708 KB Output is correct
23 Correct 6 ms 9704 KB Output is correct
24 Correct 5 ms 9704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9644 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 4 ms 9712 KB Output is correct
11 Correct 6 ms 9700 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9656 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 4 ms 9684 KB Output is correct
22 Correct 5 ms 9708 KB Output is correct
23 Correct 6 ms 9704 KB Output is correct
24 Correct 5 ms 9704 KB Output is correct
25 Correct 4 ms 9684 KB Output is correct
26 Correct 8 ms 10012 KB Output is correct
27 Correct 8 ms 9812 KB Output is correct
28 Correct 8 ms 10196 KB Output is correct
29 Correct 7 ms 10196 KB Output is correct
30 Correct 8 ms 9812 KB Output is correct
31 Correct 6 ms 9728 KB Output is correct
32 Correct 7 ms 10100 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 6 ms 9916 KB Output is correct
35 Correct 8 ms 10216 KB Output is correct
36 Correct 9 ms 9812 KB Output is correct
37 Correct 9 ms 9840 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 10 ms 9940 KB Output is correct
40 Correct 9 ms 9812 KB Output is correct
41 Correct 10 ms 9848 KB Output is correct
42 Correct 7 ms 9940 KB Output is correct
43 Correct 15 ms 9964 KB Output is correct
44 Correct 6 ms 9712 KB Output is correct
45 Correct 8 ms 9940 KB Output is correct
46 Correct 8 ms 9976 KB Output is correct
47 Correct 6 ms 9684 KB Output is correct
48 Correct 7 ms 9848 KB Output is correct
49 Correct 7 ms 10180 KB Output is correct
50 Correct 7 ms 10232 KB Output is correct
51 Correct 8 ms 9940 KB Output is correct
52 Correct 6 ms 9848 KB Output is correct
53 Correct 8 ms 9972 KB Output is correct
54 Correct 8 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9644 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 4 ms 9712 KB Output is correct
11 Correct 6 ms 9700 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9656 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 4 ms 9684 KB Output is correct
22 Correct 5 ms 9708 KB Output is correct
23 Correct 6 ms 9704 KB Output is correct
24 Correct 5 ms 9704 KB Output is correct
25 Correct 5 ms 9708 KB Output is correct
26 Correct 113 ms 16316 KB Output is correct
27 Correct 101 ms 15976 KB Output is correct
28 Correct 9 ms 9812 KB Output is correct
29 Correct 4 ms 9712 KB Output is correct
30 Correct 6 ms 9628 KB Output is correct
31 Correct 117 ms 15940 KB Output is correct
32 Correct 7 ms 9928 KB Output is correct
33 Execution timed out 3060 ms 20292 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 14880 KB Output is correct
2 Correct 178 ms 27252 KB Output is correct
3 Correct 9 ms 10108 KB Output is correct
4 Correct 8 ms 9912 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9708 KB Output is correct
7 Correct 6 ms 9844 KB Output is correct
8 Correct 128 ms 18600 KB Output is correct
9 Correct 8 ms 9812 KB Output is correct
10 Correct 112 ms 16232 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 123 ms 16168 KB Output is correct
13 Correct 129 ms 19400 KB Output is correct
14 Correct 160 ms 28700 KB Output is correct
15 Correct 84 ms 16264 KB Output is correct
16 Correct 9 ms 9940 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 209 ms 26812 KB Output is correct
19 Correct 200 ms 30956 KB Output is correct
20 Correct 10 ms 9852 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
22 Correct 113 ms 19512 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 126 ms 17392 KB Output is correct
25 Correct 180 ms 30212 KB Output is correct
26 Correct 10 ms 10196 KB Output is correct
27 Correct 9 ms 10236 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 9940 KB Output is correct
30 Correct 7 ms 9980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9644 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 4 ms 9712 KB Output is correct
11 Correct 6 ms 9700 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9620 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9656 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 4 ms 9684 KB Output is correct
22 Correct 5 ms 9708 KB Output is correct
23 Correct 6 ms 9704 KB Output is correct
24 Correct 5 ms 9704 KB Output is correct
25 Correct 4 ms 9684 KB Output is correct
26 Correct 8 ms 10012 KB Output is correct
27 Correct 8 ms 9812 KB Output is correct
28 Correct 8 ms 10196 KB Output is correct
29 Correct 7 ms 10196 KB Output is correct
30 Correct 8 ms 9812 KB Output is correct
31 Correct 6 ms 9728 KB Output is correct
32 Correct 7 ms 10100 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct
34 Correct 6 ms 9916 KB Output is correct
35 Correct 8 ms 10216 KB Output is correct
36 Correct 9 ms 9812 KB Output is correct
37 Correct 9 ms 9840 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 10 ms 9940 KB Output is correct
40 Correct 9 ms 9812 KB Output is correct
41 Correct 10 ms 9848 KB Output is correct
42 Correct 7 ms 9940 KB Output is correct
43 Correct 15 ms 9964 KB Output is correct
44 Correct 6 ms 9712 KB Output is correct
45 Correct 8 ms 9940 KB Output is correct
46 Correct 8 ms 9976 KB Output is correct
47 Correct 6 ms 9684 KB Output is correct
48 Correct 7 ms 9848 KB Output is correct
49 Correct 7 ms 10180 KB Output is correct
50 Correct 7 ms 10232 KB Output is correct
51 Correct 8 ms 9940 KB Output is correct
52 Correct 6 ms 9848 KB Output is correct
53 Correct 8 ms 9972 KB Output is correct
54 Correct 8 ms 9812 KB Output is correct
55 Correct 5 ms 9708 KB Output is correct
56 Correct 113 ms 16316 KB Output is correct
57 Correct 101 ms 15976 KB Output is correct
58 Correct 9 ms 9812 KB Output is correct
59 Correct 4 ms 9712 KB Output is correct
60 Correct 6 ms 9628 KB Output is correct
61 Correct 117 ms 15940 KB Output is correct
62 Correct 7 ms 9928 KB Output is correct
63 Execution timed out 3060 ms 20292 KB Time limit exceeded
64 Halted 0 ms 0 KB -