#include<bits/stdc++.h>
#include "dreaming.h"
using namespace std;
vector < pair < int , int > > adj[100005];
vector < int > v;
vector < int > q;
int mx, mx_cnt;
int d[100004], used[100004];
void Dia(int node, int parent, int dist) {
d[node] = dist;
q.push_back(node);
if ( d[node] > mx_cnt) {
mx_cnt = d[node];
mx = node;
}
used[node] = 1;
for ( pair < int, int >& A : adj[node]) {
int child = A.first;
if ( child == parent) continue;
Dia(child, node, dist + A.second);
}
}
int Dfs(int node, int parent, int fn) {
int baiga = 0;
if ( node == fn) return 1;
for ( pair < int, int >& A : adj[node]) {
int child = A.first;
if ( child == parent) continue;
v.push_back(child);
if(Dfs(child, node, fn)) {
baiga = 1;
}
else {
v.pop_back();
}
}
return baiga;
}
vector < int > Ans;
void Go(int x) {
int y;
mx = x;
mx_cnt = 0;
Dia(x, x, 0);
for ( int X : q) d[X] = 0;
q.clear();
x = mx;
mx_cnt = 0;
Dia(mx, mx, 0);
y = mx;
int dist = mx_cnt;
v.clear();
v.push_back(x);
Dfs(x, x, y);
int s = 1e9;
for ( int X : v) {
s = min(s, max(d[X], dist -d[X]));
}
Ans.push_back(s);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i ++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
for (int i = 0; i < N; i ++) {
if (!used[i]) {
Go(i);
}
}
sort(Ans.rbegin(), Ans.rend());
if ( Ans.size() == 2) return Ans[0] + Ans[1] + L;
return max(Ans[0] + Ans[1] , Ans[1] + Ans[2] + L) + L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
15468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
15468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
7176 KB |
Output is correct |
2 |
Correct |
15 ms |
7004 KB |
Output is correct |
3 |
Correct |
14 ms |
7004 KB |
Output is correct |
4 |
Correct |
14 ms |
7020 KB |
Output is correct |
5 |
Correct |
18 ms |
7004 KB |
Output is correct |
6 |
Correct |
16 ms |
7388 KB |
Output is correct |
7 |
Correct |
18 ms |
7216 KB |
Output is correct |
8 |
Correct |
14 ms |
6880 KB |
Output is correct |
9 |
Correct |
14 ms |
7004 KB |
Output is correct |
10 |
Correct |
14 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
5080 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5080 KB |
Output is correct |
16 |
Correct |
6 ms |
5080 KB |
Output is correct |
17 |
Correct |
4 ms |
5080 KB |
Output is correct |
18 |
Correct |
4 ms |
5084 KB |
Output is correct |
19 |
Correct |
4 ms |
5080 KB |
Output is correct |
20 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
15468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |