/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 564.0 KiB
#2 Wrong Answer 59ms 6.641 MiB
#3 Wrong Answer 61ms 6.77 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int MX = 1e5 + 5 ;
vector<int> Edge[MX] ;
int cnt = 0 , days = 0 , n , u , v , farthestNode = 0 , lev[MX] , level[MX] , mnDays;
void dfs(int id , int par){
  for(auto child : Edge[id]){
    if(child != par){
        lev[child] = lev[id] + 1 ;
        if(days < lev[child]){
            days = lev[child] ;
            farthestNode = child ;
        }
        dfs(child , id) ;
    }
  }
}
void dfs2(int id , int par){
    for(auto child : Edge[id]){
        if(child != par){
            dfs2(child , id) ;
           level[id] = max(level[id] , level[child] + 1) ;
        }
    }

}

void solve()
{
    cin >> n ;
    for(int i = 1 ; i < n ; i++){
        cin >> u >> v ;
        Edge[u].pb(v) ;
        Edge[v].pb(u) ;

    }
    dfs(1 , -1) ;
    for(int i = 1 ; i <= n ; i++) lev[i] = 0;
    dfs(farthestNode , -1) ;
    dfs2(farthestNode , -1) ;
    mnDays = (days + 1) >> 1 ;
    vector < int > node ;
   // for(int i = 1 ; i <= n ; i++) cout << level[i] << " " ; cout << endl ;
    for(int i = n ; i >= 1; i--){
        if(days % 2 == 0){
            if(lev[i] == mnDays && Edge[i].size() > 1){
                //cout << i << endl ;
                if(max(lev[i] , level[i]) == mnDays) {
                    cout << mnDays << " " << i << endl ;
                    break ;
                }
            }
        }
        else {
            if((lev[i] == mnDays && Edge[i].size() > 1) || (lev[i] == mnDays - 1 && Edge[i].size() > 1)){
                if(max(lev[i] , level[i]) == mnDays){
                    cout << mnDays << " " << i << endl ;
                    break ;
                }

            }
        }
    }
//    for(int i = 0 ; i < node.size() ; i++){
//        lev[node[i]] = 0 ;
//        days = 0 ;
//        dfs(node[i] , -1) ;
//        if(days == mnDays){
//            cout << mnDays << " " << node[i] << endl ;
//            break ;
//        }
//    }

}
int main()
{
     __Heart__
     int t ; t = 1 ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1069 Vaccination
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-12 04:15:22
Judged At
2024-11-11 03:26:56
Judged By
Score
5
Total Time
61ms
Peak Memory
6.77 MiB