이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define enter cout<<'\n';
#define INF 99999999999999999
#define MOD 1000000007
#define all(x) x.begin(),x.end()
vector<vector<pair<ll,ll>>>g;
vector<bool>vis;
vector<ll>dist,par;
ll n,m;
void dfs1(int start,int pre,ll &maxi)
{
for(auto x:g[start])
if(x.F!=pre)
{
par[x.F]=start;
vis[x.F]=1;
dist[x.F]= dist[start] + x.S;
if(dist[maxi]<dist[x.F])
maxi=x.F;
dfs1(x.F, start,maxi);
}
}
void dfs2(int start,int pre,ll &ans,ll diff,ll curr,ll count)
{
for(auto x:g[start])
if(x.F!=pre)
dfs2(x.F,start,ans,diff,curr + x.S,count);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N;
m=M;
g.assign(n,vector<pair<ll,ll>>());
vis.assign(n,0);
dist.assign(n,0);
par.assign(n,0);
for(int i=0;i<m;i++)
{
g[A[i]].pb({B[i],T[i]});
g[B[i]].pb({A[i],T[i]});
}
vector<ll>ans(3,0);
ll idx=0;
for(int i=0;i<N;i++)
if(!vis[i])
{
vis[i]=1;
ll p1=i,p2;
dfs1(i,i,p1);
dist[p1]=0;
p2=p1;
dfs1(p1,p1,p2);
ll curr=0;
ll diff=INF;
ll count=dist[p2];
ans[2]=max(ans[2],dist[p2]);
while(p2!=p1)
{
if(diff > abs((count-curr)-curr))
{
ans[idx]=max(curr,count-curr);
diff= abs((count-curr)-curr);
}
curr+=dist[p2]-dist[par[p2]];
p2=par[p2];
}
idx++;
}
return max(ans[2],ans[0] + ans[1] + L);
}
# | 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... |