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];
pii far(int cn, int p){ //{dist, node}
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 dsu[MN];
int sz[MN];
int ans[MN];
int fd(int x){
if(dsu[x] == x) return x;
dsu[x] = fd(dsu[x]); return dsu[x];
}
void jn(int x, int y){
x = fd(x); y = fd(y);
if(x == y) return;
if(sz[x] < sz[y]) swap(x, y);
dsu[y] = x;
sz[x] += sz[y];
ans[x] = min(ans[x], ans[y]);
}
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]});
}
F0R(i, n){
dsu[i] = i; sz[i] = i;
pii res = far(i, -1);
//cout << "far from " << i << " " << res.f << "\n";
ans[i] = res.f;
}
F0R(i, m){
jn(A[i], B[i]);
}
vi hmm;
F0R(i, n){
if(dsu[i] == i){
//cout << ans[i] << " ";
hmm.pb(ans[i]);
}
}
//cout << "\n";
sort(hmm.begin(), hmm.end(), greater<int>() );
if(hmm.size() == 1){
return hmm[0];
} else if(hmm.size() == 2){
return hmm[0]+hmm[1]+l;
} else{
return 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";
return 0;
}*/
# | 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... |