답안 #893536

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893536 2023-12-27T06:33:28 Z vjudge1 Meetings 2 (JOI21_meetings2) C++17
100 / 100
1422 ms 40888 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int N = 2e5 + 1;

const int inf = 1e15;

int n;

struct SegTree{
    int T[N * 4];
    SegTree(){
        for ( int i = 0; i < N * 4; i++ ){
            T[i] = -inf;
        }
    }
    void upd(int v, int l, int r, int pos, int vl, bool set){
        if ( l == r ){
            if ( set ){
                T[v] = vl;
            } else chmax(T[v], vl);
            return;
        }
        int md = (l + r) >> 1;
        if ( pos <= md ){
            upd(v * 2, l, md, pos, vl, set);
        } else{
            upd(v * 2 + 1, md + 1, r, pos, vl, set);
        }
        T[v] = max(T[v * 2], T[v * 2 + 1]);
    }
    void upd(int pos, int vl, bool set){
        upd(1, 0, n, pos, vl, set);
    }
    int get(int v, int l, int r, int tl, int tr){
        if ( l > tr or r < tl ){
            return -inf;
        }
        if ( tl <= l && tr >= r ){
            return T[v];
        }
        int md = (l + r) >> 1;
        return max(get(v * 2, l, md, tl, tr), get(v * 2 + 1, md + 1, r, tl, tr));
    }
    int get(int l, int r){
        return get(1, 0, n, l, r);
    }
} seg;

vector <int> G[N];

int us[N], sub[N];

void dfs(int u, int p){
    sub[u] = 1;
    for ( auto &v: G[u] ){
        if ( v != p && !us[v] ){
            dfs(v, u);
            sub[u] += sub[v];
        }
    }
}

int find(int u, int p, int des){
    for ( auto &v: G[u] ){
        if ( v != p && !us[v] ){
            if ( sub[v] >= des / 2 ){
                return find(v, u, des);
            }
        }
    }
    return u;
}

vector <vector<ar<int,2>>> t;

int sz;


int ans[N * 2];

void dfs2(int u, int p, int d){
    chmax(ans[min(sz, sub[u]) * 2], d + 1); // with root
    t.back().pb({sub[u], d});
    for ( auto &v: G[u] ){
        if ( v != p && !us[v] ){
            dfs2(v, u, d + 1);
        }
    }
}
void decompose(int rt){
    dfs(rt, -1);
    int u = find(rt, -1, sub[rt]);
    dfs(u, -1);
    us[u] = true;
    for ( auto &v: G[u] ){
        if ( us[v] ) continue;
        sz = sub[u] - sub[v];
        t.pb({});
        dfs2(v, u, 1);
    }
//    if ( u == 6 ){
//        for ( auto &x: t ){
//            for ( auto &[s, d]: x ){
//                cout << "{" << s << ", " << d << "}, ";
//            } cout << ln;
//        } cout << ln;
//    }
    { // prefix
        for ( auto &x: t ){
            for ( auto &[s, d]: x ){
                chmax(ans[s * 2], seg.get(s, n) + d + 1);
            }
            for ( auto &[s, d]: x ){
                seg.upd(s, d, false);
            }
        }
        for ( auto &x: t ){
            for ( auto &[s, d]: x ){
                seg.upd(s, -inf, true);
            }
        }
    } // suffix
    {
        reverse(all(t));
        for ( auto &x: t ){
            for ( auto &[s, d]: x ){
                chmax(ans[s * 2], seg.get(s, n) + d + 1);
            }
            for ( auto &[s, d]: x ){
                seg.upd(s, d, false);
            }
        }
        for ( auto &x: t ){
            for ( auto &[s, d]: x ){
                seg.upd(s, -inf, true);
            }
        }
    } t.clear();
//    cout << "Root : " << u << ln;
//    for ( int i = 1; i <= n; i++ ){
//        cout << ans[i] << ' ';
//        ans[i] = 0;
//    } cout << ln;
    for ( auto &v: G[u] ){
        if ( !us[v] ){
            decompose(v);
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    // 1 - indexed
    cin >> n;
    for ( int i = 1; i < n; i++ ){
        int u, v; cin >> u >> v;
        G[u].pb(v), G[v].pb(u);
    }
    decompose(1);
    for ( int i = n - (n & 1); i > 0; i -= 2 ){
        chmax(ans[i], ans[i + 2]);
    }
    for ( int i = 1; i <= n; i++ ){
        cout << (i & 1 ? 1LL : max(ans[i], 1LL)) << ln;
    }

    cout << '\n';
}

/*
7
1 7
3 2
2 6
5 6
6 4
4 7
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15452 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15452 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 2 ms 15452 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15448 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 2 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 2 ms 15452 KB Output is correct
21 Correct 3 ms 15460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15452 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15452 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 2 ms 15452 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15448 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 2 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 2 ms 15452 KB Output is correct
21 Correct 3 ms 15460 KB Output is correct
22 Correct 10 ms 15580 KB Output is correct
23 Correct 9 ms 15716 KB Output is correct
24 Correct 11 ms 15808 KB Output is correct
25 Correct 9 ms 15748 KB Output is correct
26 Correct 10 ms 15800 KB Output is correct
27 Correct 11 ms 15720 KB Output is correct
28 Correct 10 ms 15712 KB Output is correct
29 Correct 9 ms 15792 KB Output is correct
30 Correct 9 ms 15712 KB Output is correct
31 Correct 10 ms 15788 KB Output is correct
32 Correct 14 ms 15720 KB Output is correct
33 Correct 14 ms 15740 KB Output is correct
34 Correct 5 ms 15716 KB Output is correct
35 Correct 5 ms 15712 KB Output is correct
36 Correct 9 ms 15824 KB Output is correct
37 Correct 6 ms 15720 KB Output is correct
38 Correct 10 ms 15712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15452 KB Output is correct
2 Correct 3 ms 15452 KB Output is correct
3 Correct 3 ms 15452 KB Output is correct
4 Correct 3 ms 15452 KB Output is correct
5 Correct 2 ms 15452 KB Output is correct
6 Correct 3 ms 15452 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15448 KB Output is correct
14 Correct 4 ms 15704 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 2 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 3 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 2 ms 15452 KB Output is correct
21 Correct 3 ms 15460 KB Output is correct
22 Correct 10 ms 15580 KB Output is correct
23 Correct 9 ms 15716 KB Output is correct
24 Correct 11 ms 15808 KB Output is correct
25 Correct 9 ms 15748 KB Output is correct
26 Correct 10 ms 15800 KB Output is correct
27 Correct 11 ms 15720 KB Output is correct
28 Correct 10 ms 15712 KB Output is correct
29 Correct 9 ms 15792 KB Output is correct
30 Correct 9 ms 15712 KB Output is correct
31 Correct 10 ms 15788 KB Output is correct
32 Correct 14 ms 15720 KB Output is correct
33 Correct 14 ms 15740 KB Output is correct
34 Correct 5 ms 15716 KB Output is correct
35 Correct 5 ms 15712 KB Output is correct
36 Correct 9 ms 15824 KB Output is correct
37 Correct 6 ms 15720 KB Output is correct
38 Correct 10 ms 15712 KB Output is correct
39 Correct 794 ms 29764 KB Output is correct
40 Correct 776 ms 29916 KB Output is correct
41 Correct 799 ms 29124 KB Output is correct
42 Correct 785 ms 29680 KB Output is correct
43 Correct 782 ms 30148 KB Output is correct
44 Correct 820 ms 29684 KB Output is correct
45 Correct 1422 ms 35952 KB Output is correct
46 Correct 1406 ms 39620 KB Output is correct
47 Correct 236 ms 34152 KB Output is correct
48 Correct 156 ms 38200 KB Output is correct
49 Correct 875 ms 30884 KB Output is correct
50 Correct 249 ms 35304 KB Output is correct
51 Correct 772 ms 40888 KB Output is correct