/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 2.57 MiB
#2 Accepted 120ms 7.559 MiB
#3 Accepted 118ms 8.91 MiB
#4 Accepted 110ms 10.457 MiB
#5 Accepted 111ms 7.543 MiB
#6 Accepted 109ms 10.531 MiB
#7 Accepted 111ms 10.52 MiB
#8 Accepted 117ms 7.406 MiB
#9 Accepted 107ms 7.539 MiB
#10 Accepted 108ms 8.777 MiB
#11 Accepted 110ms 10.113 MiB
#12 Accepted 114ms 10.398 MiB
#13 Accepted 112ms 10.48 MiB
#14 Accepted 114ms 10.125 MiB
#15 Accepted 107ms 10.477 MiB
#16 Accepted 118ms 9.105 MiB
#17 Accepted 110ms 7.543 MiB
#18 Accepted 117ms 7.543 MiB
#19 Accepted 110ms 7.555 MiB
#20 Accepted 110ms 8.781 MiB
#21 Accepted 4ms 2.57 MiB
#22 Accepted 3ms 516.0 KiB
#23 Accepted 94ms 16.613 MiB
#24 Accepted 84ms 18.223 MiB
#25 Accepted 4ms 2.566 MiB
#26 Accepted 28ms 2.262 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=3e5+10,MOD=1000000007;
typedef long long ll;
vector<int>edge[M];
int level[M];
int lv[M];
int ans=1;
int target_distane=0;// using method re-rooting dp
int dp[M][2];
void dfs1(int x,int p){
    for(auto u:edge[x]){
        if(u!=p){
            int cur_level=dp[x][0];
            if(dp[x][0]==level[u]+1)cur_level=dp[x][1];
            cur_level=max(cur_level,lv[x]);
            int total=cur_level+level[u];
            total/=2;
            lv[u]=cur_level+1;// root to down length
            if(total>=target_distane){
                if(total==target_distane){
                    if(cur_level==level[u])ans=max(ans,max(u,x));
                    else{
                        int dif=cur_level-level[u];
                        if(dif==1)ans=max(ans,x);
                        if(dif==-1)ans=max(ans,u);
                    }
                }
                else{
                    if(cur_level==level[u]){
                        ans=max(u,x);
                        target_distane=total;
                    }
                    else{
                        int dif=cur_level-level[u];
                        if(dif==1)ans=x,target_distane=total;
                        if(dif==-1)ans=u,target_distane=total;
                    }
            }
        }

            dfs1(u,x);
        }
    }
}
void dfs(int x,int p){
    dp[x][1]=dp[x][0]=1;
    for(auto u:edge[x]){
        if(u!=p){
            dfs(u,x);
        }
    }
    level[p]=max(level[p],level[x]+1);
    if(p!=0){
        int cur=level[x]+1;
        vector<int>v;
        v.push_back(dp[p][0]);
        v.push_back(dp[p][1]);
        v.push_back(cur);
        sort(v.rbegin(),v.rend());
        dp[p][0]=v[0];// first max
        dp[p][1]=v[1];//second max
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
     int n;
     cin>>n;
     for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        edge[u].push_back(v);
        edge[v].push_back(u);
     }
     for(int i=1;i<=n;i++){
        level[i]=1;
     }
     dfs(n,0);// lower level and upper level length
     dfs1(n,0);
     cout<<target_distane<<" "<<ans<<"\n";
     
     
    }
    
   
   return 0;
    
}
 

Information

Submit By
Type
Submission
Problem
P1069 Vaccination
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-11 18:06:07
Judged At
2024-11-11 03:27:04
Judged By
Score
100
Total Time
120ms
Peak Memory
18.223 MiB