답안 #894929

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894929 2023-12-29T08:53:21 Z vjudge1 Mergers (JOI19_mergers) C++14
100 / 100
761 ms 154444 KB
// Parsa Jokar 2023 https://github.com/phictus/ioi

#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdint>

using namespace std;

constexpr int64_t maxn = 500001;
int64_t n, k, state[maxn], currentTime = 1, discovery[maxn], depth[maxn], parent[maxn];
vector<int64_t> adj[maxn], cities[maxn];
set<int64_t> adjStates[maxn];

class Dsu
{
public:
    Dsu()
    {
        for (int64_t i = 1; i <= maxn; i++)
            _parent[i] = i;
    }

    int64_t FindSet(int64_t v)
    {
        if (_parent[v] == v)
            return v;

        return _parent[v] = FindSet(_parent[v]);
    }

    void Unite(int64_t v, int64_t u)
    {
        v = FindSet(v);
        u = FindSet(u);
        if (v == u)
            return;

        _parent[v] = u;
    }

private:
    int64_t _parent[maxn];
} dsu;

void Init(int64_t v = 1, int64_t p = 0)
{
    discovery[v] = currentTime++;
    depth[v] = depth[p] + 1;
    parent[v] = p;

    for (int64_t u : adj[v])
    {
        if (u != p)
            Init(u, v);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);

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

    Init();

    for (int64_t i = 1; i <= k; i++)
    {
        sort(cities[i].begin(), cities[i].end(), [&](int64_t v, int64_t u) { return discovery[v] < discovery[u]; });
        for (int64_t j = 0; j + 1 < cities[i].size(); j++)
        {
            int64_t v = cities[i][j], u = cities[i][j + 1];
            if (depth[v] < depth[u])
                swap(v, u);

            while (depth[v] > depth[u])
            {
                dsu.Unite(state[v], state[parent[v]]);
                v = parent[v];
            }

            while (v != u)
            {
                dsu.Unite(state[v], state[parent[v]]);
                v = parent[v];

                dsu.Unite(state[u], state[parent[u]]);
                u = parent[u];
            }
        }
    }

    for (int64_t v = 1; v <= n; v++)
        state[v] = dsu.FindSet(state[v]);

    for (int64_t v = 1; v <= n; v++)
    {
        for (int64_t u : adj[v])
        {
            if (state[v] != state[u])
                adjStates[state[v]].insert(state[u]);
        }
    }

    int64_t result = 0;
    for (int64_t i = 1; i <= k; i++)
    {
        if (adjStates[i].size() == 1)
            result++;
    }
    cout << (result + 1) / 2 << '\n';

    return (0 ^ 0);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:85:35: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int64_t j = 0; j + 1 < cities[i].size(); j++)
      |                             ~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 55900 KB Output is correct
2 Correct 13 ms 56008 KB Output is correct
3 Correct 13 ms 55900 KB Output is correct
4 Correct 13 ms 55900 KB Output is correct
5 Correct 14 ms 55820 KB Output is correct
6 Correct 15 ms 55820 KB Output is correct
7 Correct 15 ms 55900 KB Output is correct
8 Correct 14 ms 55900 KB Output is correct
9 Correct 14 ms 55900 KB Output is correct
10 Correct 13 ms 55900 KB Output is correct
11 Correct 13 ms 56044 KB Output is correct
12 Correct 13 ms 56152 KB Output is correct
13 Correct 13 ms 56048 KB Output is correct
14 Correct 15 ms 56156 KB Output is correct
15 Correct 13 ms 56032 KB Output is correct
16 Correct 13 ms 55900 KB Output is correct
17 Correct 13 ms 56044 KB Output is correct
18 Correct 15 ms 56048 KB Output is correct
19 Correct 15 ms 56152 KB Output is correct
20 Correct 13 ms 56044 KB Output is correct
21 Correct 13 ms 55896 KB Output is correct
22 Correct 14 ms 55900 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 55900 KB Output is correct
2 Correct 13 ms 56008 KB Output is correct
3 Correct 13 ms 55900 KB Output is correct
4 Correct 13 ms 55900 KB Output is correct
5 Correct 14 ms 55820 KB Output is correct
6 Correct 15 ms 55820 KB Output is correct
7 Correct 15 ms 55900 KB Output is correct
8 Correct 14 ms 55900 KB Output is correct
9 Correct 14 ms 55900 KB Output is correct
10 Correct 13 ms 55900 KB Output is correct
11 Correct 13 ms 56044 KB Output is correct
12 Correct 13 ms 56152 KB Output is correct
13 Correct 13 ms 56048 KB Output is correct
14 Correct 15 ms 56156 KB Output is correct
15 Correct 13 ms 56032 KB Output is correct
16 Correct 13 ms 55900 KB Output is correct
17 Correct 13 ms 56044 KB Output is correct
18 Correct 15 ms 56048 KB Output is correct
19 Correct 15 ms 56152 KB Output is correct
20 Correct 13 ms 56044 KB Output is correct
21 Correct 13 ms 55896 KB Output is correct
22 Correct 14 ms 55900 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55896 KB Output is correct
25 Correct 13 ms 55896 KB Output is correct
26 Correct 15 ms 56156 KB Output is correct
27 Correct 14 ms 56152 KB Output is correct
28 Correct 15 ms 56408 KB Output is correct
29 Correct 16 ms 56552 KB Output is correct
30 Correct 14 ms 56152 KB Output is correct
31 Correct 13 ms 55900 KB Output is correct
32 Correct 14 ms 56364 KB Output is correct
33 Correct 14 ms 55900 KB Output is correct
34 Correct 14 ms 56156 KB Output is correct
35 Correct 15 ms 56412 KB Output is correct
36 Correct 14 ms 56156 KB Output is correct
37 Correct 15 ms 56156 KB Output is correct
38 Correct 13 ms 55996 KB Output is correct
39 Correct 15 ms 56152 KB Output is correct
40 Correct 14 ms 56156 KB Output is correct
41 Correct 14 ms 56156 KB Output is correct
42 Correct 14 ms 56304 KB Output is correct
43 Correct 14 ms 56304 KB Output is correct
44 Correct 14 ms 55896 KB Output is correct
45 Correct 15 ms 56156 KB Output is correct
46 Correct 14 ms 56152 KB Output is correct
47 Correct 13 ms 55896 KB Output is correct
48 Correct 14 ms 56156 KB Output is correct
49 Correct 15 ms 56564 KB Output is correct
50 Correct 15 ms 56492 KB Output is correct
51 Correct 14 ms 56156 KB Output is correct
52 Correct 13 ms 56156 KB Output is correct
53 Correct 14 ms 56056 KB Output is correct
54 Correct 16 ms 56152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 55900 KB Output is correct
2 Correct 13 ms 56008 KB Output is correct
3 Correct 13 ms 55900 KB Output is correct
4 Correct 13 ms 55900 KB Output is correct
5 Correct 14 ms 55820 KB Output is correct
6 Correct 15 ms 55820 KB Output is correct
7 Correct 15 ms 55900 KB Output is correct
8 Correct 14 ms 55900 KB Output is correct
9 Correct 14 ms 55900 KB Output is correct
10 Correct 13 ms 55900 KB Output is correct
11 Correct 13 ms 56044 KB Output is correct
12 Correct 13 ms 56152 KB Output is correct
13 Correct 13 ms 56048 KB Output is correct
14 Correct 15 ms 56156 KB Output is correct
15 Correct 13 ms 56032 KB Output is correct
16 Correct 13 ms 55900 KB Output is correct
17 Correct 13 ms 56044 KB Output is correct
18 Correct 15 ms 56048 KB Output is correct
19 Correct 15 ms 56152 KB Output is correct
20 Correct 13 ms 56044 KB Output is correct
21 Correct 13 ms 55896 KB Output is correct
22 Correct 14 ms 55900 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55896 KB Output is correct
25 Correct 13 ms 55900 KB Output is correct
26 Correct 50 ms 65936 KB Output is correct
27 Correct 62 ms 65620 KB Output is correct
28 Correct 14 ms 56156 KB Output is correct
29 Correct 13 ms 55900 KB Output is correct
30 Correct 13 ms 56036 KB Output is correct
31 Correct 60 ms 65604 KB Output is correct
32 Correct 14 ms 56152 KB Output is correct
33 Correct 131 ms 67636 KB Output is correct
34 Correct 64 ms 65732 KB Output is correct
35 Correct 14 ms 56156 KB Output is correct
36 Correct 56 ms 65224 KB Output is correct
37 Correct 14 ms 56244 KB Output is correct
38 Correct 14 ms 56156 KB Output is correct
39 Correct 49 ms 65896 KB Output is correct
40 Correct 16 ms 56152 KB Output is correct
41 Correct 60 ms 65488 KB Output is correct
42 Correct 109 ms 65932 KB Output is correct
43 Correct 13 ms 55900 KB Output is correct
44 Correct 59 ms 67500 KB Output is correct
45 Correct 123 ms 67684 KB Output is correct
46 Correct 14 ms 56156 KB Output is correct
47 Correct 14 ms 56156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 65744 KB Output is correct
2 Correct 102 ms 77184 KB Output is correct
3 Correct 14 ms 56156 KB Output is correct
4 Correct 14 ms 56028 KB Output is correct
5 Correct 13 ms 55900 KB Output is correct
6 Correct 14 ms 55900 KB Output is correct
7 Correct 16 ms 56152 KB Output is correct
8 Correct 75 ms 67412 KB Output is correct
9 Correct 13 ms 56152 KB Output is correct
10 Correct 60 ms 65620 KB Output is correct
11 Correct 13 ms 55896 KB Output is correct
12 Correct 55 ms 65108 KB Output is correct
13 Correct 77 ms 68268 KB Output is correct
14 Correct 91 ms 77136 KB Output is correct
15 Correct 49 ms 65868 KB Output is correct
16 Correct 16 ms 56156 KB Output is correct
17 Correct 13 ms 55900 KB Output is correct
18 Correct 94 ms 75216 KB Output is correct
19 Correct 94 ms 77764 KB Output is correct
20 Correct 14 ms 56152 KB Output is correct
21 Correct 13 ms 55900 KB Output is correct
22 Correct 61 ms 68220 KB Output is correct
23 Correct 14 ms 56156 KB Output is correct
24 Correct 63 ms 66644 KB Output is correct
25 Correct 107 ms 78416 KB Output is correct
26 Correct 15 ms 56408 KB Output is correct
27 Correct 14 ms 56580 KB Output is correct
28 Correct 14 ms 56048 KB Output is correct
29 Correct 14 ms 56096 KB Output is correct
30 Correct 14 ms 56156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 55900 KB Output is correct
2 Correct 13 ms 56008 KB Output is correct
3 Correct 13 ms 55900 KB Output is correct
4 Correct 13 ms 55900 KB Output is correct
5 Correct 14 ms 55820 KB Output is correct
6 Correct 15 ms 55820 KB Output is correct
7 Correct 15 ms 55900 KB Output is correct
8 Correct 14 ms 55900 KB Output is correct
9 Correct 14 ms 55900 KB Output is correct
10 Correct 13 ms 55900 KB Output is correct
11 Correct 13 ms 56044 KB Output is correct
12 Correct 13 ms 56152 KB Output is correct
13 Correct 13 ms 56048 KB Output is correct
14 Correct 15 ms 56156 KB Output is correct
15 Correct 13 ms 56032 KB Output is correct
16 Correct 13 ms 55900 KB Output is correct
17 Correct 13 ms 56044 KB Output is correct
18 Correct 15 ms 56048 KB Output is correct
19 Correct 15 ms 56152 KB Output is correct
20 Correct 13 ms 56044 KB Output is correct
21 Correct 13 ms 55896 KB Output is correct
22 Correct 14 ms 55900 KB Output is correct
23 Correct 13 ms 55900 KB Output is correct
24 Correct 13 ms 55896 KB Output is correct
25 Correct 13 ms 55896 KB Output is correct
26 Correct 15 ms 56156 KB Output is correct
27 Correct 14 ms 56152 KB Output is correct
28 Correct 15 ms 56408 KB Output is correct
29 Correct 16 ms 56552 KB Output is correct
30 Correct 14 ms 56152 KB Output is correct
31 Correct 13 ms 55900 KB Output is correct
32 Correct 14 ms 56364 KB Output is correct
33 Correct 14 ms 55900 KB Output is correct
34 Correct 14 ms 56156 KB Output is correct
35 Correct 15 ms 56412 KB Output is correct
36 Correct 14 ms 56156 KB Output is correct
37 Correct 15 ms 56156 KB Output is correct
38 Correct 13 ms 55996 KB Output is correct
39 Correct 15 ms 56152 KB Output is correct
40 Correct 14 ms 56156 KB Output is correct
41 Correct 14 ms 56156 KB Output is correct
42 Correct 14 ms 56304 KB Output is correct
43 Correct 14 ms 56304 KB Output is correct
44 Correct 14 ms 55896 KB Output is correct
45 Correct 15 ms 56156 KB Output is correct
46 Correct 14 ms 56152 KB Output is correct
47 Correct 13 ms 55896 KB Output is correct
48 Correct 14 ms 56156 KB Output is correct
49 Correct 15 ms 56564 KB Output is correct
50 Correct 15 ms 56492 KB Output is correct
51 Correct 14 ms 56156 KB Output is correct
52 Correct 13 ms 56156 KB Output is correct
53 Correct 14 ms 56056 KB Output is correct
54 Correct 16 ms 56152 KB Output is correct
55 Correct 13 ms 55900 KB Output is correct
56 Correct 50 ms 65936 KB Output is correct
57 Correct 62 ms 65620 KB Output is correct
58 Correct 14 ms 56156 KB Output is correct
59 Correct 13 ms 55900 KB Output is correct
60 Correct 13 ms 56036 KB Output is correct
61 Correct 60 ms 65604 KB Output is correct
62 Correct 14 ms 56152 KB Output is correct
63 Correct 131 ms 67636 KB Output is correct
64 Correct 64 ms 65732 KB Output is correct
65 Correct 14 ms 56156 KB Output is correct
66 Correct 56 ms 65224 KB Output is correct
67 Correct 14 ms 56244 KB Output is correct
68 Correct 14 ms 56156 KB Output is correct
69 Correct 49 ms 65896 KB Output is correct
70 Correct 16 ms 56152 KB Output is correct
71 Correct 60 ms 65488 KB Output is correct
72 Correct 109 ms 65932 KB Output is correct
73 Correct 13 ms 55900 KB Output is correct
74 Correct 59 ms 67500 KB Output is correct
75 Correct 123 ms 67684 KB Output is correct
76 Correct 14 ms 56156 KB Output is correct
77 Correct 14 ms 56156 KB Output is correct
78 Correct 49 ms 65744 KB Output is correct
79 Correct 102 ms 77184 KB Output is correct
80 Correct 14 ms 56156 KB Output is correct
81 Correct 14 ms 56028 KB Output is correct
82 Correct 13 ms 55900 KB Output is correct
83 Correct 14 ms 55900 KB Output is correct
84 Correct 16 ms 56152 KB Output is correct
85 Correct 75 ms 67412 KB Output is correct
86 Correct 13 ms 56152 KB Output is correct
87 Correct 60 ms 65620 KB Output is correct
88 Correct 13 ms 55896 KB Output is correct
89 Correct 55 ms 65108 KB Output is correct
90 Correct 77 ms 68268 KB Output is correct
91 Correct 91 ms 77136 KB Output is correct
92 Correct 49 ms 65868 KB Output is correct
93 Correct 16 ms 56156 KB Output is correct
94 Correct 13 ms 55900 KB Output is correct
95 Correct 94 ms 75216 KB Output is correct
96 Correct 94 ms 77764 KB Output is correct
97 Correct 14 ms 56152 KB Output is correct
98 Correct 13 ms 55900 KB Output is correct
99 Correct 61 ms 68220 KB Output is correct
100 Correct 14 ms 56156 KB Output is correct
101 Correct 63 ms 66644 KB Output is correct
102 Correct 107 ms 78416 KB Output is correct
103 Correct 15 ms 56408 KB Output is correct
104 Correct 14 ms 56580 KB Output is correct
105 Correct 14 ms 56048 KB Output is correct
106 Correct 14 ms 56096 KB Output is correct
107 Correct 14 ms 56156 KB Output is correct
108 Correct 392 ms 99400 KB Output is correct
109 Correct 423 ms 93020 KB Output is correct
110 Correct 473 ms 97004 KB Output is correct
111 Correct 690 ms 150100 KB Output is correct
112 Correct 761 ms 132260 KB Output is correct
113 Correct 567 ms 138504 KB Output is correct
114 Correct 439 ms 89904 KB Output is correct
115 Correct 405 ms 89556 KB Output is correct
116 Correct 579 ms 93092 KB Output is correct
117 Correct 569 ms 148280 KB Output is correct
118 Correct 367 ms 91236 KB Output is correct
119 Correct 633 ms 148308 KB Output is correct
120 Correct 681 ms 154444 KB Output is correct
121 Correct 574 ms 147952 KB Output is correct
122 Correct 650 ms 104000 KB Output is correct
123 Correct 637 ms 149176 KB Output is correct
124 Correct 628 ms 127668 KB Output is correct