답안 #805916

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
805916 2023-08-04T02:15:11 Z guagua0407 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
279 ms 59048 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];
}
 
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<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

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 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 9 ms 14580 KB Output is correct
15 Correct 8 ms 14548 KB Output is correct
16 Correct 7 ms 14576 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 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 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 9 ms 14580 KB Output is correct
15 Correct 8 ms 14548 KB Output is correct
16 Correct 7 ms 14576 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 10 ms 15572 KB Output is correct
19 Correct 13 ms 15700 KB Output is correct
20 Correct 9 ms 15572 KB Output is correct
21 Correct 9 ms 15316 KB Output is correct
22 Correct 9 ms 15444 KB Output is correct
23 Correct 11 ms 15380 KB Output is correct
24 Correct 11 ms 15444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 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 9 ms 14580 KB Output is correct
15 Correct 8 ms 14548 KB Output is correct
16 Correct 7 ms 14576 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 10 ms 15572 KB Output is correct
19 Correct 13 ms 15700 KB Output is correct
20 Correct 9 ms 15572 KB Output is correct
21 Correct 9 ms 15316 KB Output is correct
22 Correct 9 ms 15444 KB Output is correct
23 Correct 11 ms 15380 KB Output is correct
24 Correct 11 ms 15444 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 9 ms 15444 KB Output is correct
27 Correct 10 ms 15528 KB Output is correct
28 Correct 9 ms 15444 KB Output is correct
29 Correct 10 ms 15444 KB Output is correct
30 Correct 10 ms 15316 KB Output is correct
31 Correct 11 ms 15320 KB Output is correct
32 Correct 10 ms 15208 KB Output is correct
33 Correct 11 ms 15316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 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 9 ms 14580 KB Output is correct
15 Correct 8 ms 14548 KB Output is correct
16 Correct 7 ms 14576 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 10 ms 15572 KB Output is correct
19 Correct 13 ms 15700 KB Output is correct
20 Correct 9 ms 15572 KB Output is correct
21 Correct 9 ms 15316 KB Output is correct
22 Correct 9 ms 15444 KB Output is correct
23 Correct 11 ms 15380 KB Output is correct
24 Correct 11 ms 15444 KB Output is correct
25 Correct 125 ms 59048 KB Output is correct
26 Correct 106 ms 57152 KB Output is correct
27 Correct 102 ms 57120 KB Output is correct
28 Correct 215 ms 52092 KB Output is correct
29 Correct 199 ms 49496 KB Output is correct
30 Correct 177 ms 51412 KB Output is correct
31 Correct 213 ms 50760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14464 KB Output is correct
2 Correct 8 ms 14548 KB Output is correct
3 Correct 195 ms 46368 KB Output is correct
4 Correct 188 ms 46420 KB Output is correct
5 Correct 229 ms 46280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 10 ms 14548 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 8 ms 14512 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 10 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 7 ms 14520 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 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 9 ms 14580 KB Output is correct
15 Correct 8 ms 14548 KB Output is correct
16 Correct 7 ms 14576 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 10 ms 15572 KB Output is correct
19 Correct 13 ms 15700 KB Output is correct
20 Correct 9 ms 15572 KB Output is correct
21 Correct 9 ms 15316 KB Output is correct
22 Correct 9 ms 15444 KB Output is correct
23 Correct 11 ms 15380 KB Output is correct
24 Correct 11 ms 15444 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 9 ms 15444 KB Output is correct
27 Correct 10 ms 15528 KB Output is correct
28 Correct 9 ms 15444 KB Output is correct
29 Correct 10 ms 15444 KB Output is correct
30 Correct 10 ms 15316 KB Output is correct
31 Correct 11 ms 15320 KB Output is correct
32 Correct 10 ms 15208 KB Output is correct
33 Correct 11 ms 15316 KB Output is correct
34 Correct 125 ms 59048 KB Output is correct
35 Correct 106 ms 57152 KB Output is correct
36 Correct 102 ms 57120 KB Output is correct
37 Correct 215 ms 52092 KB Output is correct
38 Correct 199 ms 49496 KB Output is correct
39 Correct 177 ms 51412 KB Output is correct
40 Correct 213 ms 50760 KB Output is correct
41 Correct 8 ms 14464 KB Output is correct
42 Correct 8 ms 14548 KB Output is correct
43 Correct 195 ms 46368 KB Output is correct
44 Correct 188 ms 46420 KB Output is correct
45 Correct 229 ms 46280 KB Output is correct
46 Correct 125 ms 54604 KB Output is correct
47 Correct 122 ms 54576 KB Output is correct
48 Correct 131 ms 54556 KB Output is correct
49 Correct 130 ms 54608 KB Output is correct
50 Correct 249 ms 45900 KB Output is correct
51 Correct 274 ms 46020 KB Output is correct
52 Correct 243 ms 45988 KB Output is correct
53 Correct 279 ms 46032 KB Output is correct