Submission #390177

# Submission time Handle Problem Language Result Execution time Memory
390177 2021-04-15T13:49:27 Z georgerapeanu Meetings 2 (JOI21_meetings2) C++11
100 / 100
555 ms 50200 KB
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 2e5;
const int LGMAX = 18;

int n;
vector<int> graph[NMAX + 5];
int weight[NMAX + 5];

int father[LGMAX + 1][NMAX + 1];
int lvl[NMAX + 1];

void predfs(int nod,int tata){
    weight[nod] = 1;
    father[0][nod] = tata;
    lvl[nod] = 1 + lvl[tata];
    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        predfs(it,nod);
        weight[nod] += weight[it];
    }
}

int get_centroid(int nod){
    int tata = 0;

    while(true){
        int bigChild = -1;
        for(auto it:graph[nod]){
            if(it == tata){
                continue;
            }
            if(bigChild == -1 || weight[it] > weight[bigChild]){
                bigChild = it;
            }
        }
        if(bigChild == -1 || weight[bigChild] * 2 <= n){
            break;
        }
        tata = nod;
        nod = bigChild;
    }

    return nod;
}

vector<int> events[NMAX + 5];
map<int,int> stuff;

vector<int> nodes[NMAX + 5];

int lca(int u,int v){
    if(lvl[u] > lvl[v]){
        swap(u,v);
    }

    for(int h = LGMAX;h >= 0;h--){
        if((lvl[v] - lvl[u]) >> h){
            v = father[h][v];
        }
    }

    if(u == v){
        return u;
    }

    for(int h = LGMAX;h >= 0;h--){
        if(father[h][u] != father[h][v]){
            u = father[h][u];
            v = father[h][v];
        }
    }

    return father[0][v];
}

int dist(int u,int v){
    return lvl[u] + lvl[v] - 2 * lvl[lca(u,v)] + 1;
}

int ans[NMAX + 5];

int main(){

    scanf("%d",&n);

    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d %d",&x,&y);

        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    predfs(1,0);
    int root = get_centroid(1);
    predfs(root,0);

    for(int i = 1;i <= n;i++){
        if(root != i){
            nodes[weight[i]].push_back(i);
        }
    }

    for(int h = 1;h <= LGMAX;h++){
        for(int i = 1;i <= n;i++){
            father[h][i] = father[h - 1][father[h - 1][i]];
        }
    }

    int u = root,v = root;
    int d = 1;

    for(int i = n / 2;i >= 1;i--){
        for(auto it:nodes[i]){
            int c = dist(it,u);
            int e = dist(it,v);
            if(c > d){
                d = c;
                v = it;
            }
            else if(e > d){
                d = e;
                u = it;
            }
        }
        ans[i] = d;
    }

    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:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   89 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
meetings2.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   93 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14516 KB Output is correct
4 Correct 9 ms 14508 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14496 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14476 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 9 ms 14396 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14440 KB Output is correct
18 Correct 8 ms 14476 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14516 KB Output is correct
4 Correct 9 ms 14508 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14496 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14476 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 9 ms 14396 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14440 KB Output is correct
18 Correct 8 ms 14476 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 12 ms 14924 KB Output is correct
23 Correct 12 ms 15008 KB Output is correct
24 Correct 12 ms 14924 KB Output is correct
25 Correct 12 ms 14924 KB Output is correct
26 Correct 12 ms 14924 KB Output is correct
27 Correct 13 ms 14924 KB Output is correct
28 Correct 12 ms 15008 KB Output is correct
29 Correct 12 ms 14924 KB Output is correct
30 Correct 12 ms 14924 KB Output is correct
31 Correct 12 ms 14924 KB Output is correct
32 Correct 13 ms 15068 KB Output is correct
33 Correct 12 ms 15248 KB Output is correct
34 Correct 11 ms 14924 KB Output is correct
35 Correct 11 ms 15016 KB Output is correct
36 Correct 12 ms 14924 KB Output is correct
37 Correct 11 ms 14920 KB Output is correct
38 Correct 12 ms 15080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14516 KB Output is correct
4 Correct 9 ms 14508 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 9 ms 14496 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14412 KB Output is correct
11 Correct 9 ms 14412 KB Output is correct
12 Correct 9 ms 14412 KB Output is correct
13 Correct 9 ms 14476 KB Output is correct
14 Correct 9 ms 14412 KB Output is correct
15 Correct 9 ms 14396 KB Output is correct
16 Correct 9 ms 14412 KB Output is correct
17 Correct 9 ms 14440 KB Output is correct
18 Correct 8 ms 14476 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 9 ms 14412 KB Output is correct
21 Correct 9 ms 14412 KB Output is correct
22 Correct 12 ms 14924 KB Output is correct
23 Correct 12 ms 15008 KB Output is correct
24 Correct 12 ms 14924 KB Output is correct
25 Correct 12 ms 14924 KB Output is correct
26 Correct 12 ms 14924 KB Output is correct
27 Correct 13 ms 14924 KB Output is correct
28 Correct 12 ms 15008 KB Output is correct
29 Correct 12 ms 14924 KB Output is correct
30 Correct 12 ms 14924 KB Output is correct
31 Correct 12 ms 14924 KB Output is correct
32 Correct 13 ms 15068 KB Output is correct
33 Correct 12 ms 15248 KB Output is correct
34 Correct 11 ms 14924 KB Output is correct
35 Correct 11 ms 15016 KB Output is correct
36 Correct 12 ms 14924 KB Output is correct
37 Correct 11 ms 14920 KB Output is correct
38 Correct 12 ms 15080 KB Output is correct
39 Correct 278 ms 38772 KB Output is correct
40 Correct 273 ms 38204 KB Output is correct
41 Correct 271 ms 38792 KB Output is correct
42 Correct 290 ms 39032 KB Output is correct
43 Correct 304 ms 39016 KB Output is correct
44 Correct 295 ms 39100 KB Output is correct
45 Correct 555 ms 45596 KB Output is correct
46 Correct 297 ms 50200 KB Output is correct
47 Correct 203 ms 39372 KB Output is correct
48 Correct 166 ms 39476 KB Output is correct
49 Correct 398 ms 39828 KB Output is correct
50 Correct 195 ms 39556 KB Output is correct
51 Correct 232 ms 48048 KB Output is correct