제출 #1242281

#제출 시각아이디문제언어결과실행 시간메모리
1242281AMel0n꿈 (IOI13_dreaming)C++20
32 / 100
34 ms14916 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i,N) for(ll i = 0; i < N; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second

#include "dreaming.h"

ll N, M, L;
vector<vector<pair<ll,ll>>> adj;
vector<signed char> vis;

pair<ll,ll> mxDist(ll n, ll p, ll d) {
    vis[n] = 1;
    pair<ll,ll> ans = {d, n}; // furthest dist, furthest node
    for(pair<ll,ll> c: adj[n]) {
        if (c.F == p) continue;
        pair<ll,ll> ret = mxDist(c.F, n, d + c.S);
        if (ret.F > ans.F) ans = ret;
    }
    return ans;
}

pair<ll,ll> calc(ll n, ll p, ll d){
    // let optimal node = node in a tree that best splits the diameter in half
    // let radii = maximum distance from any node in a tree to optimal node 
    pair<ll,ll> ans = {d, 1e18}; // diameter, radii
    for(pair<ll,ll> c: adj[n]) {
        if (c.F == p) continue;
        pair<ll,ll> ret = calc(c.F, n, d + c.S);
        if (ret.F > ans.F) ans = ret;
        if (ret.F == ans.F) ans.S = min(ans.S, ret.S);
    }
    // if we're still in the second (deeper) half of the tree diameter's path
    if (d*2 >= ans.F) ans.S = d;
    return ans;
}


int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    ::N = N, ::M = M, ::L = L;
    adj.resize(N);
    vis.resize(N);
    FOR(i, M) {
        adj[A[i]].push_back({B[i], T[i]});
        adj[B[i]].push_back({A[i], T[i]});
    }

    ll res = 0;
    vector<ll> radii; 
    FOR(i, N) {
        if (vis[i]) continue;
        auto [diam, rad] = calc(mxDist(i, -1, 0).S, -1, 0);
        res = max(res, diam);
        radii.push_back(rad);
    }
    sort(all(radii));
    reverse(all(radii));
    // connect tree with the largest radii to all other trees (via respective optimal nodes)
    if ((ll)radii.size() >= 2) res = max(radii[0] + radii[1] + L, res);
    if ((ll)radii.size() >= 3) res = max(radii[1] + radii[2] + L*2, res);
    return (int)res;
}
#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...