답안 #1009615

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1009615 2024-06-27T17:03:12 Z phong Meetings 2 (JOI21_meetings2) C++17
0 / 100
1 ms 7772 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] + 1);
        }
        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){
                    ans[i / 2] = max(ans[i / 2], dist(u, v) + 1);
//                    cout << i << ' ' << u << ' ' << v << endl;
                }
            }
        }
        for(int i = 1; i <= n; ++i){
            if(i & 1) cout << 1;
            else cout << ans[i / 2];
            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();

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




*/

Compilation message

meetings2.cpp: In function 'void sub1::sol()':
meetings2.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |                 while(pos < idx.size() && sz[idx[pos]] >= i / 2){
      |                       ~~~~^~~~~~~~~~~~
meetings2.cpp: At global scope:
meetings2.cpp:126:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  126 | 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 Incorrect 1 ms 7772 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 7772 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 1 ms 7772 KB Output isn't correct
5 Halted 0 ms 0 KB -