#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll MOD = 1000000007;
const int MAXN = 100010;
vector<pii> adj[MAXN];
int sub[MAXN], diam, m1[MAXN], m2[MAXN], og[MAXN];
bool vis[MAXN];
void dfs(int node){
vis[node] = 1;
for(auto x: adj[node]){
if(vis[x.fi])continue;
dfs(x.fi);
sub[node] = max(sub[x.fi] + x.se,sub[node]);
}
}
int reroot(int node){
vis[node] = 1;
for(auto x: adj[node]){
if(sub[x.fi]+x.se>=m1[node]){
m2[node] = m1[node];
m1[node] = sub[x.fi]+x.se;
}else if(sub[x.fi]+x.se > m2[node]){
m2[node] = sub[x.fi]+x.se;
}
}
diam = max(diam, m1[node] + m2[node]);
int ans = m1[node]; og[node] = sub[node];
for(auto x: adj[node]){
if(vis[x.fi])continue;
if(m1[node] == sub[x.fi]+x.se)sub[node] = m2[node];
else sub[node] = m1[node];
ans = min(ans,reroot(x.fi));
sub[node] = og[node];
}
// cout << "node "<<node <<" m1 "<<m1[node]<<"\n";
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for(int i = 0; i < M; i++){
adj[A[i]].pb({B[i],T[i]});
adj[B[i]].pb({A[i],T[i]});
}
int k,m=0,mm=0, mmm = 0;
for(int i = 0; i < N; i++)if(!vis[i])dfs(i);
for(int i = 0; i < N; i++)vis[i] = 0;
for(int i = 0; i < N; i++)if(!vis[i]){
k = reroot(i);
// cout << "i "<<i <<" k "<<k<<"\n";
if(k >= m){
mmm = mm; mm = m; m = k;
}else if(k >= mm){
mmm = mm; mm = k;
}else if(k > mmm){
mmm = k;
}
}
return max({2*L+mmm+mm, L + m+mm, diam});
}
/*
int main(){
int a,b,c;
cin >> a >> b >> c;
int k[b],j[b],l[b];
for(int i = 0; i < b; i++){
cin >> k[i] >> j[i] >> l[i];
}
cout << travelTime(a,b,c,k,j,l)<<"\n";
}
*/
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
11768 KB |
Output is correct |
2 |
Correct |
55 ms |
11640 KB |
Output is correct |
3 |
Correct |
39 ms |
9680 KB |
Output is correct |
4 |
Correct |
10 ms |
3968 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
15 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
11768 KB |
Output is correct |
2 |
Correct |
55 ms |
11640 KB |
Output is correct |
3 |
Correct |
39 ms |
9680 KB |
Output is correct |
4 |
Correct |
10 ms |
3968 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
15 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
11768 KB |
Output is correct |
2 |
Correct |
55 ms |
11640 KB |
Output is correct |
3 |
Correct |
39 ms |
9680 KB |
Output is correct |
4 |
Correct |
10 ms |
3968 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
15 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
7040 KB |
Output is correct |
2 |
Correct |
30 ms |
7040 KB |
Output is correct |
3 |
Correct |
33 ms |
6944 KB |
Output is correct |
4 |
Correct |
27 ms |
7072 KB |
Output is correct |
5 |
Correct |
28 ms |
7040 KB |
Output is correct |
6 |
Correct |
29 ms |
7288 KB |
Output is correct |
7 |
Correct |
33 ms |
7288 KB |
Output is correct |
8 |
Correct |
28 ms |
6904 KB |
Output is correct |
9 |
Correct |
28 ms |
6912 KB |
Output is correct |
10 |
Correct |
27 ms |
7168 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
4352 KB |
Output is correct |
13 |
Correct |
4 ms |
4352 KB |
Output is correct |
14 |
Correct |
4 ms |
4352 KB |
Output is correct |
15 |
Correct |
4 ms |
4352 KB |
Output is correct |
16 |
Correct |
4 ms |
4224 KB |
Output is correct |
17 |
Correct |
4 ms |
3712 KB |
Output is correct |
18 |
Correct |
4 ms |
4352 KB |
Output is correct |
19 |
Correct |
4 ms |
4352 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
11768 KB |
Output is correct |
2 |
Correct |
55 ms |
11640 KB |
Output is correct |
3 |
Correct |
39 ms |
9680 KB |
Output is correct |
4 |
Correct |
10 ms |
3968 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
15 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
11768 KB |
Output is correct |
2 |
Correct |
55 ms |
11640 KB |
Output is correct |
3 |
Correct |
39 ms |
9680 KB |
Output is correct |
4 |
Correct |
10 ms |
3968 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
15 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |