답안 #1009634

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1009634 2024-06-27T18:05:01 Z phong Meetings 2 (JOI21_meetings2) C++17
100 / 100
189 ms 39108 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;
//        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:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |                 while(pos < idx.size() && sz[idx[pos]] >= i / 2){
      |                       ~~~~^~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  124 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7768 KB Output is correct
2 Correct 1 ms 7768 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 1 ms 7768 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 1 ms 7772 KB Output is correct
13 Correct 1 ms 7772 KB Output is correct
14 Correct 1 ms 7772 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 1 ms 7772 KB Output is correct
17 Correct 2 ms 7772 KB Output is correct
18 Correct 1 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 7768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7768 KB Output is correct
2 Correct 1 ms 7768 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 1 ms 7768 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 1 ms 7772 KB Output is correct
13 Correct 1 ms 7772 KB Output is correct
14 Correct 1 ms 7772 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 1 ms 7772 KB Output is correct
17 Correct 2 ms 7772 KB Output is correct
18 Correct 1 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 7768 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 3 ms 10076 KB Output is correct
24 Correct 3 ms 8264 KB Output is correct
25 Correct 3 ms 10232 KB Output is correct
26 Correct 3 ms 10228 KB Output is correct
27 Correct 3 ms 10076 KB Output is correct
28 Correct 3 ms 10076 KB Output is correct
29 Correct 3 ms 10200 KB Output is correct
30 Correct 3 ms 10076 KB Output is correct
31 Correct 3 ms 10240 KB Output is correct
32 Correct 4 ms 8284 KB Output is correct
33 Correct 4 ms 10332 KB Output is correct
34 Correct 3 ms 10076 KB Output is correct
35 Correct 3 ms 8284 KB Output is correct
36 Correct 3 ms 10076 KB Output is correct
37 Correct 3 ms 10076 KB Output is correct
38 Correct 3 ms 10304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 7768 KB Output is correct
2 Correct 1 ms 7768 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 1 ms 7768 KB Output is correct
11 Correct 1 ms 7772 KB Output is correct
12 Correct 1 ms 7772 KB Output is correct
13 Correct 1 ms 7772 KB Output is correct
14 Correct 1 ms 7772 KB Output is correct
15 Correct 1 ms 7772 KB Output is correct
16 Correct 1 ms 7772 KB Output is correct
17 Correct 2 ms 7772 KB Output is correct
18 Correct 1 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 7768 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 3 ms 10076 KB Output is correct
24 Correct 3 ms 8264 KB Output is correct
25 Correct 3 ms 10232 KB Output is correct
26 Correct 3 ms 10228 KB Output is correct
27 Correct 3 ms 10076 KB Output is correct
28 Correct 3 ms 10076 KB Output is correct
29 Correct 3 ms 10200 KB Output is correct
30 Correct 3 ms 10076 KB Output is correct
31 Correct 3 ms 10240 KB Output is correct
32 Correct 4 ms 8284 KB Output is correct
33 Correct 4 ms 10332 KB Output is correct
34 Correct 3 ms 10076 KB Output is correct
35 Correct 3 ms 8284 KB Output is correct
36 Correct 3 ms 10076 KB Output is correct
37 Correct 3 ms 10076 KB Output is correct
38 Correct 3 ms 10304 KB Output is correct
39 Correct 107 ms 29912 KB Output is correct
40 Correct 111 ms 29648 KB Output is correct
41 Correct 110 ms 29896 KB Output is correct
42 Correct 119 ms 29988 KB Output is correct
43 Correct 113 ms 30156 KB Output is correct
44 Correct 107 ms 30152 KB Output is correct
45 Correct 189 ms 35012 KB Output is correct
46 Correct 156 ms 39108 KB Output is correct
47 Correct 87 ms 30296 KB Output is correct
48 Correct 65 ms 30916 KB Output is correct
49 Correct 125 ms 30668 KB Output is correct
50 Correct 73 ms 30536 KB Output is correct
51 Correct 124 ms 37956 KB Output is correct