제출 #790394

#제출 시각아이디문제언어결과실행 시간메모리
790394Ronin13꿈 (IOI13_dreaming)C++17
100 / 100
88 ms22344 KiB
#include "dreaming.h"
#include <bits/stdc++.h>
#define ll long long 
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;

const int nmax = 200001;
vector <vector <pair<ll, ll> > > g(nmax);
int dp[nmax][3];
vector <bool> used(nmax);

vector <int> cmp;

void dfs(int v, int e = -1){
    used[v] = true;
    cmp.pb(v);
    for(auto x : g[v]){
        int to = x.f;
        int len = x.s;
        if(used[to]) continue;
        dfs(to, v);
        if(dp[v][0] < dp[to][0] + len){
            swap(dp[v][0], dp[v][1]);
            dp[v][2] = to;
            dp[v][0] = dp[to][0] + len;
        }
        else dp[v][1] = max(dp[v][1], dp[to][0] + len);
    }
}

void reroot(int v, int e){
    for(auto x : g[v]){
        int to = x.f;
        int len = x.s;
        if(to == e) continue;
        int mx = 0;
        if(dp[v][2] == to){
            mx = dp[v][1] + len;
        }
        else mx = dp[v][0] + len;
        if(dp[to][0] < mx)
        swap(dp[to][0], dp[to][1]), dp[to][2] = v, dp[to][0] = mx;
        else dp[to][1] = max(dp[to][1], mx);
        reroot(to, v);
    }

}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    for(int i = 0; i < M; i++){
        int u = A[i], v = B[i], w = T[i];
        g[u].pb({v, w});
        g[v].pb({u, w});
    }
    int ans = 0;
    multiset <ll> st;
    for(int i = 0; i < N; i++){
        if(used[i]) continue;
        dfs(i);
        reroot(i, i);
        int mn = 1e9;
        for(int to : cmp){
            ans = max(ans, dp[to][0]);
            mn = min(mn, dp[to][0]);
        }
        st.insert(mn);
        cmp.clear();
    }
    int a, b, c;
    a = b = c = -1e9;
    if(st.size() >= 2){
        a = *st.rbegin();
        st.erase(st.find(a));
        b = *st.rbegin();
        st.erase(st.find(b));
        ans = max(ans, a + b + L);
    }
    if(st.size() >= 1){
        c = *st.rbegin();
        ans = max(ans, b + c + 2 * L);
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...