Submission #444095

# Submission time Handle Problem Language Result Execution time Memory
444095 2021-07-13T05:57:42 Z PeppaPig Meetings 2 (JOI21_meetings2) C++14
100 / 100
1838 ms 27308 KB
#include <bits/stdc++.h>

#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 2e5 + 5;

struct SegmentTree {
    int t[N << 1];

    void update(int x, int k) {
        for(t[x += N] = k; x != 1; x >>= 1)
            t[x >> 1] = max(t[x], t[x ^ 1]);
    }

    int query(int l, int r) {
        int ret = 0;
        for(l += N, r += N + 1; l < r; l >>= 1, r >>= 1) {
            if(l & 1) ret = max(ret, t[l++]);
            if(r & 1) ret = max(ret, t[--r]);
        }
        return ret;
    }
} tree;

int n;
int ans[N];
vector<int> g[N];

int all;
int sz[N], chk[N];

int get_sz(int u, int p) {
    sz[u] = 1;
    for(int v : g[u]) if(v != p && !chk[v])
        sz[u] += get_sz(v, u);
    return sz[u];
}

int find_cen(int u, int p, pii &ret) {
    int mx = all - sz[u];
    for(int v : g[u]) if(v != p && !chk[v])
        mx = max(mx, find_cen(v, u, ret));
    ret = min(ret, pii(mx, u));
    return sz[u];
}

int dfs(int u, int p, int dist, bool fill) {
    sz[u] = 1;
    for(int v : g[u]) if(v != p && !chk[v])
        sz[u] += dfs(v, u, dist + 1, fill);

    if(!fill) {
        if(tree.query(sz[u], all) != -1e9)
            ans[sz[u]] = max(ans[sz[u]], dist + tree.query(sz[u], all) + 1);
        if(sz[u] <= all - sz[u])
            ans[sz[u]] = max(ans[sz[u]], dist + 1);
    } else if(dist > tree.t[sz[u] + N])
        tree.update(sz[u], dist);

    return sz[u];
}

void centroid(int u) {
    get_sz(u, u);

    all = sz[u];

    pii ret(all + 1, -1);
    find_cen(u, u, ret);
    u = ret.y;

    for(int i = 1; i <= all; i++)
        tree.update(i, -1e9);
    for(int v : g[u]) if(!chk[v]) {
        dfs(v, u, 1, 0);
        dfs(v, u, 1, 1);
    }
    for(int i = 1; i <= all; i++)
        tree.update(i, -1e9);
    for(auto it = g[u].rbegin(); it != g[u].rend(); it++) if(!chk[*it]) {
        dfs(*it, u, 1, 0);
        dfs(*it, u, 1, 1);
    }

    chk[u] = 1;
    for(int v : g[u]) if(!chk[v])
        centroid(v);
}

int main() {
    fill_n(ans, N, 1);

    scanf("%d", &n);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    centroid(1);

    for(int i = n / 2; i; i--)
        ans[i] = max(ans[i], ans[i + 1]);

    for(int i = 1; i <= n; i++) {
        if(i & 1) printf("1\n");
        else printf("%d\n", ans[i / 2]);
    }

    return 0;
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
meetings2.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5708 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 4 ms 5836 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5788 KB Output is correct
11 Correct 4 ms 5788 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5836 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 4 ms 5836 KB Output is correct
16 Correct 4 ms 5708 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 4 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5708 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 4 ms 5836 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5788 KB Output is correct
11 Correct 4 ms 5788 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5836 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 4 ms 5836 KB Output is correct
16 Correct 4 ms 5708 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 4 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 11 ms 5964 KB Output is correct
23 Correct 11 ms 6060 KB Output is correct
24 Correct 11 ms 5964 KB Output is correct
25 Correct 13 ms 6068 KB Output is correct
26 Correct 12 ms 6072 KB Output is correct
27 Correct 11 ms 6072 KB Output is correct
28 Correct 11 ms 5964 KB Output is correct
29 Correct 11 ms 5964 KB Output is correct
30 Correct 11 ms 6060 KB Output is correct
31 Correct 11 ms 5964 KB Output is correct
32 Correct 16 ms 6164 KB Output is correct
33 Correct 16 ms 6256 KB Output is correct
34 Correct 7 ms 6060 KB Output is correct
35 Correct 7 ms 5964 KB Output is correct
36 Correct 11 ms 6092 KB Output is correct
37 Correct 8 ms 6092 KB Output is correct
38 Correct 12 ms 6092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 4 ms 5708 KB Output is correct
4 Correct 4 ms 5708 KB Output is correct
5 Correct 4 ms 5836 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 4 ms 5836 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5788 KB Output is correct
11 Correct 4 ms 5788 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5836 KB Output is correct
14 Correct 4 ms 5708 KB Output is correct
15 Correct 4 ms 5836 KB Output is correct
16 Correct 4 ms 5708 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 4 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 11 ms 5964 KB Output is correct
23 Correct 11 ms 6060 KB Output is correct
24 Correct 11 ms 5964 KB Output is correct
25 Correct 13 ms 6068 KB Output is correct
26 Correct 12 ms 6072 KB Output is correct
27 Correct 11 ms 6072 KB Output is correct
28 Correct 11 ms 5964 KB Output is correct
29 Correct 11 ms 5964 KB Output is correct
30 Correct 11 ms 6060 KB Output is correct
31 Correct 11 ms 5964 KB Output is correct
32 Correct 16 ms 6164 KB Output is correct
33 Correct 16 ms 6256 KB Output is correct
34 Correct 7 ms 6060 KB Output is correct
35 Correct 7 ms 5964 KB Output is correct
36 Correct 11 ms 6092 KB Output is correct
37 Correct 8 ms 6092 KB Output is correct
38 Correct 12 ms 6092 KB Output is correct
39 Correct 1066 ms 18084 KB Output is correct
40 Correct 970 ms 17852 KB Output is correct
41 Correct 1000 ms 18092 KB Output is correct
42 Correct 969 ms 18376 KB Output is correct
43 Correct 1029 ms 18392 KB Output is correct
44 Correct 1004 ms 18500 KB Output is correct
45 Correct 1838 ms 23132 KB Output is correct
46 Correct 1540 ms 27308 KB Output is correct
47 Correct 280 ms 18656 KB Output is correct
48 Correct 188 ms 18772 KB Output is correct
49 Correct 979 ms 18876 KB Output is correct
50 Correct 307 ms 18876 KB Output is correct
51 Correct 889 ms 26228 KB Output is correct