답안 #1009632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1009632 2024-06-27T18:03:26 Z phong Meetings 2 (JOI21_meetings2) C++17
100 / 100
174 ms 41628 KB
#include<bits/stdc++.h>

#define ll long long
const int nmax = 2e5 + 5, N = 3e5 + 5;
const ll oo = 1e18;
const int lg = 18, M = 4e3;
const int base = 2e5, mod = 998244353;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;

int n, a[nmax];
vector<int> adj[nmax];
namespace sub1{
    int sz[nmax], root, cntw;
    int h[nmax];
    void dfs_1(int u, int p){
        sz[u] = 1;
        for(auto v : adj[u]){
            if(v == p) continue;
            dfs_1(v, u);
            sz[u] += sz[v];
        }
    }
    void dfs_2(int u, int p){
        for(auto v : adj[u]){
            if(v == p) continue;
            if(sz[v] > cntw/2) return dfs_2(v, u);
        }
        root = u;
    }
    int ans[nmax], up[nmax][lg + 1];
    void dfs(int u, int p){
        sz[u] = 1;
        for(auto v : adj[u]){
            if(v == p) continue;
            h[v] = h[u] + 1;
            up[v][0] = u;
            for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
            dfs(v, u);
            sz[u] += sz[v];
        }
    }
    int lca(int u, int v){
        if(h[u] != h[v]){
            if(h[u] < h[v]) swap(u, v);
            int k = h[u] - h[v];
            for(int j = 0; j <= lg; ++j){
                if(k >> j & 1){
                    u = up[u][j];
                }
            }
        }
        if(u == v) return u;
        for(int j = __lg(h[u]); j >= 0; --j){
            if(up[u][j] != up[v][j]){
                u = up[u][j];
                v = up[v][j];
            }
        }
        return up[u][0];
    }
    int dist(int u, int v){
        int x = lca(u, v);
        return h[u] + h[v] - 2* h[x];
    }

    void sol(){
        dfs_1(1, -1), cntw  =sz[1];
        dfs_2(1, -1);
        dfs(root, -1);
        vector<int> idx;
        for(int i = 1; i <= n; ++i){
            if(i != root){
                idx.push_back(i);
            }
        }
        sort(idx.begin(), idx.end(), [](int a, int b){
             return sz[a] > sz[b];
             });
        int pos = 0;
        int u = -1, v = -1;
        for(int i = 1; i <= n; ++i){
//            if(i == root) continue;
            ans[sz[i]] = max(ans[sz[i]], h[i]);
        }
        for(int i = n; i >= 1; --i) ans[i] = max(ans[i], ans[i + 1]);
//        cout << root << '*';
        for(int i = n; i >= 1; --i){
            if(i & 1);
            else{
                while(pos < idx.size() && sz[idx[pos]] >= i / 2){
                    int x = idx[pos];
                    if(u == -1) u = v = x;
                    else{
                        a[1] = x, a[2] = u, a[3] = v;
                        int ma = 0;
                        for(int i = 1; i <= 3; ++i){
                            for(int j = i; j <= 3; ++j){
                                int cur =dist(a[i], a[j]);
                                if(cur > ma){
                                    u = a[i];
                                    v = a[j];
                                    ma = cur;
                                }
                            }
                        }
                    }
                    ++pos;
                }
                if(u != -1){
//                    cout << i << ' ' << u << ' ' << v << endl;
                    ans[i / 2] = max(ans[i / 2], dist(u, v));
//                    cout << i << ' ' << u << ' ' << v << endl;
                }
            }
        }
        for(int i = 1; i <= n; ++i){
            if(i & 1) cout << 1;
            else cout << ans[i / 2] + 1;
            cout << endl;
        }
    }
}

main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//        freopen("code.inp", "r", stdin);
//        freopen("code.out", "w", stdout);
    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);
    }
    sub1::sol();

}
/*
5
5 4
2 5
1 5
3 5



*/

Compilation message

meetings2.cpp: In function 'void sub1::sol()':
meetings2.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |                 while(pos < idx.size() && sz[idx[pos]] >= i / 2){
      |                       ~~~~^~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:129:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  129 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7772 KB Output is correct
2 Correct 1 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 1 ms 7772 KB Output is correct
5 Correct 1 ms 7772 KB Output is correct
6 Correct 1 ms 7772 KB Output is correct
7 Correct 1 ms 7772 KB Output is correct
8 Correct 1 ms 7772 KB Output is correct
9 Correct 1 ms 7772 KB Output is correct
10 Correct 2 ms 7772 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 1 ms 7768 KB Output is correct
14 Correct 1 ms 7768 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 2 ms 7840 KB Output is correct
17 Correct 2 ms 8028 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 1 ms 7768 KB Output is correct
21 Correct 1 ms 7836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7772 KB Output is correct
2 Correct 1 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 1 ms 7772 KB Output is correct
5 Correct 1 ms 7772 KB Output is correct
6 Correct 1 ms 7772 KB Output is correct
7 Correct 1 ms 7772 KB Output is correct
8 Correct 1 ms 7772 KB Output is correct
9 Correct 1 ms 7772 KB Output is correct
10 Correct 2 ms 7772 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 1 ms 7768 KB Output is correct
14 Correct 1 ms 7768 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 2 ms 7840 KB Output is correct
17 Correct 2 ms 8028 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 1 ms 7768 KB Output is correct
21 Correct 1 ms 7836 KB Output is correct
22 Correct 4 ms 8284 KB Output is correct
23 Correct 3 ms 10076 KB Output is correct
24 Correct 4 ms 8284 KB Output is correct
25 Correct 3 ms 10076 KB Output is correct
26 Correct 3 ms 10076 KB Output is correct
27 Correct 3 ms 10236 KB Output is correct
28 Correct 4 ms 10076 KB Output is correct
29 Correct 3 ms 10076 KB Output is correct
30 Correct 4 ms 10076 KB Output is correct
31 Correct 3 ms 10072 KB Output is correct
32 Correct 4 ms 8284 KB Output is correct
33 Correct 6 ms 10588 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 3 ms 8204 KB Output is correct
36 Correct 4 ms 10072 KB Output is correct
37 Correct 3 ms 10164 KB Output is correct
38 Correct 3 ms 10152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7772 KB Output is correct
2 Correct 1 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 1 ms 7772 KB Output is correct
5 Correct 1 ms 7772 KB Output is correct
6 Correct 1 ms 7772 KB Output is correct
7 Correct 1 ms 7772 KB Output is correct
8 Correct 1 ms 7772 KB Output is correct
9 Correct 1 ms 7772 KB Output is correct
10 Correct 2 ms 7772 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 1 ms 7768 KB Output is correct
14 Correct 1 ms 7768 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 2 ms 7840 KB Output is correct
17 Correct 2 ms 8028 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 1 ms 7768 KB Output is correct
21 Correct 1 ms 7836 KB Output is correct
22 Correct 4 ms 8284 KB Output is correct
23 Correct 3 ms 10076 KB Output is correct
24 Correct 4 ms 8284 KB Output is correct
25 Correct 3 ms 10076 KB Output is correct
26 Correct 3 ms 10076 KB Output is correct
27 Correct 3 ms 10236 KB Output is correct
28 Correct 4 ms 10076 KB Output is correct
29 Correct 3 ms 10076 KB Output is correct
30 Correct 4 ms 10076 KB Output is correct
31 Correct 3 ms 10072 KB Output is correct
32 Correct 4 ms 8284 KB Output is correct
33 Correct 6 ms 10588 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 3 ms 8204 KB Output is correct
36 Correct 4 ms 10072 KB Output is correct
37 Correct 3 ms 10164 KB Output is correct
38 Correct 3 ms 10152 KB Output is correct
39 Correct 107 ms 32456 KB Output is correct
40 Correct 95 ms 32200 KB Output is correct
41 Correct 131 ms 32456 KB Output is correct
42 Correct 114 ms 32468 KB Output is correct
43 Correct 124 ms 32596 KB Output is correct
44 Correct 112 ms 32548 KB Output is correct
45 Correct 174 ms 37544 KB Output is correct
46 Correct 163 ms 41628 KB Output is correct
47 Correct 85 ms 32892 KB Output is correct
48 Correct 69 ms 33224 KB Output is correct
49 Correct 127 ms 33272 KB Output is correct
50 Correct 84 ms 33096 KB Output is correct
51 Correct 116 ms 40516 KB Output is correct