#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
#include "dreaming.h"
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 1e5 + 5;
int n, m, l;
vector<pair<int, int>> adjList[MAXN];
bool vis[MAXN];
pair<int, int> best[MAXN][2];
vector<int> nodes;
int mx = 1e9, idx = 0;
int ans = 0;
void dfs(int src, int p) {
vis[src] = true;
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
dfs(i.first, src);
if(best[i.first][0].first + i.second > best[src][0].first){
best[src][1] = best[src][0];
best[src][0] = best[i.first][0];
best[src][0].first += i.second;
best[src][0].second = i.first;
}
else if (best[i.first][0].first + i.second > best[src][1].first) {
best[src][1] = best[i.first][0];
best[src][1].first += i.second;
best[src][1].second = i.first;
}
}
}
void cnt(int src, int p, int dist) {
if (p != -1) {
if (best[p][0].second != src) {
if (best[p][0].first + dist > best[src][0].first) {
best[src][1] = best[src][0];
best[src][0] = best[p][0];
best[src][0].first += dist;
best[src][0].second = p;
}
else if (best[p][0].first + dist > best[src][1].first) {
best[src][1] = best[p][0];
best[src][1].first += dist;
best[src][1].second = p;
}
}
else {
if (best[p][1].first + dist > best[src][0].first) {
best[src][1] = best[src][0];
best[src][0] = best[p][1];
best[src][0].first += dist;
best[src][0].second = p;
}
else if (best[p][1].first + dist > best[src][1].first) {
best[src][1] = best[p][1];
best[src][1].first += dist;
best[src][1].second = p;
}
}
}
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
cnt(i.first, src, i.second);
}
}
void add(int src, int p) {
mx = min(mx, best[src][0].first);
ans = max(ans, best[src][0].first + best[src][1].first);
if (mx == best[src][0].first) {
idx = src;
}
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
add(i.first, src);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N; m = M; l = L;
for (int i = 0; i < m; i++) {
adjList[A[i]].push_back({ B[i], T[i] });
adjList[B[i]].push_back({ A[i], T[i] });
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
mx = 1e9;
idx = 0;
dfs(i, -1);
cnt(i, -1, 0);
add(i, -1);
nodes.push_back(mx);
}
}
//for (int i = 0; i < n; i++) {
// cout << i << " " << best[i]->first << endl;
//}
//cout << ans << endl;
sort(nodes.begin(), nodes.end());
reverse(nodes.begin(), nodes.end());
ans = max(ans, nodes[0] + l + nodes[1]);
ans = max(ans, nodes[1] + l + l + nodes[2]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13292 KB |
Output is correct |
2 |
Correct |
57 ms |
13036 KB |
Output is correct |
3 |
Correct |
37 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
7 ms |
3564 KB |
Output is correct |
6 |
Correct |
14 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13292 KB |
Output is correct |
2 |
Correct |
57 ms |
13036 KB |
Output is correct |
3 |
Correct |
37 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
7 ms |
3564 KB |
Output is correct |
6 |
Correct |
14 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13292 KB |
Output is correct |
2 |
Correct |
57 ms |
13036 KB |
Output is correct |
3 |
Correct |
37 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
7 ms |
3564 KB |
Output is correct |
6 |
Correct |
14 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
7404 KB |
Output is correct |
2 |
Correct |
27 ms |
7404 KB |
Output is correct |
3 |
Correct |
33 ms |
7404 KB |
Output is correct |
4 |
Correct |
28 ms |
7404 KB |
Output is correct |
5 |
Correct |
27 ms |
7404 KB |
Output is correct |
6 |
Correct |
28 ms |
7920 KB |
Output is correct |
7 |
Correct |
28 ms |
7536 KB |
Output is correct |
8 |
Correct |
27 ms |
7280 KB |
Output is correct |
9 |
Correct |
26 ms |
7276 KB |
Output is correct |
10 |
Correct |
27 ms |
7532 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
7 ms |
4584 KB |
Output is correct |
13 |
Correct |
7 ms |
4968 KB |
Output is correct |
14 |
Correct |
7 ms |
4456 KB |
Output is correct |
15 |
Correct |
7 ms |
4584 KB |
Output is correct |
16 |
Correct |
6 ms |
4328 KB |
Output is correct |
17 |
Correct |
6 ms |
3688 KB |
Output is correct |
18 |
Correct |
7 ms |
4968 KB |
Output is correct |
19 |
Correct |
7 ms |
4328 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 |
59 ms |
13292 KB |
Output is correct |
2 |
Correct |
57 ms |
13036 KB |
Output is correct |
3 |
Correct |
37 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
7 ms |
3564 KB |
Output is correct |
6 |
Correct |
14 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
13292 KB |
Output is correct |
2 |
Correct |
57 ms |
13036 KB |
Output is correct |
3 |
Correct |
37 ms |
11264 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
7 ms |
3564 KB |
Output is correct |
6 |
Correct |
14 ms |
5100 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |