Submission #1046169

# Submission time Handle Problem Language Result Execution time Memory
1046169 2024-08-06T10:54:54 Z vladilius Mergers (JOI19_mergers) C++17
100 / 100
1000 ms 138076 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);
    
    auto rem = [&](int l, int r){
        vector<int> rm;
        auto it = st.lower_bound(l);
        while (it != st.end() && (*it) <= r){
            rm.pb(*it);
            it++;
        }
        for (int i: rm) st.erase(i);
    };
    
    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);
    for (int i = 1; i <= n; i++) inv[pos[i]] = i;
    
    vector<bool> ban(n + 1);
    vector<int> bd;
    for (int i: st){
        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:84:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for (int j = 0; j + 1 < all[i].size(); j++){
      |                         ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 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 348 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 0 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 348 KB Output is correct
21 Correct 0 ms 460 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 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 348 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 0 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 348 KB Output is correct
21 Correct 0 ms 460 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 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 860 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 856 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 2 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 2 ms 860 KB Output is correct
37 Correct 2 ms 856 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 2 ms 860 KB Output is correct
42 Correct 2 ms 976 KB Output is correct
43 Correct 1 ms 1116 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 2 ms 1116 KB Output is correct
46 Correct 2 ms 896 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 2 ms 860 KB Output is correct
49 Correct 1 ms 1112 KB Output is correct
50 Correct 1 ms 1116 KB Output is correct
51 Correct 2 ms 728 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 1 ms 860 KB Output is correct
54 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 348 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 0 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 348 KB Output is correct
21 Correct 0 ms 460 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 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 77 ms 15640 KB Output is correct
27 Correct 153 ms 15184 KB Output is correct
28 Correct 2 ms 856 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 456 KB Output is correct
31 Correct 87 ms 15592 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 60 ms 24136 KB Output is correct
34 Correct 125 ms 15380 KB Output is correct
35 Correct 2 ms 856 KB Output is correct
36 Correct 58 ms 16980 KB Output is correct
37 Correct 2 ms 856 KB Output is correct
38 Correct 2 ms 860 KB Output is correct
39 Correct 75 ms 15436 KB Output is correct
40 Correct 2 ms 1116 KB Output is correct
41 Correct 135 ms 15008 KB Output is correct
42 Correct 55 ms 18516 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 56 ms 24268 KB Output is correct
45 Correct 75 ms 20048 KB Output is correct
46 Correct 3 ms 724 KB Output is correct
47 Correct 2 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 14028 KB Output is correct
2 Correct 44 ms 22540 KB Output is correct
3 Correct 2 ms 856 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 88 ms 18840 KB Output is correct
9 Correct 2 ms 856 KB Output is correct
10 Correct 73 ms 16268 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 60 ms 16208 KB Output is correct
13 Correct 76 ms 18772 KB Output is correct
14 Correct 54 ms 22352 KB Output is correct
15 Correct 78 ms 15440 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 53 ms 21732 KB Output is correct
19 Correct 56 ms 26912 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 72 ms 19140 KB Output is correct
23 Correct 2 ms 856 KB Output is correct
24 Correct 72 ms 16836 KB Output is correct
25 Correct 54 ms 25380 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 1 ms 1116 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 348 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 0 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 348 KB Output is correct
21 Correct 0 ms 460 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 456 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 860 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 856 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 2 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 2 ms 860 KB Output is correct
37 Correct 2 ms 856 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 2 ms 860 KB Output is correct
42 Correct 2 ms 976 KB Output is correct
43 Correct 1 ms 1116 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 2 ms 1116 KB Output is correct
46 Correct 2 ms 896 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 2 ms 860 KB Output is correct
49 Correct 1 ms 1112 KB Output is correct
50 Correct 1 ms 1116 KB Output is correct
51 Correct 2 ms 728 KB Output is correct
52 Correct 2 ms 860 KB Output is correct
53 Correct 1 ms 860 KB Output is correct
54 Correct 2 ms 860 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 77 ms 15640 KB Output is correct
57 Correct 153 ms 15184 KB Output is correct
58 Correct 2 ms 856 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 456 KB Output is correct
61 Correct 87 ms 15592 KB Output is correct
62 Correct 2 ms 856 KB Output is correct
63 Correct 60 ms 24136 KB Output is correct
64 Correct 125 ms 15380 KB Output is correct
65 Correct 2 ms 856 KB Output is correct
66 Correct 58 ms 16980 KB Output is correct
67 Correct 2 ms 856 KB Output is correct
68 Correct 2 ms 860 KB Output is correct
69 Correct 75 ms 15436 KB Output is correct
70 Correct 2 ms 1116 KB Output is correct
71 Correct 135 ms 15008 KB Output is correct
72 Correct 55 ms 18516 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 56 ms 24268 KB Output is correct
75 Correct 75 ms 20048 KB Output is correct
76 Correct 3 ms 724 KB Output is correct
77 Correct 2 ms 748 KB Output is correct
78 Correct 72 ms 14028 KB Output is correct
79 Correct 44 ms 22540 KB Output is correct
80 Correct 2 ms 856 KB Output is correct
81 Correct 2 ms 860 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 452 KB Output is correct
84 Correct 2 ms 860 KB Output is correct
85 Correct 88 ms 18840 KB Output is correct
86 Correct 2 ms 856 KB Output is correct
87 Correct 73 ms 16268 KB Output is correct
88 Correct 1 ms 344 KB Output is correct
89 Correct 60 ms 16208 KB Output is correct
90 Correct 76 ms 18772 KB Output is correct
91 Correct 54 ms 22352 KB Output is correct
92 Correct 78 ms 15440 KB Output is correct
93 Correct 2 ms 860 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 53 ms 21732 KB Output is correct
96 Correct 56 ms 26912 KB Output is correct
97 Correct 2 ms 856 KB Output is correct
98 Correct 0 ms 348 KB Output is correct
99 Correct 72 ms 19140 KB Output is correct
100 Correct 2 ms 856 KB Output is correct
101 Correct 72 ms 16836 KB Output is correct
102 Correct 54 ms 25380 KB Output is correct
103 Correct 2 ms 1116 KB Output is correct
104 Correct 1 ms 1116 KB Output is correct
105 Correct 2 ms 724 KB Output is correct
106 Correct 2 ms 860 KB Output is correct
107 Correct 2 ms 860 KB Output is correct
108 Correct 634 ms 92228 KB Output is correct
109 Correct 531 ms 97680 KB Output is correct
110 Correct 514 ms 110784 KB Output is correct
111 Correct 468 ms 138076 KB Output is correct
112 Correct 534 ms 119504 KB Output is correct
113 Correct 402 ms 108732 KB Output is correct
114 Correct 919 ms 75108 KB Output is correct
115 Correct 1000 ms 75204 KB Output is correct
116 Correct 908 ms 86008 KB Output is correct
117 Correct 397 ms 110788 KB Output is correct
118 Correct 582 ms 79184 KB Output is correct
119 Correct 409 ms 110924 KB Output is correct
120 Correct 457 ms 127700 KB Output is correct
121 Correct 419 ms 110788 KB Output is correct
122 Correct 849 ms 96464 KB Output is correct
123 Correct 301 ms 112236 KB Output is correct
124 Correct 466 ms 107112 KB Output is correct