Submission #571876

# Submission time Handle Problem Language Result Execution time Memory
571876 2022-06-03T03:27:55 Z dantoh000 Meetings 2 (JOI21_meetings2) C++14
100 / 100
339 ms 40592 KB
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> G[200005];
int sz[200005];
int p[18][200005];
int d[200005];
int ord[200005];
int ans[200005];
bool cmp(int a, int b){
    return sz[a] > sz[b];
}
void dfs(int u){
    sz[u] = 1;
    for (auto v: G[u]){
        if (v == p[0][u]) continue;
        p[0][v] = u;
        d[v] = d[u]+1;
        dfs(v);
        sz[u] += sz[v];
    }
}
int dfs2(int u){
    for (auto v : G[u]){
        if (v == p[0][u]) continue;
        if (sz[v] > n/2) return dfs2(v);
    }
    return u;
}
int lca(int u, int v){
    if (d[u] < d[v]) swap(u,v);
    int diff = d[u]-d[v];
    for (int k = 17; k >= 0; k--){
        if (diff>>k&1) u = p[k][u];
    }
    if (u == v) return u;
    for (int k = 17; k >= 0; k--){
        if (p[k][u] && p[k][v] && p[k][u] != p[k][v]){
            u = p[k][u];
            v = p[k][v];
        }
    }
    assert(p[0][u] == p[0][v]);
    return p[0][u];
}
int dist(int u, int v){
    return d[u] + d[v] - 2*d[lca(u,v)];
}
int main(){
    scanf("%d",&n);
    for (int i = 1,u,v; i < n; i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1);
    int c = dfs2(1);
    for (int i = 1; i <= n; i++){
        p[0][i] = 0;
    }
    d[c] = 0;
    dfs(c);
    for (int k = 1; k < 18; k++){
        for (int i = 1; i <= n; i++){
            p[k][i] = p[k-1][p[k-1][i]];
        }
    }
    iota(ord, ord+n, 1);
    sort(ord, ord+n, cmp);
    int end1 = c, end2 = c;
    int L = 1;
    for (int i = 0; i < n; i++){
        int j = ord[i];
        ///printf("%d %d\n",j,sz[j]);
        int d1 = dist(j, end1) + 1;
        int d2 = dist(j, end2) + 1;
        if (d1 < d2){
            swap(d1,d2);
            swap(end1,end2);
        }
        if (d1 > L){
            L = d1;
            end2 = j;
        }
        ans[sz[j]] = L;
    }
    for (int i = n; i >= 1; i--){
        ans[i] = max(ans[i], ans[i+1]);
    }
    for (int i = 1; i <= n; i++){
        if (i%2 == 1) printf("1\n");
        else printf("%d\n",ans[i/2]);
    }


}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
meetings2.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         scanf("%d%d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 5036 KB Output is correct
4 Correct 3 ms 5000 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 5036 KB Output is correct
4 Correct 3 ms 5000 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 5 ms 5532 KB Output is correct
23 Correct 5 ms 5588 KB Output is correct
24 Correct 5 ms 5588 KB Output is correct
25 Correct 5 ms 5588 KB Output is correct
26 Correct 5 ms 5588 KB Output is correct
27 Correct 5 ms 5588 KB Output is correct
28 Correct 6 ms 5588 KB Output is correct
29 Correct 6 ms 5528 KB Output is correct
30 Correct 6 ms 5528 KB Output is correct
31 Correct 6 ms 5524 KB Output is correct
32 Correct 5 ms 5656 KB Output is correct
33 Correct 6 ms 5784 KB Output is correct
34 Correct 5 ms 5588 KB Output is correct
35 Correct 6 ms 5588 KB Output is correct
36 Correct 5 ms 5588 KB Output is correct
37 Correct 5 ms 5588 KB Output is correct
38 Correct 6 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 5036 KB Output is correct
4 Correct 3 ms 5000 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5004 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 5 ms 5532 KB Output is correct
23 Correct 5 ms 5588 KB Output is correct
24 Correct 5 ms 5588 KB Output is correct
25 Correct 5 ms 5588 KB Output is correct
26 Correct 5 ms 5588 KB Output is correct
27 Correct 5 ms 5588 KB Output is correct
28 Correct 6 ms 5588 KB Output is correct
29 Correct 6 ms 5528 KB Output is correct
30 Correct 6 ms 5528 KB Output is correct
31 Correct 6 ms 5524 KB Output is correct
32 Correct 5 ms 5656 KB Output is correct
33 Correct 6 ms 5784 KB Output is correct
34 Correct 5 ms 5588 KB Output is correct
35 Correct 6 ms 5588 KB Output is correct
36 Correct 5 ms 5588 KB Output is correct
37 Correct 5 ms 5588 KB Output is correct
38 Correct 6 ms 5716 KB Output is correct
39 Correct 192 ms 31168 KB Output is correct
40 Correct 180 ms 30556 KB Output is correct
41 Correct 190 ms 31348 KB Output is correct
42 Correct 198 ms 31528 KB Output is correct
43 Correct 190 ms 31564 KB Output is correct
44 Correct 203 ms 31588 KB Output is correct
45 Correct 339 ms 36476 KB Output is correct
46 Correct 228 ms 40592 KB Output is correct
47 Correct 154 ms 31920 KB Output is correct
48 Correct 122 ms 32068 KB Output is correct
49 Correct 238 ms 32136 KB Output is correct
50 Correct 136 ms 32172 KB Output is correct
51 Correct 170 ms 39528 KB Output is correct