답안 #792182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792182 2023-07-24T17:08:35 Z cig32 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
611 ms 145216 KB
#pragma GCC optimize("Ofast")
#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 set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
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;
  vector<pair<int, int>> edges[n+1];
  for(int i=1; i<n; i++) {
    int a, b;
    cin >> a >> b;
    edges[max(p[a],p[b])].push_back({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]=i;
  for(int i=1; i<=n; i++) nums[i].insert(p[i]);
  int dp[n+1];
  for(int i=1; i<=n; i++) dp[i] = 0;
  for(int i=1; i<=n; i++) {
    for(pair<int,int> x: edges[i]) {
      
      int to = (p[x.first] == i ? x.second : x.first);
      to = set_of(to);
      int mx = *nums[to].rbegin();
      dp[i] = max(dp[i], dist(pos[i], pos[mx]) + dp[mx]);
    }
    for(pair<int,int> x: edges[i]) {
      int u = set_of(x.first), v = set_of(x.second);
      if(nums[u].size() < nums[v].size()) nums[u].swap(nums[v]);
      for(int x: nums[v]) nums[u].insert(x);
      nums[v].clear();
      union_(u,v);
      nums[u].swap(nums[set_of(u)]);
    }
  }
  cout << dp[n] << "\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 'void solve(long long int)':
Main.cpp:73: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]
   73 |   for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
      |                ~^~~~~~~~~~~~~
Main.cpp:75: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]
   75 |   for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
      |                ~^~~~~~~~~~~~~
Main.cpp:77: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]
   77 |     for(int j=0; j+(1<<i)-1<euler.size(); j++) {
      |                  ~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
11 Correct 9 ms 19388 KB Output is correct
12 Correct 10 ms 19412 KB Output is correct
13 Correct 9 ms 19284 KB Output is correct
14 Correct 10 ms 19324 KB Output is correct
15 Correct 12 ms 19284 KB Output is correct
16 Correct 12 ms 19284 KB Output is correct
17 Correct 11 ms 19352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
11 Correct 9 ms 19388 KB Output is correct
12 Correct 10 ms 19412 KB Output is correct
13 Correct 9 ms 19284 KB Output is correct
14 Correct 10 ms 19324 KB Output is correct
15 Correct 12 ms 19284 KB Output is correct
16 Correct 12 ms 19284 KB Output is correct
17 Correct 11 ms 19352 KB Output is correct
18 Correct 13 ms 21828 KB Output is correct
19 Correct 13 ms 21844 KB Output is correct
20 Correct 13 ms 21852 KB Output is correct
21 Correct 15 ms 21952 KB Output is correct
22 Correct 15 ms 21884 KB Output is correct
23 Correct 16 ms 21984 KB Output is correct
24 Correct 16 ms 21972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
11 Correct 9 ms 19388 KB Output is correct
12 Correct 10 ms 19412 KB Output is correct
13 Correct 9 ms 19284 KB Output is correct
14 Correct 10 ms 19324 KB Output is correct
15 Correct 12 ms 19284 KB Output is correct
16 Correct 12 ms 19284 KB Output is correct
17 Correct 11 ms 19352 KB Output is correct
18 Correct 13 ms 21828 KB Output is correct
19 Correct 13 ms 21844 KB Output is correct
20 Correct 13 ms 21852 KB Output is correct
21 Correct 15 ms 21952 KB Output is correct
22 Correct 15 ms 21884 KB Output is correct
23 Correct 16 ms 21984 KB Output is correct
24 Correct 16 ms 21972 KB Output is correct
25 Correct 10 ms 19116 KB Output is correct
26 Correct 15 ms 21716 KB Output is correct
27 Correct 14 ms 21844 KB Output is correct
28 Correct 14 ms 21728 KB Output is correct
29 Correct 15 ms 21824 KB Output is correct
30 Correct 15 ms 21716 KB Output is correct
31 Correct 15 ms 21600 KB Output is correct
32 Correct 17 ms 21716 KB Output is correct
33 Correct 15 ms 21652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
11 Correct 9 ms 19388 KB Output is correct
12 Correct 10 ms 19412 KB Output is correct
13 Correct 9 ms 19284 KB Output is correct
14 Correct 10 ms 19324 KB Output is correct
15 Correct 12 ms 19284 KB Output is correct
16 Correct 12 ms 19284 KB Output is correct
17 Correct 11 ms 19352 KB Output is correct
18 Correct 13 ms 21828 KB Output is correct
19 Correct 13 ms 21844 KB Output is correct
20 Correct 13 ms 21852 KB Output is correct
21 Correct 15 ms 21952 KB Output is correct
22 Correct 15 ms 21884 KB Output is correct
23 Correct 16 ms 21984 KB Output is correct
24 Correct 16 ms 21972 KB Output is correct
25 Correct 159 ms 142512 KB Output is correct
26 Correct 167 ms 142136 KB Output is correct
27 Correct 161 ms 142272 KB Output is correct
28 Correct 430 ms 143736 KB Output is correct
29 Correct 420 ms 145216 KB Output is correct
30 Correct 454 ms 144308 KB Output is correct
31 Correct 424 ms 144828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19284 KB Output is correct
3 Correct 378 ms 130848 KB Output is correct
4 Correct 367 ms 130736 KB Output is correct
5 Correct 391 ms 130908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19156 KB Output is correct
2 Correct 10 ms 19132 KB Output is correct
3 Correct 13 ms 19128 KB Output is correct
4 Correct 10 ms 19192 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 10 ms 19156 KB Output is correct
7 Correct 10 ms 19136 KB Output is correct
8 Correct 10 ms 19204 KB Output is correct
9 Correct 10 ms 19124 KB Output is correct
10 Correct 12 ms 19160 KB Output is correct
11 Correct 9 ms 19388 KB Output is correct
12 Correct 10 ms 19412 KB Output is correct
13 Correct 9 ms 19284 KB Output is correct
14 Correct 10 ms 19324 KB Output is correct
15 Correct 12 ms 19284 KB Output is correct
16 Correct 12 ms 19284 KB Output is correct
17 Correct 11 ms 19352 KB Output is correct
18 Correct 13 ms 21828 KB Output is correct
19 Correct 13 ms 21844 KB Output is correct
20 Correct 13 ms 21852 KB Output is correct
21 Correct 15 ms 21952 KB Output is correct
22 Correct 15 ms 21884 KB Output is correct
23 Correct 16 ms 21984 KB Output is correct
24 Correct 16 ms 21972 KB Output is correct
25 Correct 10 ms 19116 KB Output is correct
26 Correct 15 ms 21716 KB Output is correct
27 Correct 14 ms 21844 KB Output is correct
28 Correct 14 ms 21728 KB Output is correct
29 Correct 15 ms 21824 KB Output is correct
30 Correct 15 ms 21716 KB Output is correct
31 Correct 15 ms 21600 KB Output is correct
32 Correct 17 ms 21716 KB Output is correct
33 Correct 15 ms 21652 KB Output is correct
34 Correct 159 ms 142512 KB Output is correct
35 Correct 167 ms 142136 KB Output is correct
36 Correct 161 ms 142272 KB Output is correct
37 Correct 430 ms 143736 KB Output is correct
38 Correct 420 ms 145216 KB Output is correct
39 Correct 454 ms 144308 KB Output is correct
40 Correct 424 ms 144828 KB Output is correct
41 Correct 10 ms 19156 KB Output is correct
42 Correct 10 ms 19284 KB Output is correct
43 Correct 378 ms 130848 KB Output is correct
44 Correct 367 ms 130736 KB Output is correct
45 Correct 391 ms 130908 KB Output is correct
46 Correct 350 ms 138120 KB Output is correct
47 Correct 373 ms 137044 KB Output is correct
48 Correct 354 ms 138716 KB Output is correct
49 Correct 338 ms 137092 KB Output is correct
50 Correct 578 ms 133644 KB Output is correct
51 Correct 611 ms 131536 KB Output is correct
52 Correct 565 ms 133012 KB Output is correct
53 Correct 600 ms 131408 KB Output is correct