Submission #567853

# Submission time Handle Problem Language Result Execution time Memory
567853 2022-05-24T09:25:43 Z stevancv Mergers (JOI19_mergers) C++14
100 / 100
1070 ms 135600 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 5e5 + 2;
int mod = 1000000007;
vector<int> g[N];
int lift[N][19], in[N], out[N], tsz;
void Dfs(int s, int e) {
    in[s] = ++tsz;
    lift[s][0] = e;
    for (int i = 1; i < 19; i++) lift[s][i] = lift[lift[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u != e) {
            Dfs(u, s);
        }
    }
    out[s] = tsz;
}
bool Ancestor(int a, int b) {
    return in[a] <= in[b] && out[b] <= out[a];
}
int Lca(int a, int b) {
    if (Ancestor(a, b)) return a;
    if (Ancestor(b, a)) return b;
    for (int i = 18; i >= 0; i--) {
        if (lift[a][i] != -1 && !Ancestor(lift[a][i], b)) a = lift[a][i];
    }
    return lift[a][0];
}
int sum[N], sz[N], cnt[N];
void Dfs1(int s, int e) {
    sz[s] = 1;
    for (int u : g[s]) {
        if (u != e) {
            Dfs1(u, s);
            sz[s] += sz[u];
            sum[s] += sum[u];
        }
    }
}
void Dfs2(int s, int e, int p) {
    if (sum[s] == sz[s]) {
        if (p != -1) {
            cnt[p]++;
            cnt[s]++;
        }
        p = s;
    }
    for (int u : g[s]) {
        if (u != e) {
            Dfs2(u, s, p);
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u - 1].push_back(v - 1);
        g[v - 1].push_back(u - 1);
    }
    Dfs(0, -1);
    vector<vector<int>> pos(k);
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        x -= 1;
        pos[x].push_back(i);
    }
    for (int i = 0; i < k; i++) {
        int tr = pos[i][0];
        for (int j = 0; j < pos[i].size(); j++) {
            tr = Lca(tr, pos[i][j]);
        }
        sum[tr] += pos[i].size();
    }
    Dfs1(0, -1);
    Dfs2(0, -1, -1);
    int ans = 0;
    for (int i = 0; i < n; i++) if (cnt[i] == 1) ans++;
    cout << (ans + 1) / 2 << en;
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int j = 0; j < pos[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12104 KB Output is correct
2 Correct 7 ms 12080 KB Output is correct
3 Correct 7 ms 12076 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 7 ms 12076 KB Output is correct
6 Correct 8 ms 12036 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 9 ms 12116 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 8 ms 12068 KB Output is correct
16 Correct 7 ms 12068 KB Output is correct
17 Correct 8 ms 12024 KB Output is correct
18 Correct 8 ms 12116 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 8 ms 12068 KB Output is correct
21 Correct 8 ms 12068 KB Output is correct
22 Correct 10 ms 12068 KB Output is correct
23 Correct 8 ms 11988 KB Output is correct
24 Correct 9 ms 12068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12104 KB Output is correct
2 Correct 7 ms 12080 KB Output is correct
3 Correct 7 ms 12076 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 7 ms 12076 KB Output is correct
6 Correct 8 ms 12036 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 9 ms 12116 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 8 ms 12068 KB Output is correct
16 Correct 7 ms 12068 KB Output is correct
17 Correct 8 ms 12024 KB Output is correct
18 Correct 8 ms 12116 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 8 ms 12068 KB Output is correct
21 Correct 8 ms 12068 KB Output is correct
22 Correct 10 ms 12068 KB Output is correct
23 Correct 8 ms 11988 KB Output is correct
24 Correct 9 ms 12068 KB Output is correct
25 Correct 8 ms 12104 KB Output is correct
26 Correct 10 ms 12628 KB Output is correct
27 Correct 9 ms 12476 KB Output is correct
28 Correct 9 ms 12756 KB Output is correct
29 Correct 9 ms 12628 KB Output is correct
30 Correct 9 ms 12500 KB Output is correct
31 Correct 7 ms 12004 KB Output is correct
32 Correct 9 ms 12852 KB Output is correct
33 Correct 8 ms 12116 KB Output is correct
34 Correct 10 ms 12468 KB Output is correct
35 Correct 11 ms 12672 KB Output is correct
36 Correct 9 ms 12480 KB Output is correct
37 Correct 9 ms 12536 KB Output is correct
38 Correct 8 ms 12116 KB Output is correct
39 Correct 9 ms 12596 KB Output is correct
40 Correct 9 ms 12464 KB Output is correct
41 Correct 9 ms 12500 KB Output is correct
42 Correct 11 ms 12592 KB Output is correct
43 Correct 11 ms 12764 KB Output is correct
44 Correct 9 ms 12116 KB Output is correct
45 Correct 9 ms 12724 KB Output is correct
46 Correct 10 ms 12596 KB Output is correct
47 Correct 9 ms 12116 KB Output is correct
48 Correct 9 ms 12500 KB Output is correct
49 Correct 9 ms 12628 KB Output is correct
50 Correct 13 ms 12756 KB Output is correct
51 Correct 11 ms 12500 KB Output is correct
52 Correct 9 ms 12468 KB Output is correct
53 Correct 9 ms 12592 KB Output is correct
54 Correct 10 ms 12576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12104 KB Output is correct
2 Correct 7 ms 12080 KB Output is correct
3 Correct 7 ms 12076 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 7 ms 12076 KB Output is correct
6 Correct 8 ms 12036 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 9 ms 12116 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 8 ms 12068 KB Output is correct
16 Correct 7 ms 12068 KB Output is correct
17 Correct 8 ms 12024 KB Output is correct
18 Correct 8 ms 12116 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 8 ms 12068 KB Output is correct
21 Correct 8 ms 12068 KB Output is correct
22 Correct 10 ms 12068 KB Output is correct
23 Correct 8 ms 11988 KB Output is correct
24 Correct 9 ms 12068 KB Output is correct
25 Correct 9 ms 12116 KB Output is correct
26 Correct 82 ms 26144 KB Output is correct
27 Correct 99 ms 26308 KB Output is correct
28 Correct 11 ms 12564 KB Output is correct
29 Correct 8 ms 12116 KB Output is correct
30 Correct 8 ms 12076 KB Output is correct
31 Correct 130 ms 26348 KB Output is correct
32 Correct 9 ms 12468 KB Output is correct
33 Correct 136 ms 33436 KB Output is correct
34 Correct 104 ms 26316 KB Output is correct
35 Correct 9 ms 12472 KB Output is correct
36 Correct 115 ms 27100 KB Output is correct
37 Correct 10 ms 12536 KB Output is correct
38 Correct 9 ms 12500 KB Output is correct
39 Correct 79 ms 26092 KB Output is correct
40 Correct 9 ms 12716 KB Output is correct
41 Correct 102 ms 26116 KB Output is correct
42 Correct 121 ms 28648 KB Output is correct
43 Correct 8 ms 12068 KB Output is correct
44 Correct 143 ms 33592 KB Output is correct
45 Correct 126 ms 30036 KB Output is correct
46 Correct 11 ms 12500 KB Output is correct
47 Correct 9 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 24800 KB Output is correct
2 Correct 100 ms 30436 KB Output is correct
3 Correct 9 ms 12500 KB Output is correct
4 Correct 13 ms 12524 KB Output is correct
5 Correct 8 ms 12072 KB Output is correct
6 Correct 7 ms 12116 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 156 ms 29212 KB Output is correct
9 Correct 11 ms 12532 KB Output is correct
10 Correct 114 ms 26860 KB Output is correct
11 Correct 7 ms 12068 KB Output is correct
12 Correct 114 ms 26856 KB Output is correct
13 Correct 144 ms 29092 KB Output is correct
14 Correct 108 ms 31944 KB Output is correct
15 Correct 59 ms 26148 KB Output is correct
16 Correct 12 ms 12628 KB Output is correct
17 Correct 7 ms 12112 KB Output is correct
18 Correct 92 ms 31696 KB Output is correct
19 Correct 177 ms 35784 KB Output is correct
20 Correct 12 ms 12500 KB Output is correct
21 Correct 7 ms 12064 KB Output is correct
22 Correct 84 ms 29388 KB Output is correct
23 Correct 8 ms 12500 KB Output is correct
24 Correct 132 ms 27408 KB Output is correct
25 Correct 153 ms 34404 KB Output is correct
26 Correct 8 ms 12640 KB Output is correct
27 Correct 10 ms 12660 KB Output is correct
28 Correct 8 ms 12468 KB Output is correct
29 Correct 8 ms 12500 KB Output is correct
30 Correct 8 ms 12600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12104 KB Output is correct
2 Correct 7 ms 12080 KB Output is correct
3 Correct 7 ms 12076 KB Output is correct
4 Correct 7 ms 11988 KB Output is correct
5 Correct 7 ms 12076 KB Output is correct
6 Correct 8 ms 12036 KB Output is correct
7 Correct 7 ms 11988 KB Output is correct
8 Correct 7 ms 12116 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 9 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 9 ms 12116 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 8 ms 12068 KB Output is correct
16 Correct 7 ms 12068 KB Output is correct
17 Correct 8 ms 12024 KB Output is correct
18 Correct 8 ms 12116 KB Output is correct
19 Correct 7 ms 11988 KB Output is correct
20 Correct 8 ms 12068 KB Output is correct
21 Correct 8 ms 12068 KB Output is correct
22 Correct 10 ms 12068 KB Output is correct
23 Correct 8 ms 11988 KB Output is correct
24 Correct 9 ms 12068 KB Output is correct
25 Correct 8 ms 12104 KB Output is correct
26 Correct 10 ms 12628 KB Output is correct
27 Correct 9 ms 12476 KB Output is correct
28 Correct 9 ms 12756 KB Output is correct
29 Correct 9 ms 12628 KB Output is correct
30 Correct 9 ms 12500 KB Output is correct
31 Correct 7 ms 12004 KB Output is correct
32 Correct 9 ms 12852 KB Output is correct
33 Correct 8 ms 12116 KB Output is correct
34 Correct 10 ms 12468 KB Output is correct
35 Correct 11 ms 12672 KB Output is correct
36 Correct 9 ms 12480 KB Output is correct
37 Correct 9 ms 12536 KB Output is correct
38 Correct 8 ms 12116 KB Output is correct
39 Correct 9 ms 12596 KB Output is correct
40 Correct 9 ms 12464 KB Output is correct
41 Correct 9 ms 12500 KB Output is correct
42 Correct 11 ms 12592 KB Output is correct
43 Correct 11 ms 12764 KB Output is correct
44 Correct 9 ms 12116 KB Output is correct
45 Correct 9 ms 12724 KB Output is correct
46 Correct 10 ms 12596 KB Output is correct
47 Correct 9 ms 12116 KB Output is correct
48 Correct 9 ms 12500 KB Output is correct
49 Correct 9 ms 12628 KB Output is correct
50 Correct 13 ms 12756 KB Output is correct
51 Correct 11 ms 12500 KB Output is correct
52 Correct 9 ms 12468 KB Output is correct
53 Correct 9 ms 12592 KB Output is correct
54 Correct 10 ms 12576 KB Output is correct
55 Correct 9 ms 12116 KB Output is correct
56 Correct 82 ms 26144 KB Output is correct
57 Correct 99 ms 26308 KB Output is correct
58 Correct 11 ms 12564 KB Output is correct
59 Correct 8 ms 12116 KB Output is correct
60 Correct 8 ms 12076 KB Output is correct
61 Correct 130 ms 26348 KB Output is correct
62 Correct 9 ms 12468 KB Output is correct
63 Correct 136 ms 33436 KB Output is correct
64 Correct 104 ms 26316 KB Output is correct
65 Correct 9 ms 12472 KB Output is correct
66 Correct 115 ms 27100 KB Output is correct
67 Correct 10 ms 12536 KB Output is correct
68 Correct 9 ms 12500 KB Output is correct
69 Correct 79 ms 26092 KB Output is correct
70 Correct 9 ms 12716 KB Output is correct
71 Correct 102 ms 26116 KB Output is correct
72 Correct 121 ms 28648 KB Output is correct
73 Correct 8 ms 12068 KB Output is correct
74 Correct 143 ms 33592 KB Output is correct
75 Correct 126 ms 30036 KB Output is correct
76 Correct 11 ms 12500 KB Output is correct
77 Correct 9 ms 12500 KB Output is correct
78 Correct 92 ms 24800 KB Output is correct
79 Correct 100 ms 30436 KB Output is correct
80 Correct 9 ms 12500 KB Output is correct
81 Correct 13 ms 12524 KB Output is correct
82 Correct 8 ms 12072 KB Output is correct
83 Correct 7 ms 12116 KB Output is correct
84 Correct 8 ms 12500 KB Output is correct
85 Correct 156 ms 29212 KB Output is correct
86 Correct 11 ms 12532 KB Output is correct
87 Correct 114 ms 26860 KB Output is correct
88 Correct 7 ms 12068 KB Output is correct
89 Correct 114 ms 26856 KB Output is correct
90 Correct 144 ms 29092 KB Output is correct
91 Correct 108 ms 31944 KB Output is correct
92 Correct 59 ms 26148 KB Output is correct
93 Correct 12 ms 12628 KB Output is correct
94 Correct 7 ms 12112 KB Output is correct
95 Correct 92 ms 31696 KB Output is correct
96 Correct 177 ms 35784 KB Output is correct
97 Correct 12 ms 12500 KB Output is correct
98 Correct 7 ms 12064 KB Output is correct
99 Correct 84 ms 29388 KB Output is correct
100 Correct 8 ms 12500 KB Output is correct
101 Correct 132 ms 27408 KB Output is correct
102 Correct 153 ms 34404 KB Output is correct
103 Correct 8 ms 12640 KB Output is correct
104 Correct 10 ms 12660 KB Output is correct
105 Correct 8 ms 12468 KB Output is correct
106 Correct 8 ms 12500 KB Output is correct
107 Correct 8 ms 12600 KB Output is correct
108 Correct 563 ms 97676 KB Output is correct
109 Correct 862 ms 100936 KB Output is correct
110 Correct 884 ms 112312 KB Output is correct
111 Correct 1070 ms 135600 KB Output is correct
112 Correct 1020 ms 125824 KB Output is correct
113 Correct 630 ms 111236 KB Output is correct
114 Correct 680 ms 82948 KB Output is correct
115 Correct 633 ms 82944 KB Output is correct
116 Correct 780 ms 91628 KB Output is correct
117 Correct 816 ms 112644 KB Output is correct
118 Correct 684 ms 85080 KB Output is correct
119 Correct 878 ms 112680 KB Output is correct
120 Correct 994 ms 126624 KB Output is correct
121 Correct 839 ms 112636 KB Output is correct
122 Correct 887 ms 101532 KB Output is correct
123 Correct 580 ms 114004 KB Output is correct
124 Correct 1000 ms 110316 KB Output is correct