Submission #545552

# Submission time Handle Problem Language Result Execution time Memory
545552 2022-04-04T19:46:35 Z Lobo Mergers (JOI19_mergers) C++17
100 / 100
1599 ms 240020 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 1e6+10;

int n, k, tin[maxn], p[maxn][23], ps[maxn][23], h[maxn], ds[maxn], dsz[maxn];
int timer = 0;
vector<int> g[maxn];
vector<pair<int,int>> vec[maxn];

void dfs(int u, int ant) {
    p[u][0] = ant;
    tin[u] = ++timer;

    for(int i = 1; i <= 20; i++) {
        p[u][i] = p[p[u][i-1]][i-1];
    }
    for(auto v : g[u]) {
        if(v == ant) continue;
        h[v] = h[u]+1;
        dfs(v,u);
    }
}

int lca(int u, int v) {
    if(h[u] < h[v]) swap(u,v);

    for(int i = 20; i >= 0; i--) {
        if(h[p[u][i]] >= h[v]) {
            u = p[u][i];
        }
    }

    if(u == v) return u;

    for(int i = 20; i >= 0; i--) {
        if(p[u][i] != p[v][i]) {
            u = p[u][i];
            v = p[v][i];
        }
    }

    return p[u][0];
}

int find(int v) {
    if(ds[v] == v) return v;
    return ds[v] = find(ds[v]);
}

void join(int u, int v) {
    if(dsz[u] < dsz[v]) swap(u,v);
    ds[v] = u;
    dsz[u]+= dsz[v];
}

int qtdf = 0, mark[maxn];
void dfs1(int u) {
    mark[u] = 1;
    set<int> st;
    for(auto v : g[u]) {
        v = find(v);
        if(v != u) st.insert(v);
        if(mark[v]) continue;
        dfs1(v);
    }
    if(st.size() == 1) qtdf++;
}

void solve() {
    cin >> n >> k;

    for(int i = 1; i <= n-1; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    dfs(1,1);

    for(int i = 1; i <= n; i++) {
        int cl; cin >> cl;
        vec[cl].pb(mp(tin[i],i));
    }

    for(int i = 1; i <= k; i++) {
        sort(all(vec[i]));
        vec[i].pb(vec[i][0]);
        for(int j = 1; j < vec[i].size(); j++) {
            int u = vec[i][j-1].sc;
            int v = vec[i][j].sc;

            int lc = lca(u,v);

            for(int j = 20; j >= 0; j--) {
                if(h[p[u][j]] > h[lc]) {
                    ps[u][j]++;
                    u = p[u][j];
                }
            }
            if(u != lc) {
                ps[u][0]++;
            }

            for(int j = 20; j >= 0; j--) {
                if(h[p[v][j]] > h[lc]) {
                    ps[v][j]++;
                    v = p[v][j];
                }
            }
            if(v != lc) {
                ps[v][0]++;
            }
        }
    }

    for(int j = 20; j >= 1; j--) {
        for(int i = 1; i <= n; i++) {
            ps[i][j-1]+= ps[i][j];
            ps[p[i][j-1]][j-1]+= ps[i][j];
        }
    }

    for(int i = 1; i <= n; i++) {
        ds[i] = i;
        dsz[i] = 1;
    }

    int ans = n-1;
    for(int i = 1; i <= n; i++) {
        if(ps[i][0] != 0) {
            int u = find(i);
            int v = find(p[i][0]);
            if(u != v) {
                ans--;
                join(u,v);
            }
        }
    }

    for(int i = 1; i <= n; i++) {
        int v = find(i);
        if(v == i) continue;
        for(auto x : g[i]) {
            if(x != v) g[v].pb(find(x));
        }
    }

    dfs1(find(1));

    cout << (qtdf+1)/2 << endl;

}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    
    int tt = 1;
    // cin >> tt;
    int t = 1;
    while(tt--) {
        solve();
    }

}

Compilation message

mergers.cpp: In function 'void solve()':
mergers.cpp:100:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |         for(int j = 1; j < vec[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:174:9: warning: unused variable 't' [-Wunused-variable]
  174 |     int t = 1;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47276 KB Output is correct
2 Correct 23 ms 47316 KB Output is correct
3 Correct 23 ms 47300 KB Output is correct
4 Correct 23 ms 47304 KB Output is correct
5 Correct 23 ms 47232 KB Output is correct
6 Correct 24 ms 47352 KB Output is correct
7 Correct 24 ms 47220 KB Output is correct
8 Correct 22 ms 47276 KB Output is correct
9 Correct 22 ms 47316 KB Output is correct
10 Correct 23 ms 47308 KB Output is correct
11 Correct 24 ms 47316 KB Output is correct
12 Correct 24 ms 47300 KB Output is correct
13 Correct 23 ms 47300 KB Output is correct
14 Correct 25 ms 47344 KB Output is correct
15 Correct 23 ms 47360 KB Output is correct
16 Correct 22 ms 47272 KB Output is correct
17 Correct 23 ms 47240 KB Output is correct
18 Correct 24 ms 47292 KB Output is correct
19 Correct 25 ms 47332 KB Output is correct
20 Correct 23 ms 47260 KB Output is correct
21 Correct 24 ms 47260 KB Output is correct
22 Correct 22 ms 47384 KB Output is correct
23 Correct 23 ms 47312 KB Output is correct
24 Correct 23 ms 47292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47276 KB Output is correct
2 Correct 23 ms 47316 KB Output is correct
3 Correct 23 ms 47300 KB Output is correct
4 Correct 23 ms 47304 KB Output is correct
5 Correct 23 ms 47232 KB Output is correct
6 Correct 24 ms 47352 KB Output is correct
7 Correct 24 ms 47220 KB Output is correct
8 Correct 22 ms 47276 KB Output is correct
9 Correct 22 ms 47316 KB Output is correct
10 Correct 23 ms 47308 KB Output is correct
11 Correct 24 ms 47316 KB Output is correct
12 Correct 24 ms 47300 KB Output is correct
13 Correct 23 ms 47300 KB Output is correct
14 Correct 25 ms 47344 KB Output is correct
15 Correct 23 ms 47360 KB Output is correct
16 Correct 22 ms 47272 KB Output is correct
17 Correct 23 ms 47240 KB Output is correct
18 Correct 24 ms 47292 KB Output is correct
19 Correct 25 ms 47332 KB Output is correct
20 Correct 23 ms 47260 KB Output is correct
21 Correct 24 ms 47260 KB Output is correct
22 Correct 22 ms 47384 KB Output is correct
23 Correct 23 ms 47312 KB Output is correct
24 Correct 23 ms 47292 KB Output is correct
25 Correct 23 ms 47316 KB Output is correct
26 Correct 26 ms 48076 KB Output is correct
27 Correct 26 ms 48084 KB Output is correct
28 Correct 27 ms 48248 KB Output is correct
29 Correct 30 ms 48084 KB Output is correct
30 Correct 26 ms 48084 KB Output is correct
31 Correct 28 ms 47344 KB Output is correct
32 Correct 27 ms 48328 KB Output is correct
33 Correct 25 ms 47316 KB Output is correct
34 Correct 25 ms 48188 KB Output is correct
35 Correct 27 ms 48040 KB Output is correct
36 Correct 28 ms 48044 KB Output is correct
37 Correct 26 ms 48232 KB Output is correct
38 Correct 29 ms 47228 KB Output is correct
39 Correct 28 ms 48216 KB Output is correct
40 Correct 26 ms 48076 KB Output is correct
41 Correct 25 ms 48072 KB Output is correct
42 Correct 28 ms 48212 KB Output is correct
43 Correct 26 ms 48264 KB Output is correct
44 Correct 23 ms 47268 KB Output is correct
45 Correct 27 ms 48204 KB Output is correct
46 Correct 26 ms 48144 KB Output is correct
47 Correct 24 ms 47344 KB Output is correct
48 Correct 26 ms 48084 KB Output is correct
49 Correct 26 ms 48332 KB Output is correct
50 Correct 26 ms 48248 KB Output is correct
51 Correct 27 ms 48080 KB Output is correct
52 Correct 26 ms 48084 KB Output is correct
53 Correct 26 ms 48208 KB Output is correct
54 Correct 27 ms 48092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47276 KB Output is correct
2 Correct 23 ms 47316 KB Output is correct
3 Correct 23 ms 47300 KB Output is correct
4 Correct 23 ms 47304 KB Output is correct
5 Correct 23 ms 47232 KB Output is correct
6 Correct 24 ms 47352 KB Output is correct
7 Correct 24 ms 47220 KB Output is correct
8 Correct 22 ms 47276 KB Output is correct
9 Correct 22 ms 47316 KB Output is correct
10 Correct 23 ms 47308 KB Output is correct
11 Correct 24 ms 47316 KB Output is correct
12 Correct 24 ms 47300 KB Output is correct
13 Correct 23 ms 47300 KB Output is correct
14 Correct 25 ms 47344 KB Output is correct
15 Correct 23 ms 47360 KB Output is correct
16 Correct 22 ms 47272 KB Output is correct
17 Correct 23 ms 47240 KB Output is correct
18 Correct 24 ms 47292 KB Output is correct
19 Correct 25 ms 47332 KB Output is correct
20 Correct 23 ms 47260 KB Output is correct
21 Correct 24 ms 47260 KB Output is correct
22 Correct 22 ms 47384 KB Output is correct
23 Correct 23 ms 47312 KB Output is correct
24 Correct 23 ms 47292 KB Output is correct
25 Correct 23 ms 47316 KB Output is correct
26 Correct 138 ms 73136 KB Output is correct
27 Correct 180 ms 72772 KB Output is correct
28 Correct 31 ms 48076 KB Output is correct
29 Correct 25 ms 47304 KB Output is correct
30 Correct 24 ms 47312 KB Output is correct
31 Correct 172 ms 72456 KB Output is correct
32 Correct 26 ms 48084 KB Output is correct
33 Correct 223 ms 78560 KB Output is correct
34 Correct 184 ms 72612 KB Output is correct
35 Correct 27 ms 48040 KB Output is correct
36 Correct 184 ms 73136 KB Output is correct
37 Correct 29 ms 48084 KB Output is correct
38 Correct 28 ms 48156 KB Output is correct
39 Correct 151 ms 73168 KB Output is correct
40 Correct 28 ms 48232 KB Output is correct
41 Correct 178 ms 72988 KB Output is correct
42 Correct 220 ms 74372 KB Output is correct
43 Correct 25 ms 47304 KB Output is correct
44 Correct 197 ms 79028 KB Output is correct
45 Correct 223 ms 75700 KB Output is correct
46 Correct 27 ms 48180 KB Output is correct
47 Correct 27 ms 48092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 73220 KB Output is correct
2 Correct 199 ms 79224 KB Output is correct
3 Correct 27 ms 48084 KB Output is correct
4 Correct 27 ms 48084 KB Output is correct
5 Correct 24 ms 47316 KB Output is correct
6 Correct 23 ms 47316 KB Output is correct
7 Correct 26 ms 48084 KB Output is correct
8 Correct 200 ms 74756 KB Output is correct
9 Correct 28 ms 48224 KB Output is correct
10 Correct 167 ms 73072 KB Output is correct
11 Correct 24 ms 47324 KB Output is correct
12 Correct 173 ms 73076 KB Output is correct
13 Correct 188 ms 74860 KB Output is correct
14 Correct 189 ms 74212 KB Output is correct
15 Correct 141 ms 73052 KB Output is correct
16 Correct 27 ms 48212 KB Output is correct
17 Correct 25 ms 47264 KB Output is correct
18 Correct 188 ms 78816 KB Output is correct
19 Correct 233 ms 85772 KB Output is correct
20 Correct 27 ms 48072 KB Output is correct
21 Correct 24 ms 47264 KB Output is correct
22 Correct 158 ms 75436 KB Output is correct
23 Correct 26 ms 48076 KB Output is correct
24 Correct 178 ms 74060 KB Output is correct
25 Correct 248 ms 82700 KB Output is correct
26 Correct 27 ms 48204 KB Output is correct
27 Correct 27 ms 48324 KB Output is correct
28 Correct 27 ms 48116 KB Output is correct
29 Correct 26 ms 48080 KB Output is correct
30 Correct 26 ms 48232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47276 KB Output is correct
2 Correct 23 ms 47316 KB Output is correct
3 Correct 23 ms 47300 KB Output is correct
4 Correct 23 ms 47304 KB Output is correct
5 Correct 23 ms 47232 KB Output is correct
6 Correct 24 ms 47352 KB Output is correct
7 Correct 24 ms 47220 KB Output is correct
8 Correct 22 ms 47276 KB Output is correct
9 Correct 22 ms 47316 KB Output is correct
10 Correct 23 ms 47308 KB Output is correct
11 Correct 24 ms 47316 KB Output is correct
12 Correct 24 ms 47300 KB Output is correct
13 Correct 23 ms 47300 KB Output is correct
14 Correct 25 ms 47344 KB Output is correct
15 Correct 23 ms 47360 KB Output is correct
16 Correct 22 ms 47272 KB Output is correct
17 Correct 23 ms 47240 KB Output is correct
18 Correct 24 ms 47292 KB Output is correct
19 Correct 25 ms 47332 KB Output is correct
20 Correct 23 ms 47260 KB Output is correct
21 Correct 24 ms 47260 KB Output is correct
22 Correct 22 ms 47384 KB Output is correct
23 Correct 23 ms 47312 KB Output is correct
24 Correct 23 ms 47292 KB Output is correct
25 Correct 23 ms 47316 KB Output is correct
26 Correct 26 ms 48076 KB Output is correct
27 Correct 26 ms 48084 KB Output is correct
28 Correct 27 ms 48248 KB Output is correct
29 Correct 30 ms 48084 KB Output is correct
30 Correct 26 ms 48084 KB Output is correct
31 Correct 28 ms 47344 KB Output is correct
32 Correct 27 ms 48328 KB Output is correct
33 Correct 25 ms 47316 KB Output is correct
34 Correct 25 ms 48188 KB Output is correct
35 Correct 27 ms 48040 KB Output is correct
36 Correct 28 ms 48044 KB Output is correct
37 Correct 26 ms 48232 KB Output is correct
38 Correct 29 ms 47228 KB Output is correct
39 Correct 28 ms 48216 KB Output is correct
40 Correct 26 ms 48076 KB Output is correct
41 Correct 25 ms 48072 KB Output is correct
42 Correct 28 ms 48212 KB Output is correct
43 Correct 26 ms 48264 KB Output is correct
44 Correct 23 ms 47268 KB Output is correct
45 Correct 27 ms 48204 KB Output is correct
46 Correct 26 ms 48144 KB Output is correct
47 Correct 24 ms 47344 KB Output is correct
48 Correct 26 ms 48084 KB Output is correct
49 Correct 26 ms 48332 KB Output is correct
50 Correct 26 ms 48248 KB Output is correct
51 Correct 27 ms 48080 KB Output is correct
52 Correct 26 ms 48084 KB Output is correct
53 Correct 26 ms 48208 KB Output is correct
54 Correct 27 ms 48092 KB Output is correct
55 Correct 23 ms 47316 KB Output is correct
56 Correct 138 ms 73136 KB Output is correct
57 Correct 180 ms 72772 KB Output is correct
58 Correct 31 ms 48076 KB Output is correct
59 Correct 25 ms 47304 KB Output is correct
60 Correct 24 ms 47312 KB Output is correct
61 Correct 172 ms 72456 KB Output is correct
62 Correct 26 ms 48084 KB Output is correct
63 Correct 223 ms 78560 KB Output is correct
64 Correct 184 ms 72612 KB Output is correct
65 Correct 27 ms 48040 KB Output is correct
66 Correct 184 ms 73136 KB Output is correct
67 Correct 29 ms 48084 KB Output is correct
68 Correct 28 ms 48156 KB Output is correct
69 Correct 151 ms 73168 KB Output is correct
70 Correct 28 ms 48232 KB Output is correct
71 Correct 178 ms 72988 KB Output is correct
72 Correct 220 ms 74372 KB Output is correct
73 Correct 25 ms 47304 KB Output is correct
74 Correct 197 ms 79028 KB Output is correct
75 Correct 223 ms 75700 KB Output is correct
76 Correct 27 ms 48180 KB Output is correct
77 Correct 27 ms 48092 KB Output is correct
78 Correct 145 ms 73220 KB Output is correct
79 Correct 199 ms 79224 KB Output is correct
80 Correct 27 ms 48084 KB Output is correct
81 Correct 27 ms 48084 KB Output is correct
82 Correct 24 ms 47316 KB Output is correct
83 Correct 23 ms 47316 KB Output is correct
84 Correct 26 ms 48084 KB Output is correct
85 Correct 200 ms 74756 KB Output is correct
86 Correct 28 ms 48224 KB Output is correct
87 Correct 167 ms 73072 KB Output is correct
88 Correct 24 ms 47324 KB Output is correct
89 Correct 173 ms 73076 KB Output is correct
90 Correct 188 ms 74860 KB Output is correct
91 Correct 189 ms 74212 KB Output is correct
92 Correct 141 ms 73052 KB Output is correct
93 Correct 27 ms 48212 KB Output is correct
94 Correct 25 ms 47264 KB Output is correct
95 Correct 188 ms 78816 KB Output is correct
96 Correct 233 ms 85772 KB Output is correct
97 Correct 27 ms 48072 KB Output is correct
98 Correct 24 ms 47264 KB Output is correct
99 Correct 158 ms 75436 KB Output is correct
100 Correct 26 ms 48076 KB Output is correct
101 Correct 178 ms 74060 KB Output is correct
102 Correct 248 ms 82700 KB Output is correct
103 Correct 27 ms 48204 KB Output is correct
104 Correct 27 ms 48324 KB Output is correct
105 Correct 27 ms 48116 KB Output is correct
106 Correct 26 ms 48080 KB Output is correct
107 Correct 26 ms 48232 KB Output is correct
108 Correct 939 ms 183224 KB Output is correct
109 Correct 1232 ms 186460 KB Output is correct
110 Correct 1244 ms 195816 KB Output is correct
111 Correct 1599 ms 240020 KB Output is correct
112 Correct 1583 ms 204244 KB Output is correct
113 Correct 1261 ms 203088 KB Output is correct
114 Correct 1004 ms 171860 KB Output is correct
115 Correct 958 ms 172572 KB Output is correct
116 Correct 1301 ms 178892 KB Output is correct
117 Correct 1230 ms 180060 KB Output is correct
118 Correct 975 ms 173092 KB Output is correct
119 Correct 1224 ms 180276 KB Output is correct
120 Correct 1529 ms 226080 KB Output is correct
121 Correct 1287 ms 180660 KB Output is correct
122 Correct 1218 ms 185508 KB Output is correct
123 Correct 1292 ms 204964 KB Output is correct
124 Correct 1465 ms 179860 KB Output is correct