답안 #496230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496230 2021-12-21T03:30:25 Z Rainbowbunny Meetings 2 (JOI21_meetings2) C++17
100 / 100
821 ms 26772 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 5;

int n;
int sz[MAXN], ans[MAXN], H[MAXN], nw[MAXN];
bool Mark[MAXN];
vector <int> Adj[MAXN];

void DFS(int node, int p)
{
    sz[node] = 1;
    for(auto x : Adj[node])
    {
        if(x == p or Mark[x])
        {
            continue;
        }
        DFS(x, node);
        sz[node] += sz[x];
    }
}

int FindCen(int node, int p, int cursz)
{
    for(auto x : Adj[node])
    {
        if(x == p or Mark[x])
        {
            continue;
        }
        if(2 * sz[x] > cursz)
        {
            return FindCen(x, node, cursz);
        }
    }
    return node;
}

vector <int> Cur;

void Cal(int node, int p)
{
    sz[node] = 1;
    for(auto x : Adj[node])
    {
        if(x == p or Mark[x])
        {
            continue;
        }
        H[x] = H[node] + 1;
        Cal(x, node);
        sz[node] += sz[x];
    }
    if(Cur.empty() or Cur[0] < sz[node])
    {
        // do nothing
    }
    else
    {
        int id = upper_bound(Cur.begin(), Cur.end(), sz[node], greater <int> ()) - Cur.begin() - 1;
        assert(id >= 0);
        ans[2 * sz[node]] = max(ans[2 * sz[node]], H[node] + id + 3);
    }
    nw[H[node]] = max(nw[H[node]], sz[node]);
}

void Cal2(int node, int p, int s)
{
    for(auto x : Adj[node])
    {
        if(x == p or Mark[x])
        {
            continue;
        }
        Cal2(x, node, s);
    }
    if(Cur.empty() or Cur[0] < sz[node])
    {
        // do nothing
    }
    else
    {
        int id = upper_bound(Cur.begin(), Cur.end(), sz[node], greater <int> ()) - Cur.begin() - 1;
        assert(id >= 0);
        ans[2 * sz[node]] = max(ans[2 * sz[node]], H[node] + id + 3);
    }
    nw[H[node]] = max(nw[H[node]], sz[node]);
    ans[2 * min(s, sz[node])] = max(ans[2 * min(s, sz[node])], H[node] + 2);
}

void Solve(int node, int p)
{
    DFS(node, p);
    int cen = FindCen(node, p, sz[node]);
    Mark[cen] = true;
    Cur.clear();
    int ss = sz[node];
    for(auto x : Adj[cen])
    {
        H[x] = 0;
        if(Mark[x])
        {
            continue;
        }
        Cal(x, cen);
        for(int i = 0; ; i++)
        {
            if(nw[i] == 0)
            {
                break;
            }
            if(i < (int)Cur.size())
            {
                Cur[i] = max(Cur[i], nw[i]);
            }
            else
            {
                Cur.push_back(nw[i]);
            }
            nw[i] = 0;
        }
    }
    Cur.clear();
    reverse(Adj[cen].begin(), Adj[cen].end());
    for(auto x : Adj[cen])
    {
        H[x] = 0;
        if(Mark[x])
        {
            continue;
        }
        Cal2(x, cen, ss - sz[x]);
        for(int i = 0; ; i++)
        {
            if(nw[i] == 0)
            {
                break;
            }
            if(i < (int)Cur.size())
            {
                Cur[i] = max(Cur[i], nw[i]);
            }
            else
            {
                Cur.push_back(nw[i]);
            }
            nw[i] = 0;
        }
    }
    for(auto x : Adj[cen])
    {
        if(!Mark[x])
        {
            Solve(x, cen);
        }
    }
}

int main()
{
    // freopen("Input.txt", "r", stdin);
    // freopen("Output2.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        Adj[u].push_back(v);
        Adj[v].push_back(u);
    }

    Solve(1, -1);
    for(int i = n; i >= 1; i--)
    {
        ans[i] = max(ans[i], ans[i + 1]);
    }

    for(int i = 1; i <= n; i++)
    {
        if(i & 1)
        {
            cout << 1 << '\n';
        }
        else
        {
            cout << max(ans[i], 1) << '\n';
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
21 Correct 2 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
21 Correct 2 ms 4940 KB Output is correct
22 Correct 6 ms 5132 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Correct 5 ms 5196 KB Output is correct
25 Correct 5 ms 5160 KB Output is correct
26 Correct 6 ms 5196 KB Output is correct
27 Correct 5 ms 5196 KB Output is correct
28 Correct 6 ms 5196 KB Output is correct
29 Correct 6 ms 5196 KB Output is correct
30 Correct 6 ms 5196 KB Output is correct
31 Correct 7 ms 5196 KB Output is correct
32 Correct 9 ms 5324 KB Output is correct
33 Correct 8 ms 5432 KB Output is correct
34 Correct 6 ms 5196 KB Output is correct
35 Correct 5 ms 5196 KB Output is correct
36 Correct 6 ms 5196 KB Output is correct
37 Correct 4 ms 5196 KB Output is correct
38 Correct 6 ms 5256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 2 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 2 ms 4940 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 2 ms 4940 KB Output is correct
21 Correct 2 ms 4940 KB Output is correct
22 Correct 6 ms 5132 KB Output is correct
23 Correct 5 ms 5196 KB Output is correct
24 Correct 5 ms 5196 KB Output is correct
25 Correct 5 ms 5160 KB Output is correct
26 Correct 6 ms 5196 KB Output is correct
27 Correct 5 ms 5196 KB Output is correct
28 Correct 6 ms 5196 KB Output is correct
29 Correct 6 ms 5196 KB Output is correct
30 Correct 6 ms 5196 KB Output is correct
31 Correct 7 ms 5196 KB Output is correct
32 Correct 9 ms 5324 KB Output is correct
33 Correct 8 ms 5432 KB Output is correct
34 Correct 6 ms 5196 KB Output is correct
35 Correct 5 ms 5196 KB Output is correct
36 Correct 6 ms 5196 KB Output is correct
37 Correct 4 ms 5196 KB Output is correct
38 Correct 6 ms 5256 KB Output is correct
39 Correct 526 ms 14408 KB Output is correct
40 Correct 452 ms 16616 KB Output is correct
41 Correct 502 ms 16828 KB Output is correct
42 Correct 438 ms 17056 KB Output is correct
43 Correct 448 ms 16848 KB Output is correct
44 Correct 416 ms 16936 KB Output is correct
45 Correct 821 ms 22340 KB Output is correct
46 Correct 658 ms 26772 KB Output is correct
47 Correct 159 ms 17248 KB Output is correct
48 Correct 118 ms 17400 KB Output is correct
49 Correct 410 ms 17552 KB Output is correct
50 Correct 147 ms 17432 KB Output is correct
51 Correct 339 ms 25408 KB Output is correct