답안 #792128

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792128 2023-07-24T15:36:49 Z cig32 Cat Exercise (JOI23_ho_t4) C++17
74 / 100
2000 ms 479268 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
set<int>adj[MAXN];
set<int>nums[MAXN];
int dsu[MAXN];
int n, p[MAXN];
int pos[MAXN];
bool vis[MAXN];
int nxtset;
vector<int> euler;
int l[MAXN], r[MAXN], dep[MAXN];
void dfs0(int node, int prv) {
  euler.push_back(node);
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  for(int x: adj[node]) {
    if(x != prv) {
      dfs0(x, node);
      euler.push_back(node);
    }
  }
}
int st[19][2*MAXN];
int lcadep(int x, int y) {
  int m1 = min(l[x], l[y]);
  int m2 = max(r[x], r[y]);
  int k = 32 - __builtin_clz(m2-m1+1) - 1;
  return min(st[k][m1], st[k][m2-(1<<k)+1]);
}
int dist(int x, int y) {
  return dep[x] + dep[y] - 2 * lcadep(x, y);
}
int recur(int node) {
  if(adj[node].empty()) return 0;
  int cat = dsu[node];
  int boss = *nums[cat].rbegin();
  nums[cat].erase(boss);
  node = pos[boss];
  vector<int> visited; // Remember to turn all of them to not visited at the end
  vis[node] = 1;
  visited.push_back(node);
  vector<int> start;
  start.push_back(0);
  for(int x: adj[node]) start.push_back(x);
  int len = start.size() - 1;
  int nxt[len+1],  prv[len+1];
  queue<int> q[len+1];
  for(int i=1; i<=len; i++) {
    nxt[i] = i+1;
    prv[i] = i-1;
    q[i].push(start[i]);
  }
  nxt[0] = 1;
  vector<int> bruh[len+1]; // respective lists
  vector<int>ranks; // rank according ascending of size
  while(1) {
    int id=0;
    while(nxt[id]!=len+1){
      if(ranks.size() + 1 == len) break;
      id=nxt[id];
      int f = q[id].front();
      q[id].pop();
      vis[f]=1;
      bruh[id].push_back(f);
      visited.push_back(f);
      for(int x:adj[f]) {
        if(!vis[x]) {
          q[id].push(x);
        }
      }
      if(q[id].empty()) {
        for(int x:bruh[id]){
          dsu[x]=nxtset;
          nums[cat].erase(p[x]);
          nums[nxtset].insert(p[x]);
        }
        ranks.push_back(id);
        nxtset++;
        nxt[prv[id]]=nxt[id];
        prv[nxt[id]]=prv[id];
      }
    }
    if(ranks.size() + 1 == len) break;
  }
  for(int x:visited)vis[x]=0;
  vector<pair<int,int>>vt;
  for(int i=1; i<=len; i++) {
    vt.push_back({*nums[dsu[start[i]]].rbegin(),pos[*nums[dsu[start[i]]].rbegin()]});
  }
  for(int x: adj[node]) {
    adj[x].erase(node);
  }
  int res = 0;
  for(pair<int, int> x: vt) {
    res = max(res, dist(node, x.second) + recur(x.second));
  }
  return res;
}
void solve(int tc) {
  cin >> n;
  for(int i=1; i<=n; i++) cin >> p[i];
  for(int i=1; i<=n; i++) pos[p[i]] = i;
  for(int i=1; i<n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].insert(b);
    adj[b].insert(a);
  }
  dfs0(1, -1);
  for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
  for(int i=euler.size()-1; i>=0; i--) l[euler[i]] = i;
  for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
  for(int i=1; i<19; i++) {
    for(int j=0; j+(1<<i)-1<euler.size(); j++) {
      st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
    }
  }
  for(int i=1; i<=n; i++) dsu[i]=1;
  for(int i=1; i<=n; i++) nums[1].insert(i);
  nxtset=2;
  cout<<recur(1)<<'\n';

}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷

Compilation message

Main.cpp: In function 'long long int recur(long long int)':
Main.cpp:78:27: 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]
   78 |       if(ranks.size() + 1 == len) break;
      |          ~~~~~~~~~~~~~~~~~^~~~~~
Main.cpp:102:25: 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]
  102 |     if(ranks.size() + 1 == len) break;
      |        ~~~~~~~~~~~~~~~~~^~~~~~
Main.cpp: In function 'void solve(long long int)':
Main.cpp:129:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |   for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
      |                ~^~~~~~~~~~~~~
Main.cpp:131:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
      |                ~^~~~~~~~~~~~~
Main.cpp:133:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     for(int j=0; j+(1<<i)-1<euler.size(); j++) {
      |                  ~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
11 Correct 8 ms 19796 KB Output is correct
12 Correct 11 ms 19768 KB Output is correct
13 Correct 9 ms 19824 KB Output is correct
14 Correct 9 ms 19376 KB Output is correct
15 Correct 9 ms 19412 KB Output is correct
16 Correct 9 ms 19384 KB Output is correct
17 Correct 10 ms 19380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
11 Correct 8 ms 19796 KB Output is correct
12 Correct 11 ms 19768 KB Output is correct
13 Correct 9 ms 19824 KB Output is correct
14 Correct 9 ms 19376 KB Output is correct
15 Correct 9 ms 19412 KB Output is correct
16 Correct 9 ms 19384 KB Output is correct
17 Correct 10 ms 19380 KB Output is correct
18 Correct 16 ms 30416 KB Output is correct
19 Correct 16 ms 29892 KB Output is correct
20 Correct 16 ms 29840 KB Output is correct
21 Correct 17 ms 21780 KB Output is correct
22 Correct 17 ms 21852 KB Output is correct
23 Correct 17 ms 21916 KB Output is correct
24 Correct 20 ms 21840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
11 Correct 8 ms 19796 KB Output is correct
12 Correct 11 ms 19768 KB Output is correct
13 Correct 9 ms 19824 KB Output is correct
14 Correct 9 ms 19376 KB Output is correct
15 Correct 9 ms 19412 KB Output is correct
16 Correct 9 ms 19384 KB Output is correct
17 Correct 10 ms 19380 KB Output is correct
18 Correct 16 ms 30416 KB Output is correct
19 Correct 16 ms 29892 KB Output is correct
20 Correct 16 ms 29840 KB Output is correct
21 Correct 17 ms 21780 KB Output is correct
22 Correct 17 ms 21852 KB Output is correct
23 Correct 17 ms 21916 KB Output is correct
24 Correct 20 ms 21840 KB Output is correct
25 Correct 11 ms 19216 KB Output is correct
26 Correct 18 ms 28228 KB Output is correct
27 Correct 18 ms 28276 KB Output is correct
28 Correct 19 ms 28356 KB Output is correct
29 Correct 19 ms 28320 KB Output is correct
30 Correct 21 ms 21964 KB Output is correct
31 Correct 24 ms 22552 KB Output is correct
32 Correct 21 ms 22172 KB Output is correct
33 Correct 21 ms 22356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
11 Correct 8 ms 19796 KB Output is correct
12 Correct 11 ms 19768 KB Output is correct
13 Correct 9 ms 19824 KB Output is correct
14 Correct 9 ms 19376 KB Output is correct
15 Correct 9 ms 19412 KB Output is correct
16 Correct 9 ms 19384 KB Output is correct
17 Correct 10 ms 19380 KB Output is correct
18 Correct 16 ms 30416 KB Output is correct
19 Correct 16 ms 29892 KB Output is correct
20 Correct 16 ms 29840 KB Output is correct
21 Correct 17 ms 21780 KB Output is correct
22 Correct 17 ms 21852 KB Output is correct
23 Correct 17 ms 21916 KB Output is correct
24 Correct 20 ms 21840 KB Output is correct
25 Correct 513 ms 479268 KB Output is correct
26 Correct 419 ms 460848 KB Output is correct
27 Correct 419 ms 460936 KB Output is correct
28 Correct 683 ms 139404 KB Output is correct
29 Correct 681 ms 140392 KB Output is correct
30 Correct 698 ms 139256 KB Output is correct
31 Correct 648 ms 139508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19160 KB Output is correct
2 Correct 10 ms 19284 KB Output is correct
3 Correct 494 ms 164224 KB Output is correct
4 Correct 502 ms 168652 KB Output is correct
5 Correct 495 ms 161920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19156 KB Output is correct
2 Correct 8 ms 19120 KB Output is correct
3 Correct 9 ms 19132 KB Output is correct
4 Correct 8 ms 19156 KB Output is correct
5 Correct 9 ms 19156 KB Output is correct
6 Correct 9 ms 19216 KB Output is correct
7 Correct 10 ms 19120 KB Output is correct
8 Correct 9 ms 19216 KB Output is correct
9 Correct 11 ms 19160 KB Output is correct
10 Correct 8 ms 19156 KB Output is correct
11 Correct 8 ms 19796 KB Output is correct
12 Correct 11 ms 19768 KB Output is correct
13 Correct 9 ms 19824 KB Output is correct
14 Correct 9 ms 19376 KB Output is correct
15 Correct 9 ms 19412 KB Output is correct
16 Correct 9 ms 19384 KB Output is correct
17 Correct 10 ms 19380 KB Output is correct
18 Correct 16 ms 30416 KB Output is correct
19 Correct 16 ms 29892 KB Output is correct
20 Correct 16 ms 29840 KB Output is correct
21 Correct 17 ms 21780 KB Output is correct
22 Correct 17 ms 21852 KB Output is correct
23 Correct 17 ms 21916 KB Output is correct
24 Correct 20 ms 21840 KB Output is correct
25 Correct 11 ms 19216 KB Output is correct
26 Correct 18 ms 28228 KB Output is correct
27 Correct 18 ms 28276 KB Output is correct
28 Correct 19 ms 28356 KB Output is correct
29 Correct 19 ms 28320 KB Output is correct
30 Correct 21 ms 21964 KB Output is correct
31 Correct 24 ms 22552 KB Output is correct
32 Correct 21 ms 22172 KB Output is correct
33 Correct 21 ms 22356 KB Output is correct
34 Correct 513 ms 479268 KB Output is correct
35 Correct 419 ms 460848 KB Output is correct
36 Correct 419 ms 460936 KB Output is correct
37 Correct 683 ms 139404 KB Output is correct
38 Correct 681 ms 140392 KB Output is correct
39 Correct 698 ms 139256 KB Output is correct
40 Correct 648 ms 139508 KB Output is correct
41 Correct 10 ms 19160 KB Output is correct
42 Correct 10 ms 19284 KB Output is correct
43 Correct 494 ms 164224 KB Output is correct
44 Correct 502 ms 168652 KB Output is correct
45 Correct 495 ms 161920 KB Output is correct
46 Execution timed out 2062 ms 272236 KB Time limit exceeded
47 Halted 0 ms 0 KB -