Submission #805911

# Submission time Handle Problem Language Result Execution time Memory
805911 2023-08-04T02:09:33 Z guagua0407 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
274 ms 71332 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

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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 9 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 7 ms 14592 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 9 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 7 ms 14592 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 10 ms 15836 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 12 ms 15844 KB Output is correct
21 Correct 9 ms 15528 KB Output is correct
22 Correct 10 ms 15652 KB Output is correct
23 Correct 9 ms 15572 KB Output is correct
24 Correct 9 ms 15528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 9 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 7 ms 14592 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 10 ms 15836 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 12 ms 15844 KB Output is correct
21 Correct 9 ms 15528 KB Output is correct
22 Correct 10 ms 15652 KB Output is correct
23 Correct 9 ms 15572 KB Output is correct
24 Correct 9 ms 15528 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 11 ms 15708 KB Output is correct
27 Correct 9 ms 15700 KB Output is correct
28 Correct 10 ms 15704 KB Output is correct
29 Correct 11 ms 15708 KB Output is correct
30 Correct 10 ms 15324 KB Output is correct
31 Correct 10 ms 15456 KB Output is correct
32 Correct 10 ms 15444 KB Output is correct
33 Correct 10 ms 15444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 9 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 7 ms 14592 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 10 ms 15836 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 12 ms 15844 KB Output is correct
21 Correct 9 ms 15528 KB Output is correct
22 Correct 10 ms 15652 KB Output is correct
23 Correct 9 ms 15572 KB Output is correct
24 Correct 9 ms 15528 KB Output is correct
25 Correct 120 ms 71332 KB Output is correct
26 Correct 107 ms 68788 KB Output is correct
27 Correct 129 ms 68836 KB Output is correct
28 Correct 171 ms 60396 KB Output is correct
29 Correct 183 ms 56396 KB Output is correct
30 Correct 175 ms 59396 KB Output is correct
31 Correct 190 ms 58460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14552 KB Output is correct
3 Correct 195 ms 52132 KB Output is correct
4 Correct 184 ms 52276 KB Output is correct
5 Correct 179 ms 52064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14532 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14488 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14416 KB Output is correct
7 Correct 7 ms 14448 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14444 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 9 ms 14548 KB Output is correct
12 Correct 8 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 9 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 7 ms 14592 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 10 ms 15836 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 12 ms 15844 KB Output is correct
21 Correct 9 ms 15528 KB Output is correct
22 Correct 10 ms 15652 KB Output is correct
23 Correct 9 ms 15572 KB Output is correct
24 Correct 9 ms 15528 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 11 ms 15708 KB Output is correct
27 Correct 9 ms 15700 KB Output is correct
28 Correct 10 ms 15704 KB Output is correct
29 Correct 11 ms 15708 KB Output is correct
30 Correct 10 ms 15324 KB Output is correct
31 Correct 10 ms 15456 KB Output is correct
32 Correct 10 ms 15444 KB Output is correct
33 Correct 10 ms 15444 KB Output is correct
34 Correct 120 ms 71332 KB Output is correct
35 Correct 107 ms 68788 KB Output is correct
36 Correct 129 ms 68836 KB Output is correct
37 Correct 171 ms 60396 KB Output is correct
38 Correct 183 ms 56396 KB Output is correct
39 Correct 175 ms 59396 KB Output is correct
40 Correct 190 ms 58460 KB Output is correct
41 Correct 7 ms 14420 KB Output is correct
42 Correct 8 ms 14552 KB Output is correct
43 Correct 195 ms 52132 KB Output is correct
44 Correct 184 ms 52276 KB Output is correct
45 Correct 179 ms 52064 KB Output is correct
46 Correct 125 ms 64704 KB Output is correct
47 Correct 135 ms 64800 KB Output is correct
48 Correct 139 ms 64724 KB Output is correct
49 Correct 144 ms 64804 KB Output is correct
50 Correct 274 ms 51352 KB Output is correct
51 Correct 209 ms 51512 KB Output is correct
52 Correct 218 ms 51436 KB Output is correct
53 Correct 264 ms 51520 KB Output is correct