Submission #1150913

#TimeUsernameProblemLanguageResultExecution timeMemory
1150913jiahngCat Exercise (JOI23_ho_t4)C++20
54 / 100
136 ms74056 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ll int typedef pair<int,int> pi; typedef pair<ll,ll> pill; typedef vector <int> vi; typedef vector <pi> vpi; typedef pair<pi, ll> pii; typedef set <ll> si; typedef long double ld; #define f first #define s second #define mp make_pair #define FOR(i,s,e) for(int i=s;i<=int(e);++i) #define DEC(i,s,e) for(int i=s;i>=int(e);--i) #define pb push_back #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) #define aFOR(i,x) for (auto i: x) #define mem(x,i) memset(x,i,sizeof x) #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define INF (ll)1e18 #define MOD 1000000007 typedef pair <vi, int> pvi; typedef pair <int,pi> ipi; typedef vector <pii> vpii; typedef pair <pi,pi> pipi; #define maxn 500010 int N; int dp[maxn],A[maxn],B[maxn],best[maxn]; vi adj[maxn]; int p[maxn],twok[maxn][20],depth[maxn]; int fl(int x){ if (p[x] == x) return x; return p[x] = fl(p[x]); } void dfs(int x,int p){ twok[x][0] = p; FOR(i,1,19){ if (twok[x][i-1] == -1) break; twok[x][i] = twok[twok[x][i-1]][i-1]; } aFOR(i, adj[x]) if (i != p){ depth[i] = depth[x] + 1; dfs(i,x); } } int lca(int a,int b){ if (depth[a] < depth[b]) swap(a,b); int k = depth[a] - depth[b]; FOR(i,0,19) if (k & (1 << i)){ a = twok[a][i]; } if (a == b) return a; DEC(i,19,0) if (twok[a][i] != twok[b][i]){ a = twok[a][i]; b = twok[b][i]; } return twok[a][0]; } int d(int a,int b){ int c = lca(a,b); return depth[a] + depth[b] - 2 * depth[c]; } int32_t main(){ fast; cin >> N; FOR(i,1,N) cin >> A[i]; FOR(i,1,N) B[A[i]] = i; int a,b; FOR(i,1,N-1){ cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } mem(twok,-1); dfs(1,-1); FOR(i,1,N) p[i] = i; FOR(i,1,N){ int x = B[i]; aFOR(j, adj[x]) if (A[j] < i){ int opt = fl(j); dp[x] = max(dp[x],d(opt,x) + dp[opt]); p[opt] = x; } } cout << dp[B[N]]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...