# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
805912 | 2023-08-04T02:10:40 Z | guagua0407 | Cat Exercise (JOI23_ho_t4) | C++17 | 248 ms | 60568 KB |
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> #define f first #define s second #define all(x) x.begin(),x.end() #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } const int mxn=2e5+5; vector<int> adj[mxn],adj2[mxn]; vector<int> nxt[mxn]; int p[mxn]; int depth[mxn]; int up[20][mxn]; struct DSU{ vector<int> e; vector<int> mx; DSU(int n){ e=vector<int>(n,-1); mx=vector<int>(n); for(int i=0;i<n;i++){ mx[i]=i; } } int find(int x){ return (e[x]<0?x:e[x]=find(e[x])); } bool unite(int a,int b){ a=find(a); b=find(b); if(a==b) return false; if(e[a]>e[b]) swap(a,b); mx[a]=max(mx[a],mx[b]); e[a]+=e[b]; e[b]=a; return true; } }; void dfs(int v,int p=-1){ if(p!=-1) depth[v]=depth[p]+1; if(p!=-1) up[0][v]=p; for(auto u:adj2[v]){ if(u==p) continue; dfs(u,v); } } int lca(int a,int b){ if(depth[a]<depth[b]) swap(a,b); int len=depth[a]-depth[b]; for(int i=0;i<20;i++){ if(len&(1<<i)){ a=up[i][a]; } } if(a==b) return a; for(int i=19;i>=0;i--){ int ta=up[i][a]; int tb=up[i][b]; if(ta!=tb){ a=ta; b=tb; } } return up[0][a]; } ll dist(int a,int b){ int c=lca(a,b); return depth[a]+depth[b]-2*depth[c]; } ll solve(int v){ ll ans=0; for(auto u:nxt[v]){ ans=max(ans,dist(u,v)+solve(u)); } return ans; } int main() {_ int n; cin>>n; for(int i=0;i<n;i++){ cin>>p[i]; p[i]--; } for(int i=0;i<n-1;i++){ int a,b; cin>>a>>b; a--; b--; a=p[a]; b=p[b]; adj2[a].push_back(b); adj2[b].push_back(a); if(a>b) swap(a,b); adj[b].push_back(a); } DSU dsu(n); for(int i=0;i<n;i++){ for(auto v:adj[i]){ nxt[i].push_back(dsu.mx[dsu.find(v)]); } for(auto v:adj[i]){ dsu.unite(i,v); } } dfs(0); for(int i=1;i<20;i++){ for(int j=0;j<n;j++){ up[i][j]=up[i-1][up[i-1][j]]; } } cout<<solve(n-1); return 0; } //maybe its multiset not set
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 8 ms | 14548 KB | Output is correct |
13 | Correct | 8 ms | 14548 KB | Output is correct |
14 | Correct | 8 ms | 14548 KB | Output is correct |
15 | Correct | 8 ms | 14592 KB | Output is correct |
16 | Correct | 8 ms | 14548 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 8 ms | 14548 KB | Output is correct |
13 | Correct | 8 ms | 14548 KB | Output is correct |
14 | Correct | 8 ms | 14548 KB | Output is correct |
15 | Correct | 8 ms | 14592 KB | Output is correct |
16 | Correct | 8 ms | 14548 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 10 ms | 15572 KB | Output is correct |
19 | Correct | 10 ms | 15568 KB | Output is correct |
20 | Correct | 9 ms | 15572 KB | Output is correct |
21 | Correct | 10 ms | 15344 KB | Output is correct |
22 | Correct | 10 ms | 15516 KB | Output is correct |
23 | Correct | 12 ms | 15424 KB | Output is correct |
24 | Correct | 10 ms | 15444 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 8 ms | 14548 KB | Output is correct |
13 | Correct | 8 ms | 14548 KB | Output is correct |
14 | Correct | 8 ms | 14548 KB | Output is correct |
15 | Correct | 8 ms | 14592 KB | Output is correct |
16 | Correct | 8 ms | 14548 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 10 ms | 15572 KB | Output is correct |
19 | Correct | 10 ms | 15568 KB | Output is correct |
20 | Correct | 9 ms | 15572 KB | Output is correct |
21 | Correct | 10 ms | 15344 KB | Output is correct |
22 | Correct | 10 ms | 15516 KB | Output is correct |
23 | Correct | 12 ms | 15424 KB | Output is correct |
24 | Correct | 10 ms | 15444 KB | Output is correct |
25 | Correct | 8 ms | 14420 KB | Output is correct |
26 | Correct | 10 ms | 15528 KB | Output is correct |
27 | Correct | 11 ms | 15576 KB | Output is correct |
28 | Correct | 10 ms | 15572 KB | Output is correct |
29 | Correct | 10 ms | 15572 KB | Output is correct |
30 | Correct | 10 ms | 15316 KB | Output is correct |
31 | Correct | 11 ms | 15292 KB | Output is correct |
32 | Correct | 13 ms | 15240 KB | Output is correct |
33 | Correct | 10 ms | 15316 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 8 ms | 14548 KB | Output is correct |
13 | Correct | 8 ms | 14548 KB | Output is correct |
14 | Correct | 8 ms | 14548 KB | Output is correct |
15 | Correct | 8 ms | 14592 KB | Output is correct |
16 | Correct | 8 ms | 14548 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 10 ms | 15572 KB | Output is correct |
19 | Correct | 10 ms | 15568 KB | Output is correct |
20 | Correct | 9 ms | 15572 KB | Output is correct |
21 | Correct | 10 ms | 15344 KB | Output is correct |
22 | Correct | 10 ms | 15516 KB | Output is correct |
23 | Correct | 12 ms | 15424 KB | Output is correct |
24 | Correct | 10 ms | 15444 KB | Output is correct |
25 | Correct | 108 ms | 60568 KB | Output is correct |
26 | Correct | 114 ms | 58696 KB | Output is correct |
27 | Correct | 102 ms | 58740 KB | Output is correct |
28 | Correct | 178 ms | 53724 KB | Output is correct |
29 | Correct | 174 ms | 50996 KB | Output is correct |
30 | Correct | 164 ms | 52968 KB | Output is correct |
31 | Correct | 183 ms | 52376 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14548 KB | Output is correct |
3 | Correct | 196 ms | 47940 KB | Output is correct |
4 | Correct | 198 ms | 48012 KB | Output is correct |
5 | Correct | 180 ms | 47820 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 8 ms | 14420 KB | Output is correct |
3 | Correct | 8 ms | 14536 KB | Output is correct |
4 | Correct | 8 ms | 14532 KB | Output is correct |
5 | Correct | 8 ms | 14476 KB | Output is correct |
6 | Correct | 8 ms | 14420 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14420 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 8 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 8 ms | 14548 KB | Output is correct |
13 | Correct | 8 ms | 14548 KB | Output is correct |
14 | Correct | 8 ms | 14548 KB | Output is correct |
15 | Correct | 8 ms | 14592 KB | Output is correct |
16 | Correct | 8 ms | 14548 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 10 ms | 15572 KB | Output is correct |
19 | Correct | 10 ms | 15568 KB | Output is correct |
20 | Correct | 9 ms | 15572 KB | Output is correct |
21 | Correct | 10 ms | 15344 KB | Output is correct |
22 | Correct | 10 ms | 15516 KB | Output is correct |
23 | Correct | 12 ms | 15424 KB | Output is correct |
24 | Correct | 10 ms | 15444 KB | Output is correct |
25 | Correct | 8 ms | 14420 KB | Output is correct |
26 | Correct | 10 ms | 15528 KB | Output is correct |
27 | Correct | 11 ms | 15576 KB | Output is correct |
28 | Correct | 10 ms | 15572 KB | Output is correct |
29 | Correct | 10 ms | 15572 KB | Output is correct |
30 | Correct | 10 ms | 15316 KB | Output is correct |
31 | Correct | 11 ms | 15292 KB | Output is correct |
32 | Correct | 13 ms | 15240 KB | Output is correct |
33 | Correct | 10 ms | 15316 KB | Output is correct |
34 | Correct | 108 ms | 60568 KB | Output is correct |
35 | Correct | 114 ms | 58696 KB | Output is correct |
36 | Correct | 102 ms | 58740 KB | Output is correct |
37 | Correct | 178 ms | 53724 KB | Output is correct |
38 | Correct | 174 ms | 50996 KB | Output is correct |
39 | Correct | 164 ms | 52968 KB | Output is correct |
40 | Correct | 183 ms | 52376 KB | Output is correct |
41 | Correct | 8 ms | 14420 KB | Output is correct |
42 | Correct | 8 ms | 14548 KB | Output is correct |
43 | Correct | 196 ms | 47940 KB | Output is correct |
44 | Correct | 198 ms | 48012 KB | Output is correct |
45 | Correct | 180 ms | 47820 KB | Output is correct |
46 | Correct | 138 ms | 56256 KB | Output is correct |
47 | Correct | 131 ms | 56136 KB | Output is correct |
48 | Correct | 130 ms | 56132 KB | Output is correct |
49 | Correct | 128 ms | 56076 KB | Output is correct |
50 | Correct | 240 ms | 47692 KB | Output is correct |
51 | Correct | 248 ms | 47568 KB | Output is correct |
52 | Correct | 228 ms | 47536 KB | Output is correct |
53 | Correct | 231 ms | 47564 KB | Output is correct |