답안 #1004757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004757 2024-06-21T13:46:44 Z Andrey Meetings 2 (JOI21_meetings2) C++14
100 / 100
95 ms 33860 KB
#include<bits/stdc++.h>
using namespace std;

vector<int> haha[200001];
vector<int> st(200001);
vector<int> wow[200001];
vector<int> ans(200001,0);
vector<int> dep(200001);

void dfs(int a, int t) {
    st[a] = 1;
    for(int v: haha[a]) {
        if(v != t) {
            dfs(v,a);
            st[a]+=st[v];
        }
    }
}

void dude(int a, int t, int d) {
    st[a] = 1;
    int p = -1,big = 0;
    dep[a] = d;
    for(int v: haha[a]) {
        if(v != t) {
            dude(v,a,d+1);
            st[a]+=st[v];
            if(st[v] > big) {
                big = st[v];
                p = v;
            }
        }
    }
    if(p == -1) {
        wow[a].push_back(d);
    }
    else {
        swap(wow[a],wow[p]);
        for(int v: haha[a]) {
            if(v != t && v != p) {
                for(int j = 0; j < wow[v].size(); j++) {
                    ans[j+1] = max(ans[j+1],wow[v][j]+wow[a][j]-2*d);
                    wow[a][j] = max(wow[a][j],wow[v][j]);
                }
                for(int j = wow[v].size()-2; j >= 0; j--) {
                    wow[v][j] = max(wow[v][j],wow[v][j+1]);
                }
            }
        }
        for(int i = wow[a].size(); i < st[a]; i++) {
            wow[a].push_back(d);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,a,b;
    cin >> n;
    for(int i = 0; i < n-1; i++) {
        cin >> a >> b;
        haha[a].push_back(b);
        haha[b].push_back(a);
    }
    dfs(1,-1);
    int c = 1;
    bool yeah = true;
    while(yeah) {
        yeah = false;
        for(int v: haha[c]) {
            if(st[v] > n/2 && st[v] < st[c]) {
                c = v;
                yeah = true;
                break;
            }
        }
    }
    dude(c,-1,0);
    for(int i = 1; i <= n; i++) {
        if(i != c) {
            ans[st[i]] = max(ans[st[i]],dep[i]);
        }
    }
    for(int i = n-1; i >= 1; i--) {
        ans[i] = max(ans[i],ans[i+1]);
    }
    for(int i = 1; i <= n; i++) {
        if(i%2 == 0) {
            cout << ans[i/2]+1 << "\n";
        }
        else {
            cout << 1 << "\n";
        }
    }
    return 0;
}

Compilation message

meetings2.cpp: In function 'void dude(int, int, int)':
meetings2.cpp:41:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |                 for(int j = 0; j < wow[v].size(); j++) {
      |                                ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12120 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12220 KB Output is correct
5 Correct 2 ms 12124 KB Output is correct
6 Correct 2 ms 12124 KB Output is correct
7 Correct 2 ms 12124 KB Output is correct
8 Correct 2 ms 12220 KB Output is correct
9 Correct 3 ms 12124 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 3 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 3 ms 12124 KB Output is correct
16 Correct 3 ms 12124 KB Output is correct
17 Correct 3 ms 12156 KB Output is correct
18 Correct 2 ms 12124 KB Output is correct
19 Correct 3 ms 12220 KB Output is correct
20 Correct 3 ms 12124 KB Output is correct
21 Correct 3 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12120 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12220 KB Output is correct
5 Correct 2 ms 12124 KB Output is correct
6 Correct 2 ms 12124 KB Output is correct
7 Correct 2 ms 12124 KB Output is correct
8 Correct 2 ms 12220 KB Output is correct
9 Correct 3 ms 12124 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 3 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 3 ms 12124 KB Output is correct
16 Correct 3 ms 12124 KB Output is correct
17 Correct 3 ms 12156 KB Output is correct
18 Correct 2 ms 12124 KB Output is correct
19 Correct 3 ms 12220 KB Output is correct
20 Correct 3 ms 12124 KB Output is correct
21 Correct 3 ms 12124 KB Output is correct
22 Correct 4 ms 12368 KB Output is correct
23 Correct 4 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
25 Correct 4 ms 12380 KB Output is correct
26 Correct 4 ms 12380 KB Output is correct
27 Correct 4 ms 12380 KB Output is correct
28 Correct 4 ms 12380 KB Output is correct
29 Correct 4 ms 12380 KB Output is correct
30 Correct 6 ms 12636 KB Output is correct
31 Correct 4 ms 12460 KB Output is correct
32 Correct 4 ms 12484 KB Output is correct
33 Correct 4 ms 12636 KB Output is correct
34 Correct 4 ms 12632 KB Output is correct
35 Correct 4 ms 12376 KB Output is correct
36 Correct 4 ms 12412 KB Output is correct
37 Correct 4 ms 12380 KB Output is correct
38 Correct 4 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 3 ms 12120 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 3 ms 12220 KB Output is correct
5 Correct 2 ms 12124 KB Output is correct
6 Correct 2 ms 12124 KB Output is correct
7 Correct 2 ms 12124 KB Output is correct
8 Correct 2 ms 12220 KB Output is correct
9 Correct 3 ms 12124 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 3 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 3 ms 12124 KB Output is correct
16 Correct 3 ms 12124 KB Output is correct
17 Correct 3 ms 12156 KB Output is correct
18 Correct 2 ms 12124 KB Output is correct
19 Correct 3 ms 12220 KB Output is correct
20 Correct 3 ms 12124 KB Output is correct
21 Correct 3 ms 12124 KB Output is correct
22 Correct 4 ms 12368 KB Output is correct
23 Correct 4 ms 12380 KB Output is correct
24 Correct 5 ms 12380 KB Output is correct
25 Correct 4 ms 12380 KB Output is correct
26 Correct 4 ms 12380 KB Output is correct
27 Correct 4 ms 12380 KB Output is correct
28 Correct 4 ms 12380 KB Output is correct
29 Correct 4 ms 12380 KB Output is correct
30 Correct 6 ms 12636 KB Output is correct
31 Correct 4 ms 12460 KB Output is correct
32 Correct 4 ms 12484 KB Output is correct
33 Correct 4 ms 12636 KB Output is correct
34 Correct 4 ms 12632 KB Output is correct
35 Correct 4 ms 12376 KB Output is correct
36 Correct 4 ms 12412 KB Output is correct
37 Correct 4 ms 12380 KB Output is correct
38 Correct 4 ms 12380 KB Output is correct
39 Correct 95 ms 28748 KB Output is correct
40 Correct 92 ms 28236 KB Output is correct
41 Correct 85 ms 28616 KB Output is correct
42 Correct 90 ms 28872 KB Output is correct
43 Correct 89 ms 28796 KB Output is correct
44 Correct 94 ms 28876 KB Output is correct
45 Correct 80 ms 30660 KB Output is correct
46 Correct 78 ms 33860 KB Output is correct
47 Correct 82 ms 27212 KB Output is correct
48 Correct 77 ms 29264 KB Output is correct
49 Correct 83 ms 27860 KB Output is correct
50 Correct 73 ms 30020 KB Output is correct
51 Correct 86 ms 32456 KB Output is correct