제출 #1255596

#제출 시각아이디문제언어결과실행 시간메모리
1255596thdh__Cat Exercise (JOI23_ho_t4)C++20
100 / 100
242 ms61600 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define eb emplace_back #define pu push #define ins insert #define fi first #define se second #define all(a) a.begin(),a.end() #define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fu(x,a,b) for (auto x=a;x<=b;x++) #define fd(x,a,b) for (auto x=a;x>=b;x--) #define int ll using namespace std; //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); /* Competitive Programming notes that I need to study & fix my dumbass self: 1. Coding: - Always be sure to check the memory of arrays (maybe use vectors), for loops - Always try to maximize the memory if possible, even if you are going for subtasks - Do not exploit #define int long long, it will kill you 2. Stress: - Always try generating big testcases and try if they run 3. Time management: - Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked - Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time Time management schedule: Offline / LAH days (4 problems - 3h): 15' thinking of solution / idea 1. no idea: skip 2. yes idea: continue thinking for <= 15' + implementing: <= 20' + brute-force: <= 5' + test generator: <= 5' I hate offline because I am dumb */ typedef pair<int, int> ii; const int N = 2e5+5; const int M = 20; const int B = 750; const int mod = 1e9+7; const int inf = 1e18; using cd = complex<double>; const long double PI = acos(-1); int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} int n; int a[N], h[N]; vector<int> adj[N]; int up[N][M]; int dp[N]; void dfs(int u, int p) { up[u][0] = p; for (int i = 1; i < M; i++) up[u][i] = up[up[u][i-1]][i-1]; for (auto v : adj[u]) { if (v == p) continue; h[v] = h[u] + 1; dfs(v, u); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u,v); int diff = h[u] - h[v]; for (int i = 0; i < M; i++) if (diff>>i & 1) u = up[u][i]; if (u == v) return u; for (int i = M-1; i >= 0; i--) if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i];; return up[u][0]; } int dist(int u, int v) { return h[u] + h[v] - 2 * h[lca(u,v)]; } int par[N], sz[N]; int find(int u) { if (u == par[u]) return u; return par[u] = find(par[u]); } void merge(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (a[u] < a[v]) swap(u,v); par[v] = u; sz[u] += sz[v]; } bool cmp(int i, int j) { return a[i] < a[j]; } void solve() { cin>>n; int root = 0; for (int i = 1; i <= n; i++) { cin>>a[i]; if (a[i] == n) root = i; } for (int i = 1; i < n; i++) { int u,v; cin>>u>>v; adj[u].pb(v); adj[v].pb(u); } dfs(1, 0); vector<int> id; for (int i = 1; i <= n; i++) { id.pb(i); par[i] = i; sz[i] = 1; } sort(all(id), cmp); for (int i = 0; i < n; i++) { int u = id[i]; for (auto v : adj[u]) { if (a[v] > a[u]) continue; v = find(v); dp[u] = max(dp[u], dp[v] + dist(u, v)); merge(u, v); } } cout<<dp[root]; } /* Go through the mistakes you usually make and revise your code, for god's sake... */ signed main() { bruh //freopen("input.inp","r",stdin); //freopen("output.inp","w",stdout); int t = 1; // cin>>t; while (t--) { solve(); cout<<"\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...