이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define fi first
#define se second
#define INF (int)(1e9)
#define ll long long
vector<vector<pair<ll,ll> > > vec;
bool vis[100005];
ll maxi=0,a,idx=0;
vector<pair<ll,ll> > tree_d;
pair<ll,ll> p[100005],center[100005];
vector<ll> nodes;
void dfs(ll node,ll d){
nodes.push_back(node);
if(d>maxi)
a=node,maxi=d;
vis[node]=1;
for(auto child:vec[node])
if(child.fi!=p[node].fi){
p[child.fi]={node,child.se};
dfs(child.fi,d+child.se);
}
}
pair<ll,ll> findCenter(ll node){
maxi=0;
dfs(node,0);
for(auto i:nodes)
p[i]={-1,-1};
nodes.clear();
ll B=a,sum=0,mini=INF,c=node,sh=INF;
maxi=0;
if(B!=-1)
dfs(B,0);
nodes.clear();
for(;a!=-1;sum+=p[a].se,a=p[a].fi){
if(abs(maxi/2-sum)<mini)
mini=abs(maxi/2-sum),c=a,sh=sum;
if(maxi%2&&abs((maxi/2)+1-sum)<mini)
mini=abs((maxi/2)+1-sum),c=a,sh=sum;
}
tree_d.push_back({maxi,idx});
return {c,max(maxi-sh,0LL)};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vec.resize(N);
memset(p,-1,sizeof p);
for (ll i = 0; i < M; ++i)
{
vec[A[i]].push_back({B[i],T[i]});
vec[B[i]].push_back({A[i],T[i]});
}
for (ll i = 0; i < N; ++i)
if(!vis[i])
center[idx++]=findCenter(i);
sort(tree_d.begin(),tree_d.end(),greater<pair<ll,ll> >());
for (ll i = 1; i < idx; ++i)
{
vec[center[tree_d[i].se].fi].push_back({center[tree_d[0].se].fi,L});
vec[center[tree_d[0].se].fi].push_back({center[tree_d[i].se].fi,L});
}
maxi=0;
memset(p,-1,sizeof p);
dfs(0,0);
nodes.clear();
ll b=a;
if(b==-1)return maxi;
memset(p,-1,sizeof p);
maxi=0;
dfs(b,0);
return maxi;
}
| # | 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... |