/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 532.0 KiB
#2 Time Exceeded ≥2071ms ≥6.207 MiB
#3 Time Exceeded ≥2070ms ≥6.383 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] , mnDays;
vector < int > Ans ;
vector<bool> removed;
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 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) ;
    mnDays = (days + 1) >> 1 ;
    vector < int > node ;
    for(int i = 1 ; i <= n; i++){
        if(lev[i] == mnDays || lev[i] == mnDays + 1){
            node.pb(i) ;
        }
    }
    for(int i = node.size() - 1 ; i >= 0 ; 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 01:54:12
Judged At
2024-11-11 03:27:00
Judged By
Score
5
Total Time
≥2071ms
Peak Memory
≥6.383 MiB