Submission #718957

# Submission time Handle Problem Language Result Execution time Memory
718957 2023-04-05T07:49:55 Z walterw Mergers (JOI19_mergers) C++17
100 / 100
669 ms 91888 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

vector<int> adj[500005];
int par[500005];
vector<int> group[500005];
int depth[500005];

void dfs(int node, int parr = -1) {
    for (auto nxt : adj[node]) {
        if (nxt == parr) continue;

        par[nxt] = node;
        depth[nxt] = depth[node] + 1;
        dfs(nxt, node);
    }
}

int pr[500005];
int deg[500005];

int findset(int i) {
    if (pr[i] == i) return i;
    return pr[i] = findset(pr[i]);
}

void merge(int a, int b) {
    while (a != b) {
        if (depth[a] < depth[b]) swap(a, b);
        pr[a] = findset(par[a]);
        a = pr[a];
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, k; cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) pr[i] = i;

    for (int i = 1; i <= n; i++) {
        int temp; cin >> temp;
        group[temp].push_back(i);
    }

    depth[1] = 1;
    dfs(1);

    for (int i = 1; i <= n; i++) {
        if (group[i].empty()) continue;

        for (int j = 1; j < group[i].size(); j++) {
            merge(group[i][0], group[i][j]);
        }
    }

    for (int i = 1; i <= n; i++) {
        for (auto j : adj[i]) {
            if (findset(i) != findset(j)) {
                deg[findset(i)]++;
                deg[findset(j)]++;
            }
        }
    }

    int ct = 0;

    for (int i = 1; i <= n; i++) {
        if (deg[i] == 2) ct++;
    }
    cout << (ct + 1) / 2;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:88:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for (int j = 1; j < group[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 14 ms 23812 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 12 ms 23740 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 12 ms 23812 KB Output is correct
12 Correct 14 ms 23796 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 13 ms 23820 KB Output is correct
15 Correct 13 ms 23816 KB Output is correct
16 Correct 12 ms 23820 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23824 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 15 ms 23820 KB Output is correct
24 Correct 11 ms 23824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 14 ms 23812 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 12 ms 23740 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 12 ms 23812 KB Output is correct
12 Correct 14 ms 23796 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 13 ms 23820 KB Output is correct
15 Correct 13 ms 23816 KB Output is correct
16 Correct 12 ms 23820 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23824 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 15 ms 23820 KB Output is correct
24 Correct 11 ms 23824 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 13 ms 24020 KB Output is correct
28 Correct 15 ms 24120 KB Output is correct
29 Correct 14 ms 24020 KB Output is correct
30 Correct 17 ms 24052 KB Output is correct
31 Correct 13 ms 23820 KB Output is correct
32 Correct 14 ms 24220 KB Output is correct
33 Correct 14 ms 23816 KB Output is correct
34 Correct 16 ms 24024 KB Output is correct
35 Correct 13 ms 24120 KB Output is correct
36 Correct 13 ms 24004 KB Output is correct
37 Correct 14 ms 24088 KB Output is correct
38 Correct 14 ms 23764 KB Output is correct
39 Correct 14 ms 24036 KB Output is correct
40 Correct 15 ms 23988 KB Output is correct
41 Correct 14 ms 23960 KB Output is correct
42 Correct 14 ms 24020 KB Output is correct
43 Correct 15 ms 24220 KB Output is correct
44 Correct 14 ms 23820 KB Output is correct
45 Correct 13 ms 24088 KB Output is correct
46 Correct 14 ms 24020 KB Output is correct
47 Correct 12 ms 23824 KB Output is correct
48 Correct 14 ms 24012 KB Output is correct
49 Correct 13 ms 24068 KB Output is correct
50 Correct 13 ms 24148 KB Output is correct
51 Correct 15 ms 23956 KB Output is correct
52 Correct 13 ms 23960 KB Output is correct
53 Correct 13 ms 24084 KB Output is correct
54 Correct 13 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 14 ms 23812 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 12 ms 23740 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 12 ms 23812 KB Output is correct
12 Correct 14 ms 23796 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 13 ms 23820 KB Output is correct
15 Correct 13 ms 23816 KB Output is correct
16 Correct 12 ms 23820 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23824 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 15 ms 23820 KB Output is correct
24 Correct 11 ms 23824 KB Output is correct
25 Correct 12 ms 23816 KB Output is correct
26 Correct 50 ms 30532 KB Output is correct
27 Correct 78 ms 30184 KB Output is correct
28 Correct 15 ms 24020 KB Output is correct
29 Correct 12 ms 23728 KB Output is correct
30 Correct 12 ms 23816 KB Output is correct
31 Correct 67 ms 30232 KB Output is correct
32 Correct 13 ms 23988 KB Output is correct
33 Correct 76 ms 35912 KB Output is correct
34 Correct 66 ms 30192 KB Output is correct
35 Correct 13 ms 23972 KB Output is correct
36 Correct 70 ms 30848 KB Output is correct
37 Correct 13 ms 23956 KB Output is correct
38 Correct 13 ms 24020 KB Output is correct
39 Correct 50 ms 30356 KB Output is correct
40 Correct 14 ms 24148 KB Output is correct
41 Correct 67 ms 30008 KB Output is correct
42 Correct 82 ms 31948 KB Output is correct
43 Correct 13 ms 23820 KB Output is correct
44 Correct 78 ms 36036 KB Output is correct
45 Correct 70 ms 33196 KB Output is correct
46 Correct 13 ms 24020 KB Output is correct
47 Correct 15 ms 24000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 30492 KB Output is correct
2 Correct 67 ms 33720 KB Output is correct
3 Correct 14 ms 24020 KB Output is correct
4 Correct 14 ms 24048 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 14 ms 23800 KB Output is correct
7 Correct 13 ms 24036 KB Output is correct
8 Correct 76 ms 31884 KB Output is correct
9 Correct 13 ms 23956 KB Output is correct
10 Correct 67 ms 30668 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
12 Correct 105 ms 30608 KB Output is correct
13 Correct 91 ms 31732 KB Output is correct
14 Correct 91 ms 33392 KB Output is correct
15 Correct 59 ms 30436 KB Output is correct
16 Correct 18 ms 24020 KB Output is correct
17 Correct 18 ms 23812 KB Output is correct
18 Correct 86 ms 33456 KB Output is correct
19 Correct 107 ms 36600 KB Output is correct
20 Correct 15 ms 23944 KB Output is correct
21 Correct 12 ms 23816 KB Output is correct
22 Correct 61 ms 32128 KB Output is correct
23 Correct 13 ms 23956 KB Output is correct
24 Correct 83 ms 31080 KB Output is correct
25 Correct 95 ms 35532 KB Output is correct
26 Correct 13 ms 24100 KB Output is correct
27 Correct 14 ms 24068 KB Output is correct
28 Correct 13 ms 23960 KB Output is correct
29 Correct 12 ms 24008 KB Output is correct
30 Correct 13 ms 23944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 14 ms 23812 KB Output is correct
3 Correct 13 ms 23796 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 12 ms 23740 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 12 ms 23812 KB Output is correct
12 Correct 14 ms 23796 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 13 ms 23820 KB Output is correct
15 Correct 13 ms 23816 KB Output is correct
16 Correct 12 ms 23820 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 12 ms 23824 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 13 ms 23764 KB Output is correct
23 Correct 15 ms 23820 KB Output is correct
24 Correct 11 ms 23824 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 13 ms 24020 KB Output is correct
27 Correct 13 ms 24020 KB Output is correct
28 Correct 15 ms 24120 KB Output is correct
29 Correct 14 ms 24020 KB Output is correct
30 Correct 17 ms 24052 KB Output is correct
31 Correct 13 ms 23820 KB Output is correct
32 Correct 14 ms 24220 KB Output is correct
33 Correct 14 ms 23816 KB Output is correct
34 Correct 16 ms 24024 KB Output is correct
35 Correct 13 ms 24120 KB Output is correct
36 Correct 13 ms 24004 KB Output is correct
37 Correct 14 ms 24088 KB Output is correct
38 Correct 14 ms 23764 KB Output is correct
39 Correct 14 ms 24036 KB Output is correct
40 Correct 15 ms 23988 KB Output is correct
41 Correct 14 ms 23960 KB Output is correct
42 Correct 14 ms 24020 KB Output is correct
43 Correct 15 ms 24220 KB Output is correct
44 Correct 14 ms 23820 KB Output is correct
45 Correct 13 ms 24088 KB Output is correct
46 Correct 14 ms 24020 KB Output is correct
47 Correct 12 ms 23824 KB Output is correct
48 Correct 14 ms 24012 KB Output is correct
49 Correct 13 ms 24068 KB Output is correct
50 Correct 13 ms 24148 KB Output is correct
51 Correct 15 ms 23956 KB Output is correct
52 Correct 13 ms 23960 KB Output is correct
53 Correct 13 ms 24084 KB Output is correct
54 Correct 13 ms 24020 KB Output is correct
55 Correct 12 ms 23816 KB Output is correct
56 Correct 50 ms 30532 KB Output is correct
57 Correct 78 ms 30184 KB Output is correct
58 Correct 15 ms 24020 KB Output is correct
59 Correct 12 ms 23728 KB Output is correct
60 Correct 12 ms 23816 KB Output is correct
61 Correct 67 ms 30232 KB Output is correct
62 Correct 13 ms 23988 KB Output is correct
63 Correct 76 ms 35912 KB Output is correct
64 Correct 66 ms 30192 KB Output is correct
65 Correct 13 ms 23972 KB Output is correct
66 Correct 70 ms 30848 KB Output is correct
67 Correct 13 ms 23956 KB Output is correct
68 Correct 13 ms 24020 KB Output is correct
69 Correct 50 ms 30356 KB Output is correct
70 Correct 14 ms 24148 KB Output is correct
71 Correct 67 ms 30008 KB Output is correct
72 Correct 82 ms 31948 KB Output is correct
73 Correct 13 ms 23820 KB Output is correct
74 Correct 78 ms 36036 KB Output is correct
75 Correct 70 ms 33196 KB Output is correct
76 Correct 13 ms 24020 KB Output is correct
77 Correct 15 ms 24000 KB Output is correct
78 Correct 54 ms 30492 KB Output is correct
79 Correct 67 ms 33720 KB Output is correct
80 Correct 14 ms 24020 KB Output is correct
81 Correct 14 ms 24048 KB Output is correct
82 Correct 12 ms 23764 KB Output is correct
83 Correct 14 ms 23800 KB Output is correct
84 Correct 13 ms 24036 KB Output is correct
85 Correct 76 ms 31884 KB Output is correct
86 Correct 13 ms 23956 KB Output is correct
87 Correct 67 ms 30668 KB Output is correct
88 Correct 14 ms 23732 KB Output is correct
89 Correct 105 ms 30608 KB Output is correct
90 Correct 91 ms 31732 KB Output is correct
91 Correct 91 ms 33392 KB Output is correct
92 Correct 59 ms 30436 KB Output is correct
93 Correct 18 ms 24020 KB Output is correct
94 Correct 18 ms 23812 KB Output is correct
95 Correct 86 ms 33456 KB Output is correct
96 Correct 107 ms 36600 KB Output is correct
97 Correct 15 ms 23944 KB Output is correct
98 Correct 12 ms 23816 KB Output is correct
99 Correct 61 ms 32128 KB Output is correct
100 Correct 13 ms 23956 KB Output is correct
101 Correct 83 ms 31080 KB Output is correct
102 Correct 95 ms 35532 KB Output is correct
103 Correct 13 ms 24100 KB Output is correct
104 Correct 14 ms 24068 KB Output is correct
105 Correct 13 ms 23960 KB Output is correct
106 Correct 12 ms 24008 KB Output is correct
107 Correct 13 ms 23944 KB Output is correct
108 Correct 395 ms 65588 KB Output is correct
109 Correct 531 ms 69852 KB Output is correct
110 Correct 546 ms 78840 KB Output is correct
111 Correct 669 ms 91888 KB Output is correct
112 Correct 635 ms 83984 KB Output is correct
113 Correct 423 ms 73508 KB Output is correct
114 Correct 461 ms 55596 KB Output is correct
115 Correct 465 ms 55572 KB Output is correct
116 Correct 543 ms 61948 KB Output is correct
117 Correct 625 ms 73556 KB Output is correct
118 Correct 497 ms 58496 KB Output is correct
119 Correct 567 ms 73428 KB Output is correct
120 Correct 659 ms 84512 KB Output is correct
121 Correct 578 ms 73484 KB Output is correct
122 Correct 614 ms 67116 KB Output is correct
123 Correct 410 ms 74996 KB Output is correct
124 Correct 619 ms 72156 KB Output is correct