제출 #1125121

#제출 시각아이디문제언어결과실행 시간메모리
1125121panCat Exercise (JOI23_ho_t4)C++20
100 / 100
488 ms72200 KiB
#include <bits/stdc++.h> //#include "includeall.h" //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define endl '\n' #define f first #define s second #define pb push_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define input(x) scanf("%lld", &x); #define input2(x, y) scanf("%lld%lld", &x, &y); #define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z); #define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a); #define print(x, y) printf("%lld%c", x, y); #define show(x) cerr << #x << " is " << x << endl; #define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl; #define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl; #define all(x) x.begin(), x.end() #define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); #define FOR(i, x, n) for (ll i =x; i<=n; ++i) #define RFOR(i, x, n) for (ll i =x; i>=n; --i) using namespace std; //using namespace __gnu_pbds; //#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> //#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> typedef long long ll; //typedef __int128 ull; typedef long double ld; typedef pair<ld, ll> pd; typedef pair<string, ll> psl; typedef pair<ll, ll> pi; typedef pair<pi, ll> pii; typedef pair<pi, pi> piii; // General ll n, u, v, ans = 0; ll p[200005], r[200005]; // 2-k decomposition vector<ll> adj[200005]; ll dist[200005]; ll twok[200005][20]; void dfs(ll x= 1, ll p = -1) { for (ll k=1; k<20; ++k) { if (twok[x][k-1]==-1) twok[x][k] = -1; else twok[x][k] = twok[twok[x][k-1]][k-1]; } for (ll u: adj[x]) if (u!=p) { dist[u] = dist[x] + 1; twok[u][0] = x; dfs(u, x); } } ll lca(ll a, ll b) { if (a==b) return a; if (dist[a]< dist[b]) swap(a, b); for (ll k=19; k>=0; --k) if ((dist[a] - dist[b]) & (1LL << k)) a = twok[a][k]; if (a==b) return a; for (ll k=19; k>=0; --k) { if (twok[a][k]==twok[b][k]) continue; a = twok[a][k], b = twok[b][k]; } return twok[a][0]; } ll sp(ll a, ll b) { return dist[a] + dist[b] - 2*dist[lca(a, b)]; } // UFDS Tree vector<ll> adj2[200005]; ll par[200005]; ll find(ll x) {return (par[x]==x)?x:par[x] = find(par[x]);} void unite(ll x, ll y) { x = find(x), y = find(y); if (x==y) return; if (p[x] > p[y]) swap(x, y); par[x] = y; adj2[y].pb(x); } void dfs2(ll x, ll v = 0) { //show2(x, v); ans = max(ans, v); for (ll u: adj2[x]) dfs2(u, v + sp(x, u)); } int main() { cin >> n; for (ll i=1; i<=n; ++i) {cin >> p[i]; r[p[i]] = i;} for (ll i=0; i<n-1; ++i) { cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dist[1] = 0; twok[1][0] = -1; dfs(); for (ll i=1; i<=n; ++i) par[i] = i; for (ll i=1; i<=n; ++i) for (ll u: adj[r[i]]) {if (p[u] < i) unite(u, r[i]);} //for (ll i=1; i<=n; ++i) {show(i) ; for (ll u: adj2[i]) show(u);} dfs2(r[n]); cout << ans << endl; return 0; }
#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...