Submission #752148

# Submission time Handle Problem Language Result Execution time Memory
752148 2023-06-02T11:14:49 Z Sam_a17 Meetings 2 (JOI21_meetings2) C++17
100 / 100
2558 ms 44028 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define blt(x) __builtin_popcount(x)
#define uniq(x) x.resize(unique(all(x)) - x.begin());
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  } else {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
  }
}
 
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10, LOG = 21;
vector<int> adj[N];
bool used[N];
int n, sz[N], most;
int compSize;

struct segTreeMax {
  vector<long long> mTree;
  int size;
 
  void init(long long n) {
    size = 1;
    while(size < n)  {
      size *= 2;
    }
    mTree.assign(2 * size - 1, -1);
  }
 
  void upd(int u, long long v, int x, int lx, int rx) { // set value at pos u
    if(rx - lx == 1) {
      if(v == -1) {
        mTree[x] = -1;
      } else {
        mTree[x] = max(mTree[x], v);
      }
      return;
    }
 
    int m = (lx + rx) / 2;
    if(u < m) {
      upd(u, v, 2 * x + 1, lx, m);
    }else {
      upd(u, v, 2 * x + 2, m, rx);
    }
    mTree[x] = max(mTree[2 * x + 1], mTree[2 * x + 2]);
  }
 
  void upd(int u, long long v) {
    upd(u, v, 0, 0, size);
  }
 
  long long qry (int l, int r, int x, int lx, int rx) { // range queries
    if(l >= rx || lx >= r) {
      return -1;
    }
    if(lx >= l && r >= rx) {
      return mTree[x];
    }
 
    int m = (rx + lx) / 2;
    long long s1 = qry(l, r, 2 * x + 1, lx, m);
    long long s2 = qry(l, r, 2 * x + 2, m, rx);
    return max(s1, s2);
  }
 
  long long qry(int l, int r) {
    return qry(l, r, 0, 0, size);
  }
};

segTreeMax seg_max;

void dfs_sz(int node, int parent) {
  sz[node] = 1, compSize++;
  for(auto i: adj[node]) {
    if(i == parent || used[i]) continue;
    dfs_sz(i, node);
    sz[node] += sz[i];
  }
}

int get_centroid(int node, int parent) {
  for(auto i: adj[node]) {
    if(i == parent || used[i]) continue;
    if(2 * sz[i] > compSize) {
      return get_centroid(i, node);
    }
  }
  return node;
}

int centroid(int u) {
  compSize = 0;
  dfs_sz(u, 0);

  int p = get_centroid(u, 0);
  if(!most) most = p;
  dfs_sz(p, 0);
  return p;
}


bool use[N];
vector<pair<int, int>> curr;
vector<int> full;
int ans[N], bb;

void solve(int node, int parent, int depth, int lim) {
  if(depth) {
    if(!use[sz[node]]) {
      full.push_back(sz[node]);
      use[sz[node]] = true;
    }

    curr.push_back({sz[node], depth});

    ans[2 * min(sz[node], lim)] = max(ans[2 * min(sz[node], lim)], depth);
  }

  for(auto i: adj[node]) {
    if(i == parent || used[i]) continue;
    if(!depth) {
      solve(i, node, depth + 1, n - sz[i]);
    } else {
      solve(i, node, depth + 1, lim);
    }

    if(parent) continue;

    for(auto j: curr) {
      int max_dep = seg_max.qry(j.first, n + 1);

      if(max_dep != -1) {
        ans[j.first * 2] = max(ans[j.first * 2], max_dep + j.second); 
      }
    }

    for(auto j: curr) {
      seg_max.upd(j.first, j.second);
    }

    curr.clear();
  }
}

void centroid_decomposition() {
  queue<int> q;
  q.push(1);

  while(!q.empty()) {
    auto u = q.front();
    q.pop();

    int get = centroid(u);
    solve(get, 0, 0, 0);

    for(auto i: full) {
      use[i] = false;
      seg_max.upd(i, -1);
    }
    full.clear();

    reverse(all(adj[get]));

    solve(get, 0, 0, 0);
    
    for(auto i: full) {
      use[i] = false;
      seg_max.upd(i, -1);
    }
    full.clear();

    used[get] = true;
    for(auto i: adj[get]) {
      if(used[i] || sz[i] == 1) continue;
      q.push(i);
    }
  }
}

int up[N][LOG];
int depths[N];

void dfs(int node, int parent, int depth) {
  for(auto i: adj[node]) {
    if(i == parent) continue;
    up[i][0] = node;
    for(int j = 1; j < LOG; j++) {
      up[i][j] = up[up[i][j - 1]][j - 1];
    } depths[i] = depths[node] + 1;
    dfs(i, node, depth + 1);
  }
}

int lca(int a, int b) {
  if(a == b) {
    return a;
  }

  if(depths[a] > depths[b]) {
    swap(a, b);
  }

  int delta = depths[b] - depths[a];
  for(int i = 0; i < LOG; i++) {
    if(delta & (1 << i)) {
      b = up[b][i];
    }
  }

  if(a == b) {
    return a;
  }

  for(int i = LOG - 1; i >= 0; i--) {
    if(up[a][i] != up[b][i]) {
      a = up[a][i], b = up[b][i];
    }
  }
  return up[a][0];
}

void solve_() {
  cin >> n;

  seg_max.init(n + 5);
  for(int i = 1; i <= n - 1; i++) {
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  centroid_decomposition();

  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 << ans[i] + 1 << '\n';
    }
  }
}


int main() {
  setIO();
  
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1; 
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
}

Compilation message

meetings2.cpp: In function 'void setIO(std::string)':
meetings2.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
meetings2.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5040 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5032 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 3 ms 4936 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5040 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5040 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5032 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 3 ms 4936 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5040 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 13 ms 5344 KB Output is correct
23 Correct 14 ms 5344 KB Output is correct
24 Correct 14 ms 5332 KB Output is correct
25 Correct 14 ms 5352 KB Output is correct
26 Correct 13 ms 5352 KB Output is correct
27 Correct 15 ms 5332 KB Output is correct
28 Correct 13 ms 5352 KB Output is correct
29 Correct 13 ms 5332 KB Output is correct
30 Correct 14 ms 5332 KB Output is correct
31 Correct 15 ms 5348 KB Output is correct
32 Correct 21 ms 5608 KB Output is correct
33 Correct 25 ms 5692 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5308 KB Output is correct
36 Correct 14 ms 5308 KB Output is correct
37 Correct 7 ms 5332 KB Output is correct
38 Correct 13 ms 5576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 5040 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5032 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 3 ms 4936 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5040 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 13 ms 5344 KB Output is correct
23 Correct 14 ms 5344 KB Output is correct
24 Correct 14 ms 5332 KB Output is correct
25 Correct 14 ms 5352 KB Output is correct
26 Correct 13 ms 5352 KB Output is correct
27 Correct 15 ms 5332 KB Output is correct
28 Correct 13 ms 5352 KB Output is correct
29 Correct 13 ms 5332 KB Output is correct
30 Correct 14 ms 5332 KB Output is correct
31 Correct 15 ms 5348 KB Output is correct
32 Correct 21 ms 5608 KB Output is correct
33 Correct 25 ms 5692 KB Output is correct
34 Correct 7 ms 5332 KB Output is correct
35 Correct 6 ms 5308 KB Output is correct
36 Correct 14 ms 5308 KB Output is correct
37 Correct 7 ms 5332 KB Output is correct
38 Correct 13 ms 5576 KB Output is correct
39 Correct 1408 ms 21016 KB Output is correct
40 Correct 1397 ms 20804 KB Output is correct
41 Correct 1438 ms 20996 KB Output is correct
42 Correct 1462 ms 21124 KB Output is correct
43 Correct 1425 ms 21164 KB Output is correct
44 Correct 1468 ms 21072 KB Output is correct
45 Correct 2558 ms 32992 KB Output is correct
46 Correct 2529 ms 44028 KB Output is correct
47 Correct 406 ms 20788 KB Output is correct
48 Correct 209 ms 20580 KB Output is correct
49 Correct 1579 ms 22040 KB Output is correct
50 Correct 427 ms 21892 KB Output is correct
51 Correct 1293 ms 33276 KB Output is correct