/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 3ms 2.676 MiB
#2 Wrong Answer 60ms 7.449 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=1e5+10,MOD=1000000007;
typedef long long ll;
vector<int>e[M];
int level[M];
int lv[M];
int ans=1;
int target_distane=0;// using method re-rooting dp
int dp[M][2];
int vis[M];
void dfs1(int x,int p){
    for(int u:e[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;
	vis[x]=1;
    for(int u:e[x]){
        if(vis[u]==0){
			vis[u]=1;
            dfs(u,x);
        }
    }
    if(p!=-1)
    level[p]=max(level[p],level[x]+1);
    if(p!=-1){
        int cur=level[x]+1;
        vector<int>v;
        v.push_back((int)dp[p][0]);
        v.push_back((int)dp[p][1]);
       // v.push_back((int)cur);
        sort(v.rbegin(),v.rend());
        //dp[p][0]=1;// first max
        //dp[p][1]=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;
        e[u].push_back(v);
        e[v].push_back(u);
     }
     for(int i=1;i<=n;i++){
        level[i]=1;
		 dp[i][0]=0;
		 dp[i][1]=0;
     }
     dfs(1,-1);// 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++11 (G++ 13.2.0)
Submit At
2024-07-11 17:47:50
Judged At
2024-11-11 03:27:10
Judged By
Score
0
Total Time
60ms
Peak Memory
7.449 MiB