Submission #869870

# Submission time Handle Problem Language Result Execution time Memory
869870 2023-11-06T03:17:44 Z garam1732 Meetings 2 (JOI21_meetings2) C++14
100 / 100
716 ms 42628 KB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define bl " "
#define endl "\n"
#define all(v) v.begin(), v.end()
#define comp(v) v.erase(unique(all(v)), v.end())
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 200200;
const int MOD = 998244353;

vector<int> adj[MAXN];
priority_queue<pi> pq[MAXN], q;
set<pi> st;

bool vst[MAXN];
int res[MAXN], num[MAXN], tmp[MAXN];
int dst[MAXN], cnt[MAXN];

void dfs1(int here, int par) {
    cnt[here] = 1;

    if(vst[here]) return;
    for(int there : adj[here]) {
        if(there == par) continue;

        dfs1(there, here);
        cnt[here] += cnt[there];
    }
}

void dfs2(int here, int par, int t) {
    if(!vst[here]) {
        for(int there : adj[here]) {
            if(there == par) continue;

            dst[there] = dst[here] + 1;
            dfs2(there, here, t);
        }
    }

    int x = (num[here] < num[par] ? num[here] : num[1]-num[par]);
    pq[t].push(pi(x, dst[here]));
}

void solve(int here) {
    dfs1(here, here);

    int n = cnt[here];
    for(int par = here; ; ) {
        bool flag = true;
        for(int there : adj[here]) {
            if(there == par || vst[there]) continue;
            if(cnt[there]*2 > n) {
                flag = false;
                par = here; here = there;
                break;
            }
        }

        if(flag) break;
    }

    dst[here] = 0; int t = 0;
    for(int there : adj[here]) {
        while(pq[t].size()) pq[t].pop();
        dst[there] = dst[here] + 1;
        dfs2(there, here, t);

        tmp[t] = 0;
        t++;
    }

    if(t == 0) return;

    while(q.size()) q.pop();
    for(int i = 0; i < t; i++) q.push(pi(pq[i].top().ff, i));

    st.clear();
    int mx = q.top().ff;
    while(q.size()) {
        while(q.size() && q.top().ff == mx) {
            pi x = q.top(); q.pop();
            if(st.find(pi(tmp[x.ss], x.ss)) != st.end()) st.erase(pi(tmp[x.ss], x.ss));
            tmp[x.ss] = max(tmp[x.ss], pq[x.ss].top().ss);
            st.insert(pi(tmp[x.ss], x.ss));
            pq[x.ss].pop();
            if(pq[x.ss].size()) q.push(pi(pq[x.ss].top().ff, x.ss));
        }

        //cout<<mx<<bl<<it->ff<<bl<<next(it)->ff<<endl;
        if(st.size() > 1) {
            auto it = prev(st.end());
            res[mx] = max(res[mx], it->ff+prev(it)->ff+1);
        }
        if(q.size()) mx = q.top().ff;
    }

    vst[here] = 1;
    for(int there : adj[here]) {
        if(!vst[there])
            solve(there);
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int n; cin >> n;
    for(int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs1(1, 1);

    for(int i = 1; i <= n; i++) num[i] = cnt[i];

    int idx = 0;
    for(int i = 2; i <= n; i++)
        idx = max(idx, min(cnt[i], n-cnt[i]));

    res[n] = 1; res[idx] = 2;

    solve(1);

    for(int i = n-1; i; i--) res[i] = max(res[i], res[i+1]);
    for(int i = 1; i <= n; i++) cout << (i%2 ? 1 : res[i/2]) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15448 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 3 ms 15368 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15372 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15332 KB Output is correct
21 Correct 4 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15448 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 3 ms 15368 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15372 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15332 KB Output is correct
21 Correct 4 ms 15196 KB Output is correct
22 Correct 11 ms 15380 KB Output is correct
23 Correct 9 ms 15452 KB Output is correct
24 Correct 9 ms 15608 KB Output is correct
25 Correct 9 ms 15608 KB Output is correct
26 Correct 9 ms 15596 KB Output is correct
27 Correct 8 ms 15464 KB Output is correct
28 Correct 8 ms 15452 KB Output is correct
29 Correct 8 ms 15452 KB Output is correct
30 Correct 9 ms 15452 KB Output is correct
31 Correct 8 ms 15448 KB Output is correct
32 Correct 11 ms 15708 KB Output is correct
33 Correct 9 ms 15708 KB Output is correct
34 Correct 6 ms 15728 KB Output is correct
35 Correct 5 ms 15708 KB Output is correct
36 Correct 8 ms 15704 KB Output is correct
37 Correct 5 ms 15708 KB Output is correct
38 Correct 8 ms 15848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15196 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15448 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 3 ms 15368 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15372 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 3 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15332 KB Output is correct
21 Correct 4 ms 15196 KB Output is correct
22 Correct 11 ms 15380 KB Output is correct
23 Correct 9 ms 15452 KB Output is correct
24 Correct 9 ms 15608 KB Output is correct
25 Correct 9 ms 15608 KB Output is correct
26 Correct 9 ms 15596 KB Output is correct
27 Correct 8 ms 15464 KB Output is correct
28 Correct 8 ms 15452 KB Output is correct
29 Correct 8 ms 15452 KB Output is correct
30 Correct 9 ms 15452 KB Output is correct
31 Correct 8 ms 15448 KB Output is correct
32 Correct 11 ms 15708 KB Output is correct
33 Correct 9 ms 15708 KB Output is correct
34 Correct 6 ms 15728 KB Output is correct
35 Correct 5 ms 15708 KB Output is correct
36 Correct 8 ms 15704 KB Output is correct
37 Correct 5 ms 15708 KB Output is correct
38 Correct 8 ms 15848 KB Output is correct
39 Correct 506 ms 26688 KB Output is correct
40 Correct 472 ms 26824 KB Output is correct
41 Correct 455 ms 26552 KB Output is correct
42 Correct 460 ms 26832 KB Output is correct
43 Correct 487 ms 26864 KB Output is correct
44 Correct 467 ms 27136 KB Output is correct
45 Correct 716 ms 31556 KB Output is correct
46 Correct 596 ms 36176 KB Output is correct
47 Correct 181 ms 36684 KB Output is correct
48 Correct 156 ms 42628 KB Output is correct
49 Correct 427 ms 27476 KB Output is correct
50 Correct 179 ms 35552 KB Output is correct
51 Correct 374 ms 41404 KB Output is correct