답안 #955219

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955219 2024-03-29T17:45:18 Z abczz Meetings 2 (JOI21_meetings2) C++14
100 / 100
1467 ms 79596 KB
#include <iostream>
#include <vector>
#include <queue>
#include <array>
#include <map>
#define ll long long
 
using namespace std;
 
vector <ll> adj[200000];
bool C[200000];
vector <array<ll, 2>> U;
vector <ll> V[200000];
vector <vector<ll>> D;
bool ok;
ll n, k, a, b, f = 1e18, g, sz[200000], F[200000];
 
struct SegTree{
  vector <ll> st{vector <ll> (800000, -1e18)};
  vector <ll> updated;
  void update(ll id, ll l, ll r, ll q, ll w) {
    updated.push_back(id);
    if (l == r) {
      st[id] = max(st[id], w);
      return;
    }
    ll mid = (l+r)/2;
    if (q <= mid) update(id*2, l, mid, q, w);
    else update(id*2+1, mid+1, r, q, w);
    st[id] = max(st[id*2], st[id*2+1]);
  }
  ll query(ll id, ll l, ll r, ll q) {
    if (r < q) return -1e18;
    if (q <= l) return st[id];
    ll mid = (l+r)/2;
    return max(query(id*2, l, mid, q), query(id*2+1, mid+1, r, q));
  }
  void rollback() {
    while (!updated.empty()) {
      auto id = updated.back();
      updated.pop_back();
      st[id] = -1e18;
    }
  }
}ST;
 
void dfs_sz(ll u, ll p) {
  sz[u] = 1;
  for (auto v : adj[u]) {
    if (v != p && !C[v]) {
      dfs_sz(v, u);
      sz[u] += sz[v];
    }
  }
  if (ok) g = max(g, min(sz[u], n-sz[u]));
}
 
ll find_cent(ll u, ll p, ll tot) {
  ll mx = -1, ret = -1;
  for (auto v : adj[u]) {
    if (v != p && !C[v]) {
      ret = max(ret, find_cent(v, u, tot));
      mx = max(mx, sz[v]);
    }
  }
  mx = max(mx, tot-sz[u]);
  if (mx <= tot/2) return u;
  else return ret;
}
 
void dfs_resz(ll u, ll p, ll w, ll x) {
  if (D[x].size() == w) D[x].push_back(0);
  sz[u] = 1;
  for (auto v : adj[u]) {
    if (v != p && !C[v]) {
      dfs_resz(v, u, w+1, x);
      sz[u] += sz[v];
    }
  }
  D[x][w] = max(D[x][w], sz[u]);
}
 
void solve(ll u) {
  dfs_sz(u, -1);
  ok = 0;
  ll tot = sz[u];
  ll cent = find_cent(u, -1, tot);
  C[cent] = 1;
  map <ll, ll> mp;
  D.clear();
  ll x = 0;
  for (auto v : adj[cent]) {
    if (!C[v]) {
      D.push_back({});
      dfs_resz(v, cent, 0, x);
      for (int i=0; i<D[x].size(); ++i) {
        F[D[x][i]-1] = max(F[D[x][i]-1], ST.query(1, 0, n-1, D[x][i]-1) + i + 3);
        F[min(D[x][i], tot-sz[v])-1] = max(F[min(D[x][i], tot-sz[v])-1], (ll)i + 2);
      }
      for (int i=0; i<D[x].size(); ++i) {
        ST.update(1, 0, n-1, D[x][i]-1, i);
      }
      ++x;
    }
  }
  ST.rollback();
  while (x--) {
    for (int i=0; i<D[x].size(); ++i) {
      F[D[x][i]-1] = max(F[D[x][i]-1], ST.query(1, 0, n-1, D[x][i]-1) + i + 3);
      //cout << cent+1 << " " << ST.query(1, 0, n-1, D[x][i]-1) << " " << i << '\n';
    }
    for (int i=0; i<D[x].size(); ++i) {
      ST.update(1, 0, n-1, D[x][i]-1, i);
    }
  }
  ST.rollback();
  for (auto v : adj[cent]) {
    if (!C[v]) solve(v);
  }
}
 
int main() {
  cin >> n;
  for (int i=0; i<n-1; ++i) {
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ok = 1;
  solve(0);
  for (int i=n-2; i>=0; --i) {
    F[i] = max(F[i], F[i+1]);
    if (i <= g-1) F[i] = max(2LL, F[i]);
  }
  for (int i=0; i<n; ++i) {
    if (!(i & 1)) cout << "1\n";
    else cout << max(1LL, F[i/2]) << '\n';
  }
}

Compilation message

meetings2.cpp: In function 'void dfs_resz(long long int, long long int, long long int, long long int)':
meetings2.cpp:72:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   72 |   if (D[x].size() == w) D[x].push_back(0);
      |       ~~~~~~~~~~~~^~~~
meetings2.cpp: In function 'void solve(long long int)':
meetings2.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |       for (int i=0; i<D[x].size(); ++i) {
      |                     ~^~~~~~~~~~~~
meetings2.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |       for (int i=0; i<D[x].size(); ++i) {
      |                     ~^~~~~~~~~~~~
meetings2.cpp:108:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for (int i=0; i<D[x].size(); ++i) {
      |                   ~^~~~~~~~~~~~
meetings2.cpp:112:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for (int i=0; i<D[x].size(); ++i) {
      |                   ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 5 ms 19120 KB Output is correct
6 Correct 4 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19268 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19036 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 5 ms 19036 KB Output is correct
16 Correct 4 ms 19036 KB Output is correct
17 Correct 4 ms 19036 KB Output is correct
18 Correct 4 ms 19036 KB Output is correct
19 Correct 4 ms 19036 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 5 ms 19120 KB Output is correct
6 Correct 4 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19268 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19036 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 5 ms 19036 KB Output is correct
16 Correct 4 ms 19036 KB Output is correct
17 Correct 4 ms 19036 KB Output is correct
18 Correct 4 ms 19036 KB Output is correct
19 Correct 4 ms 19036 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19268 KB Output is correct
22 Correct 10 ms 19292 KB Output is correct
23 Correct 10 ms 19300 KB Output is correct
24 Correct 11 ms 19492 KB Output is correct
25 Correct 9 ms 19520 KB Output is correct
26 Correct 9 ms 19544 KB Output is correct
27 Correct 10 ms 19424 KB Output is correct
28 Correct 9 ms 19528 KB Output is correct
29 Correct 10 ms 19292 KB Output is correct
30 Correct 9 ms 19292 KB Output is correct
31 Correct 9 ms 19372 KB Output is correct
32 Correct 14 ms 19928 KB Output is correct
33 Correct 17 ms 20184 KB Output is correct
34 Correct 8 ms 19928 KB Output is correct
35 Correct 7 ms 19928 KB Output is correct
36 Correct 13 ms 19548 KB Output is correct
37 Correct 7 ms 19804 KB Output is correct
38 Correct 12 ms 20184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 5 ms 19120 KB Output is correct
6 Correct 4 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19268 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19036 KB Output is correct
14 Correct 5 ms 19036 KB Output is correct
15 Correct 5 ms 19036 KB Output is correct
16 Correct 4 ms 19036 KB Output is correct
17 Correct 4 ms 19036 KB Output is correct
18 Correct 4 ms 19036 KB Output is correct
19 Correct 4 ms 19036 KB Output is correct
20 Correct 4 ms 19036 KB Output is correct
21 Correct 4 ms 19268 KB Output is correct
22 Correct 10 ms 19292 KB Output is correct
23 Correct 10 ms 19300 KB Output is correct
24 Correct 11 ms 19492 KB Output is correct
25 Correct 9 ms 19520 KB Output is correct
26 Correct 9 ms 19544 KB Output is correct
27 Correct 10 ms 19424 KB Output is correct
28 Correct 9 ms 19528 KB Output is correct
29 Correct 10 ms 19292 KB Output is correct
30 Correct 9 ms 19292 KB Output is correct
31 Correct 9 ms 19372 KB Output is correct
32 Correct 14 ms 19928 KB Output is correct
33 Correct 17 ms 20184 KB Output is correct
34 Correct 8 ms 19928 KB Output is correct
35 Correct 7 ms 19928 KB Output is correct
36 Correct 13 ms 19548 KB Output is correct
37 Correct 7 ms 19804 KB Output is correct
38 Correct 12 ms 20184 KB Output is correct
39 Correct 410 ms 29520 KB Output is correct
40 Correct 402 ms 29280 KB Output is correct
41 Correct 399 ms 29472 KB Output is correct
42 Correct 388 ms 29700 KB Output is correct
43 Correct 493 ms 29780 KB Output is correct
44 Correct 393 ms 29780 KB Output is correct
45 Correct 1060 ms 53824 KB Output is correct
46 Correct 1467 ms 75852 KB Output is correct
47 Correct 310 ms 70576 KB Output is correct
48 Correct 224 ms 74588 KB Output is correct
49 Correct 551 ms 30408 KB Output is correct
50 Correct 217 ms 50856 KB Output is correct
51 Correct 883 ms 79596 KB Output is correct