# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
805915 | 2023-08-04T02:12:23 Z | guagua0407 | Cat Exercise (JOI23_ho_t4) | C++17 | 247 ms | 58996 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[18][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<18;i++){ if(len&(1<<i)){ a=up[i][a]; } } if(a==b) return a; for(int i=17;i>=0;i--){ int ta=up[i][a]; int tb=up[i][b]; if(ta!=tb){ a=ta; b=tb; } } return up[0][a]; } int 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<18;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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 10 ms | 14488 KB | Output is correct |
13 | Correct | 7 ms | 14548 KB | Output is correct |
14 | Correct | 7 ms | 14448 KB | Output is correct |
15 | Correct | 8 ms | 14576 KB | Output is correct |
16 | Correct | 8 ms | 14568 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 10 ms | 14488 KB | Output is correct |
13 | Correct | 7 ms | 14548 KB | Output is correct |
14 | Correct | 7 ms | 14448 KB | Output is correct |
15 | Correct | 8 ms | 14576 KB | Output is correct |
16 | Correct | 8 ms | 14568 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 11 ms | 15648 KB | Output is correct |
19 | Correct | 10 ms | 15532 KB | Output is correct |
20 | Correct | 10 ms | 15600 KB | Output is correct |
21 | Correct | 10 ms | 15288 KB | Output is correct |
22 | Correct | 11 ms | 15468 KB | Output is correct |
23 | Correct | 12 ms | 15400 KB | Output is correct |
24 | Correct | 14 ms | 15444 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 10 ms | 14488 KB | Output is correct |
13 | Correct | 7 ms | 14548 KB | Output is correct |
14 | Correct | 7 ms | 14448 KB | Output is correct |
15 | Correct | 8 ms | 14576 KB | Output is correct |
16 | Correct | 8 ms | 14568 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 11 ms | 15648 KB | Output is correct |
19 | Correct | 10 ms | 15532 KB | Output is correct |
20 | Correct | 10 ms | 15600 KB | Output is correct |
21 | Correct | 10 ms | 15288 KB | Output is correct |
22 | Correct | 11 ms | 15468 KB | Output is correct |
23 | Correct | 12 ms | 15400 KB | Output is correct |
24 | Correct | 14 ms | 15444 KB | Output is correct |
25 | Correct | 8 ms | 14420 KB | Output is correct |
26 | Correct | 10 ms | 15444 KB | Output is correct |
27 | Correct | 10 ms | 15444 KB | Output is correct |
28 | Correct | 12 ms | 15432 KB | Output is correct |
29 | Correct | 15 ms | 15516 KB | Output is correct |
30 | Correct | 10 ms | 15316 KB | Output is correct |
31 | Correct | 11 ms | 15324 KB | Output is correct |
32 | Correct | 12 ms | 15316 KB | Output is correct |
33 | Correct | 13 ms | 15328 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 10 ms | 14488 KB | Output is correct |
13 | Correct | 7 ms | 14548 KB | Output is correct |
14 | Correct | 7 ms | 14448 KB | Output is correct |
15 | Correct | 8 ms | 14576 KB | Output is correct |
16 | Correct | 8 ms | 14568 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 11 ms | 15648 KB | Output is correct |
19 | Correct | 10 ms | 15532 KB | Output is correct |
20 | Correct | 10 ms | 15600 KB | Output is correct |
21 | Correct | 10 ms | 15288 KB | Output is correct |
22 | Correct | 11 ms | 15468 KB | Output is correct |
23 | Correct | 12 ms | 15400 KB | Output is correct |
24 | Correct | 14 ms | 15444 KB | Output is correct |
25 | Correct | 107 ms | 58996 KB | Output is correct |
26 | Correct | 104 ms | 57144 KB | Output is correct |
27 | Correct | 108 ms | 57152 KB | Output is correct |
28 | Correct | 181 ms | 52160 KB | Output is correct |
29 | Correct | 182 ms | 49412 KB | Output is correct |
30 | Correct | 171 ms | 51300 KB | Output is correct |
31 | Correct | 170 ms | 50772 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 14420 KB | Output is correct |
2 | Correct | 10 ms | 14548 KB | Output is correct |
3 | Correct | 200 ms | 46252 KB | Output is correct |
4 | Correct | 187 ms | 46424 KB | Output is correct |
5 | Correct | 178 ms | 46240 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 14420 KB | Output is correct |
2 | Correct | 9 ms | 14436 KB | Output is correct |
3 | Correct | 8 ms | 14420 KB | Output is correct |
4 | Correct | 8 ms | 14420 KB | Output is correct |
5 | Correct | 8 ms | 14460 KB | Output is correct |
6 | Correct | 8 ms | 14448 KB | Output is correct |
7 | Correct | 8 ms | 14420 KB | Output is correct |
8 | Correct | 8 ms | 14500 KB | Output is correct |
9 | Correct | 8 ms | 14420 KB | Output is correct |
10 | Correct | 7 ms | 14420 KB | Output is correct |
11 | Correct | 8 ms | 14548 KB | Output is correct |
12 | Correct | 10 ms | 14488 KB | Output is correct |
13 | Correct | 7 ms | 14548 KB | Output is correct |
14 | Correct | 7 ms | 14448 KB | Output is correct |
15 | Correct | 8 ms | 14576 KB | Output is correct |
16 | Correct | 8 ms | 14568 KB | Output is correct |
17 | Correct | 8 ms | 14548 KB | Output is correct |
18 | Correct | 11 ms | 15648 KB | Output is correct |
19 | Correct | 10 ms | 15532 KB | Output is correct |
20 | Correct | 10 ms | 15600 KB | Output is correct |
21 | Correct | 10 ms | 15288 KB | Output is correct |
22 | Correct | 11 ms | 15468 KB | Output is correct |
23 | Correct | 12 ms | 15400 KB | Output is correct |
24 | Correct | 14 ms | 15444 KB | Output is correct |
25 | Correct | 8 ms | 14420 KB | Output is correct |
26 | Correct | 10 ms | 15444 KB | Output is correct |
27 | Correct | 10 ms | 15444 KB | Output is correct |
28 | Correct | 12 ms | 15432 KB | Output is correct |
29 | Correct | 15 ms | 15516 KB | Output is correct |
30 | Correct | 10 ms | 15316 KB | Output is correct |
31 | Correct | 11 ms | 15324 KB | Output is correct |
32 | Correct | 12 ms | 15316 KB | Output is correct |
33 | Correct | 13 ms | 15328 KB | Output is correct |
34 | Correct | 107 ms | 58996 KB | Output is correct |
35 | Correct | 104 ms | 57144 KB | Output is correct |
36 | Correct | 108 ms | 57152 KB | Output is correct |
37 | Correct | 181 ms | 52160 KB | Output is correct |
38 | Correct | 182 ms | 49412 KB | Output is correct |
39 | Correct | 171 ms | 51300 KB | Output is correct |
40 | Correct | 170 ms | 50772 KB | Output is correct |
41 | Correct | 8 ms | 14420 KB | Output is correct |
42 | Correct | 10 ms | 14548 KB | Output is correct |
43 | Correct | 200 ms | 46252 KB | Output is correct |
44 | Correct | 187 ms | 46424 KB | Output is correct |
45 | Correct | 178 ms | 46240 KB | Output is correct |
46 | Correct | 143 ms | 54612 KB | Output is correct |
47 | Correct | 123 ms | 54604 KB | Output is correct |
48 | Correct | 134 ms | 54608 KB | Output is correct |
49 | Correct | 128 ms | 54484 KB | Output is correct |
50 | Correct | 247 ms | 46020 KB | Output is correct |
51 | Correct | 246 ms | 45984 KB | Output is correct |
52 | Correct | 242 ms | 45912 KB | Output is correct |
53 | Correct | 229 ms | 46084 KB | Output is correct |