/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 576.0 KiB
#2 Accepted 76ms 7.48 MiB
#3 Accepted 79ms 7.543 MiB
#4 Accepted 75ms 7.363 MiB
#5 Accepted 74ms 7.461 MiB
#6 Accepted 83ms 7.547 MiB
#7 Accepted 74ms 7.406 MiB
#8 Accepted 72ms 7.406 MiB
#9 Accepted 73ms 7.359 MiB
#10 Accepted 75ms 7.383 MiB
#11 Accepted 75ms 7.395 MiB
#12 Accepted 74ms 7.41 MiB
#13 Accepted 74ms 7.414 MiB
#14 Accepted 75ms 7.328 MiB
#15 Accepted 80ms 7.562 MiB
#16 Accepted 77ms 7.406 MiB
#17 Accepted 78ms 7.547 MiB
#18 Accepted 74ms 7.543 MiB
#19 Accepted 75ms 7.559 MiB
#20 Accepted 78ms 7.406 MiB
#21 Accepted 5ms 532.0 KiB
#22 Accepted 5ms 532.0 KiB
#23 Accepted 53ms 16.398 MiB
#24 Accepted 54ms 16.473 MiB
#25 Accepted 5ms 580.0 KiB
#26 Accepted 20ms 2.078 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(1,0);// lower level and upper level length
     dfs1(1,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-12 16:57:45
Judged At
2024-11-11 03:26:50
Judged By
Score
100
Total Time
83ms
Peak Memory
16.473 MiB