이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<ll,ll>;
const ll MX = 100000;
vector<ii> AL[MX];
bool visited[MX];
map<ii,ll> maxDepthMem;
// returns the maximum depth
ll maxDepth(ll v, ll p)
{
if(maxDepthMem.count({v,p}))
return maxDepthMem[{v,p}];
ll res = 0;
for(auto& [u,w]: AL[v])
{
if(u==p) continue;
res = max(res, maxDepth(u,v)+w);
}
maxDepthMem[{v,p}] = res;
return res;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(ll i=0;i<M;i++)
{
ll a = A[i], b=B[i], w=T[i];
AL[a].push_back({b,w});
AL[b].push_back({a,w});
}
vector<ll> ecc; // eccentricity vector
for(ll i=0;i<N;i++)
{
if(visited[i]) continue;
vector<ll> nodes;
queue<ll> q;
q.push(i);
visited[i]=1;
nodes.push_back(i);
while(!q.empty())
{
ll v = q.front(); q.pop();
for(auto& [u,w]: AL[v])
{
if(visited[u]) continue;
q.push(u);
visited[u]=1;
nodes.push_back(u);
}
}
//cout<<"nodes: "; for(ll v: nodes) cout<<v<<" "; cout<<"\n";
ll mnMxDepth = 1e9;
for(ll v: nodes)
{
ll mxDepth = 0;
for(auto& [u,w]: AL[v])
mxDepth=max(mxDepth, maxDepth(u,v)+w);
mnMxDepth = min(mnMxDepth, mxDepth);
}
ecc.push_back(mnMxDepth);
}
//cout<<"ecc: "; for(ll x: ecc) cout<<x<<" "; cout<<"\n";
sort(ecc.begin(),ecc.end(),greater<ll>());
ll res = ecc[0];
if(ecc.size()>=2) res=max(res,ecc[0]+ecc[1]+L);
if(ecc.size()>=3) res=max(res,ecc[1]+ecc[2]+2*L);
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |