Submission #1046171

# Submission time Handle Problem Language Result Execution time Memory
1046171 2024-08-06T10:59:50 Z vladilius Mergers (JOI19_mergers) C++17
100 / 100
503 ms 132256 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, k; cin>>n>>k;
    vector<int> g[n + 1];
    for (int i = 1; i < n; i++){
        int x, y; cin>>x>>y;
        g[x].pb(y);
        g[y].pb(x);
    }
    vector<int> s(n + 1), all[k + 1];
    for (int i = 1; i <= n; i++){
        cin>>s[i];
        all[s[i]].pb(i);
    }
    
    vector<int> sz(n + 1), h(n + 1), d(n + 1), p(n + 1);
    function<void(int, int)> fill = [&](int v, int pr){
        p[v] = pr;
        d[v] = d[pr] + 1;
        sz[v] = 1;
        for (int i: g[v]){
            if (i == pr) continue;
            fill(i, v);
            if (sz[i] > sz[h[v]]){
                h[v] = i;
            }
            sz[v] += sz[i];
        }
    };
    fill(1, 0);
    
    vector<int> head(n + 1), pos(n + 1);
    int timer = 0;
    function<void(int, int)> fill_hld = [&](int v, int k){
        head[v] = k;
        pos[v] = ++timer;
        if (!h[v]) return;
        fill_hld(h[v], k);
        for (int i: g[v]){
            if (pos[i]) continue;
            fill_hld(i, i);
        }
    };
    fill_hld(1, 1);
    
    set<int> st;
    for (int i = 2; i <= n; i++) st.ins(i);
    
    vector<int> a(n + 2);
    
    auto rem = [&](int l, int r){
        a[l]++; a[r + 1]--;
    };
    
    auto upd = [&](int x, int y){
        while (head[x] != head[y]){
            if (d[head[x]] > d[head[y]]){
                swap(x, y);
            }
            rem(pos[head[y]], pos[y]);
            y = p[head[y]];
        }
        if (d[x] > d[y]) swap(x, y);
        rem(pos[x] + 1, pos[y]);
    };
    
    for (int i = 1; i <= k; i++){
        for (int j = 0; j + 1 < all[i].size(); j++){
            upd(all[i][j], all[i][j + 1]);
        }
    }
    
    vector<int> inv(n + 1), pr(n + 1);
    for (int i = 1; i <= n; i++){
        inv[pos[i]] = i;
        pr[i] = pr[i - 1] + a[i];
    }
    
    vector<bool> ban(n + 1);
    vector<int> bd;
    for (int i = 2; i <= n; i++){
        if (!pr[i]){
            bd.pb(inv[i]);
            ban[inv[i]] = 1;
        }
    }
    
    auto f = [&](int x, int y){
        return (d[x] > d[y]) ? x : y;
    };
    
    vector<int> gr(n + 1);
    timer = 0;
    function<void(int)> dfs = [&](int v){
        gr[v] = timer;
        for (int i: g[v]){
            if (gr[i] || ban[f(i, v)]) continue;
            dfs(i);
        }
    };
    
    for (int i = 1; i <= n; i++){
        if (!gr[i]){
            timer++;
            dfs(i);
        }
    }
    
    if (timer == 1){
        cout<<0<<"\n";
        return 0;
    }
    
    vector<int> cc(timer + 1);
    
    for (int i: bd){
        cc[gr[i]]++;
        cc[gr[p[i]]]++;
    }
    
    int tot = 0;
    for (int i = 1; i <= timer; i++) tot += (cc[i] == 1);
    
    cout<<(tot + 1) / 2<<"\n";
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:80:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for (int j = 0; j + 1 < all[i].size(); j++){
      |                         ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 1116 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 860 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 2 ms 856 KB Output is correct
42 Correct 1 ms 860 KB Output is correct
43 Correct 1 ms 1116 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 1116 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 860 KB Output is correct
49 Correct 1 ms 860 KB Output is correct
50 Correct 1 ms 1116 KB Output is correct
51 Correct 1 ms 860 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 1 ms 860 KB Output is correct
54 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 40 ms 15828 KB Output is correct
27 Correct 58 ms 15692 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 49 ms 15420 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 55 ms 24100 KB Output is correct
34 Correct 57 ms 15436 KB Output is correct
35 Correct 1 ms 856 KB Output is correct
36 Correct 55 ms 16172 KB Output is correct
37 Correct 2 ms 860 KB Output is correct
38 Correct 1 ms 812 KB Output is correct
39 Correct 36 ms 15584 KB Output is correct
40 Correct 1 ms 1116 KB Output is correct
41 Correct 53 ms 15292 KB Output is correct
42 Correct 50 ms 18568 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 49 ms 24268 KB Output is correct
45 Correct 51 ms 20048 KB Output is correct
46 Correct 1 ms 856 KB Output is correct
47 Correct 2 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 15560 KB Output is correct
2 Correct 45 ms 21580 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 52 ms 17680 KB Output is correct
9 Correct 1 ms 856 KB Output is correct
10 Correct 59 ms 15444 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 48 ms 15696 KB Output is correct
13 Correct 55 ms 17740 KB Output is correct
14 Correct 54 ms 21452 KB Output is correct
15 Correct 36 ms 15696 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 42 ms 20932 KB Output is correct
19 Correct 56 ms 26040 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 45 ms 18124 KB Output is correct
23 Correct 2 ms 856 KB Output is correct
24 Correct 58 ms 15920 KB Output is correct
25 Correct 54 ms 24524 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 1116 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 1116 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 860 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 2 ms 856 KB Output is correct
42 Correct 1 ms 860 KB Output is correct
43 Correct 1 ms 1116 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 1 ms 1116 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 860 KB Output is correct
49 Correct 1 ms 860 KB Output is correct
50 Correct 1 ms 1116 KB Output is correct
51 Correct 1 ms 860 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 1 ms 860 KB Output is correct
54 Correct 1 ms 860 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 40 ms 15828 KB Output is correct
57 Correct 58 ms 15692 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 49 ms 15420 KB Output is correct
62 Correct 1 ms 860 KB Output is correct
63 Correct 55 ms 24100 KB Output is correct
64 Correct 57 ms 15436 KB Output is correct
65 Correct 1 ms 856 KB Output is correct
66 Correct 55 ms 16172 KB Output is correct
67 Correct 2 ms 860 KB Output is correct
68 Correct 1 ms 812 KB Output is correct
69 Correct 36 ms 15584 KB Output is correct
70 Correct 1 ms 1116 KB Output is correct
71 Correct 53 ms 15292 KB Output is correct
72 Correct 50 ms 18568 KB Output is correct
73 Correct 1 ms 344 KB Output is correct
74 Correct 49 ms 24268 KB Output is correct
75 Correct 51 ms 20048 KB Output is correct
76 Correct 1 ms 856 KB Output is correct
77 Correct 2 ms 856 KB Output is correct
78 Correct 51 ms 15560 KB Output is correct
79 Correct 45 ms 21580 KB Output is correct
80 Correct 1 ms 860 KB Output is correct
81 Correct 1 ms 860 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 860 KB Output is correct
85 Correct 52 ms 17680 KB Output is correct
86 Correct 1 ms 856 KB Output is correct
87 Correct 59 ms 15444 KB Output is correct
88 Correct 0 ms 344 KB Output is correct
89 Correct 48 ms 15696 KB Output is correct
90 Correct 55 ms 17740 KB Output is correct
91 Correct 54 ms 21452 KB Output is correct
92 Correct 36 ms 15696 KB Output is correct
93 Correct 1 ms 860 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 42 ms 20932 KB Output is correct
96 Correct 56 ms 26040 KB Output is correct
97 Correct 2 ms 856 KB Output is correct
98 Correct 0 ms 348 KB Output is correct
99 Correct 45 ms 18124 KB Output is correct
100 Correct 2 ms 856 KB Output is correct
101 Correct 58 ms 15920 KB Output is correct
102 Correct 54 ms 24524 KB Output is correct
103 Correct 1 ms 860 KB Output is correct
104 Correct 1 ms 1116 KB Output is correct
105 Correct 1 ms 860 KB Output is correct
106 Correct 1 ms 860 KB Output is correct
107 Correct 1 ms 860 KB Output is correct
108 Correct 301 ms 86460 KB Output is correct
109 Correct 492 ms 97096 KB Output is correct
110 Correct 440 ms 110528 KB Output is correct
111 Correct 455 ms 132256 KB Output is correct
112 Correct 456 ms 118976 KB Output is correct
113 Correct 294 ms 102832 KB Output is correct
114 Correct 431 ms 75376 KB Output is correct
115 Correct 503 ms 75456 KB Output is correct
116 Correct 461 ms 80532 KB Output is correct
117 Correct 385 ms 104892 KB Output is correct
118 Correct 375 ms 74956 KB Output is correct
119 Correct 413 ms 104896 KB Output is correct
120 Correct 419 ms 121536 KB Output is correct
121 Correct 428 ms 104896 KB Output is correct
122 Correct 465 ms 90568 KB Output is correct
123 Correct 284 ms 106208 KB Output is correct
124 Correct 433 ms 101220 KB Output is correct