Submission #1032274

# Submission time Handle Problem Language Result Execution time Memory
1032274 2024-07-23T14:39:28 Z tvladm2009 Meetings 2 (JOI21_meetings2) C++17
100 / 100
484 ms 33472 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define all(a) (a).begin(), (a).end()

const int N = 2e5 + 5;

int n;
vector<int> g[N];
int init_sz[N], init_d[N];
bool used[N];
int sz[N], d[N], ans[N], first[N];

void build(int u, int p = -1, int cd = 0) {
    init_sz[u] = 1;
    init_d[u] = cd;
    for (auto v : g[u]) {
        if (v != p) {
            build(v, u, cd + 1);
            init_sz[u] += init_sz[v];
        }
    }
}

int get_subtree_sz(int u, int v) {
    return init_d[u] < init_d[v] ? init_sz[v] : n - init_sz[u];
}

void dfs(int u, int p = -1, int cd = 0) {
    sz[u] = 1;
    d[u] = cd;
    for (auto v : g[u]) {
        if (!used[v] && v != p) {
            dfs(v, u, cd + 1);
            sz[u] += sz[v];
        }
    }
}

int fcen(int all_sz, int u, int p = -1) {
    for (auto v : g[u]) {
        if (!used[v] && v != p && 2 * sz[v] > all_sz) {
            return fcen(all_sz, v, u);
        }
    }
    return u;
}

void dfs_ans(int root, vector<pair<int, int>> &vs, int u, int p = -1) {
    if (u != root) {
        first[u] = p == root ? u : first[p];
        vs.push_back({get_subtree_sz(p, u), u});
        ans[2 * min(get_subtree_sz(p, u), get_subtree_sz(first[u], root))] = max(ans[2 * min(get_subtree_sz(p, u), get_subtree_sz(first[u], root))], d[u] + 1);
    }
    for (auto v : g[u]) {
        if (!used[v] && v != p) {
            dfs_ans(root, vs, v, u);
        }
    }
}

void solve(int root) {
    dfs(root);
    vector<pair<int, int>> vs;
    dfs_ans(root, vs, root);
    sort(all(vs));
    reverse(all(vs));
    vector<pair<int, int>> cur;
    for (auto [s, v] : vs) {
        for (auto [du, fu] : cur) {
            if (fu != first[v]) {
                ans[2 * s] = max(ans[2 * s], du + d[v] + 1);
            }
        }
        cur.push_back({d[v], first[v]});
        sort(all(cur));
        bool cont = true;
        for (int i = 0; i < cur.size() && cont; ++i) {
            for (int j = i + 1; j < cur.size() && cont; ++j) {
                if (cur[i].second == cur[j].second) {
                    cur.erase(cur.begin() + i);
                    cont = false;
                }
            }
        }
        if (cur.size() == 3) {
            cur.erase(cur.begin());
        }
    }
    used[root] = true;
    for (auto v : g[root]) {
        if (!used[v]) {
            solve(fcen(sz[v], v));
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for (int i = 1; i <= n; ++i) {
        ans[i] = 1;
    }
    build(0);
    dfs(0);
    solve(fcen(n, 0));
    for (int i = n - 2; i >= 0; --i) {
        ans[i] = max(ans[i], ans[i + 2]);
    }
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << "\n";
    }
    return 0;
}

Compilation message

meetings2.cpp: In function 'void solve(int)':
meetings2.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (int i = 0; i < cur.size() && cont; ++i) {
      |                         ~~^~~~~~~~~~~~
meetings2.cpp:82:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |             for (int j = i + 1; j < cur.size() && cont; ++j) {
      |                                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5160 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5080 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4952 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 2 ms 5160 KB Output is correct
15 Correct 2 ms 4952 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 5136 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 2 ms 5156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5160 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5080 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4952 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 2 ms 5160 KB Output is correct
15 Correct 2 ms 4952 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 5136 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 2 ms 5156 KB Output is correct
22 Correct 6 ms 5468 KB Output is correct
23 Correct 6 ms 5536 KB Output is correct
24 Correct 6 ms 5468 KB Output is correct
25 Correct 6 ms 5532 KB Output is correct
26 Correct 6 ms 5432 KB Output is correct
27 Correct 5 ms 5468 KB Output is correct
28 Correct 6 ms 5464 KB Output is correct
29 Correct 6 ms 5468 KB Output is correct
30 Correct 6 ms 5308 KB Output is correct
31 Correct 6 ms 5468 KB Output is correct
32 Correct 7 ms 5468 KB Output is correct
33 Correct 6 ms 5724 KB Output is correct
34 Correct 4 ms 5468 KB Output is correct
35 Correct 4 ms 5472 KB Output is correct
36 Correct 6 ms 5556 KB Output is correct
37 Correct 5 ms 5428 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 5160 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 5080 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4952 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 4956 KB Output is correct
14 Correct 2 ms 5160 KB Output is correct
15 Correct 2 ms 4952 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 5136 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 2 ms 5156 KB Output is correct
22 Correct 6 ms 5468 KB Output is correct
23 Correct 6 ms 5536 KB Output is correct
24 Correct 6 ms 5468 KB Output is correct
25 Correct 6 ms 5532 KB Output is correct
26 Correct 6 ms 5432 KB Output is correct
27 Correct 5 ms 5468 KB Output is correct
28 Correct 6 ms 5464 KB Output is correct
29 Correct 6 ms 5468 KB Output is correct
30 Correct 6 ms 5308 KB Output is correct
31 Correct 6 ms 5468 KB Output is correct
32 Correct 7 ms 5468 KB Output is correct
33 Correct 6 ms 5724 KB Output is correct
34 Correct 4 ms 5468 KB Output is correct
35 Correct 4 ms 5472 KB Output is correct
36 Correct 6 ms 5556 KB Output is correct
37 Correct 5 ms 5428 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
39 Correct 343 ms 22348 KB Output is correct
40 Correct 348 ms 21984 KB Output is correct
41 Correct 342 ms 21956 KB Output is correct
42 Correct 340 ms 22468 KB Output is correct
43 Correct 314 ms 22132 KB Output is correct
44 Correct 371 ms 22316 KB Output is correct
45 Correct 484 ms 28092 KB Output is correct
46 Correct 463 ms 33472 KB Output is correct
47 Correct 147 ms 21324 KB Output is correct
48 Correct 78 ms 21408 KB Output is correct
49 Correct 336 ms 23452 KB Output is correct
50 Correct 119 ms 22912 KB Output is correct
51 Correct 265 ms 30520 KB Output is correct