This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <dreaming.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for (ll i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (ll i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (ll i=(a); i >= b; i--)
#define MN 100005
vpii e[MN];
int vis[MN];
pii far(int cn, int p){ //{dist, node}
vis[cn] = true;
pii ans = {0, cn};
for(auto nn : e[cn]) if(nn.f != p){
pii res = far(nn.f, cn);
ans = max(ans, {res.f+nn.s, res.s});
}
return ans;
}
int d[2][MN];
int cmn;
void gda(int t, int cn, int p){
if(t == 1){
cmn = min(cmn, max(d[0][cn], d[1][cn]));
}
for(auto nn : e[cn]) if(nn.f != p){
d[t][nn.f] = d[t][cn]+nn.s;
gda(t, nn.f, cn);
}
}
int ans;
int diam(int x){
pii res1 = far(x, -1);
pii res2 = far(res1.s, -1);
//return {res2.f, {res1.s, res2.s}};
ans = max(ans, res2.f);
cmn = INF;
gda(0, res1.s, -1);
gda(1, res2.s, -1);
return cmn;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
int n = N; int m = M; int l = L;
F0R(i, m){
e[A[i]].pb({B[i], T[i]});
e[B[i]].pb({A[i], T[i]});
}
vi hmm;
F0R(i, n){
if(!vis[i]){
hmm.pb(diam(i));
}
}
sort(hmm.begin(), hmm.end(), greater<int>() );
if(hmm.size() == 1){
return max(ans, hmm[0]);
} else if(hmm.size() == 2){
return max(ans, hmm[0]+hmm[1]+l);
} else{
return max(ans, max(hmm[0]+hmm[1]+l, hmm[1]+hmm[2]+l+l));
}
}
/*int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
/*int N, M, L;
cin >> N >> M >> L;
int A[M], B[M], T[M];
F0R(i, M) cin >> A[i] >> B[i] >> T[i];
cout << travelTime(N, M, L, A, B, T) << "\n";
int N=20; int M=N-2, L=rand()%1000;
vi hmm;
F0R(i, N){
hmm.pb(i);
}
F0R(_, 10) random_shuffle(hmm.begin(), hmm.end());
int spl = 5+rand()%10;
vpii hmm2;
F0R(i, N-1){
if(i != spl){
hmm2.pb({hmm[i], hmm[i+1]});
}
}
F0R(_, 10) random_shuffle(hmm2.begin(), hmm2.end());
int A[M], B[M], T[M];
F0R(i, M){
if(rand()%2){
A[i] = hmm2[i].f; B[i] = hmm2[i].s;
} else{
A[i] = hmm2[i].s; B[i] = hmm2[i].f;
}
T[i] = rand()%1000;
}
cout << N << " " << M << " " << L << "\n";
F0R(i, M){
cout << A[i] << " " << B[i] << " " << T[i] << "\n";
}
cout << "\n";
cout << travelTime(N, M, L, A, B, T) << "\n";
return 0;
}*/
Compilation message (stderr)
dreaming.cpp:83:5: warning: "/*" within comment [-Wcomment]
/*int N, M, 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... |