#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<ll, ll>> adjList[MAXN];
bool vis[MAXN];
pair<ll, ll> best[MAXN][2];
vector<ll> nodes;
ll mx = 1e12, idx = 0;
ll 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 |
66 ms |
15724 KB |
Output is correct |
2 |
Correct |
71 ms |
15468 KB |
Output is correct |
3 |
Correct |
41 ms |
13036 KB |
Output is correct |
4 |
Correct |
9 ms |
4716 KB |
Output is correct |
5 |
Correct |
9 ms |
4076 KB |
Output is correct |
6 |
Correct |
16 ms |
5868 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
15724 KB |
Output is correct |
2 |
Correct |
71 ms |
15468 KB |
Output is correct |
3 |
Correct |
41 ms |
13036 KB |
Output is correct |
4 |
Correct |
9 ms |
4716 KB |
Output is correct |
5 |
Correct |
9 ms |
4076 KB |
Output is correct |
6 |
Correct |
16 ms |
5868 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
15724 KB |
Output is correct |
2 |
Correct |
71 ms |
15468 KB |
Output is correct |
3 |
Correct |
41 ms |
13036 KB |
Output is correct |
4 |
Correct |
9 ms |
4716 KB |
Output is correct |
5 |
Correct |
9 ms |
4076 KB |
Output is correct |
6 |
Correct |
16 ms |
5868 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
8688 KB |
Output is correct |
2 |
Correct |
32 ms |
8812 KB |
Output is correct |
3 |
Correct |
29 ms |
8688 KB |
Output is correct |
4 |
Correct |
38 ms |
8684 KB |
Output is correct |
5 |
Correct |
30 ms |
8684 KB |
Output is correct |
6 |
Correct |
32 ms |
9452 KB |
Output is correct |
7 |
Correct |
32 ms |
8944 KB |
Output is correct |
8 |
Correct |
31 ms |
8560 KB |
Output is correct |
9 |
Correct |
29 ms |
8552 KB |
Output is correct |
10 |
Correct |
30 ms |
8808 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
8 ms |
5476 KB |
Output is correct |
13 |
Correct |
8 ms |
6244 KB |
Output is correct |
14 |
Correct |
8 ms |
5220 KB |
Output is correct |
15 |
Correct |
8 ms |
5604 KB |
Output is correct |
16 |
Correct |
7 ms |
5092 KB |
Output is correct |
17 |
Correct |
7 ms |
4196 KB |
Output is correct |
18 |
Correct |
10 ms |
6372 KB |
Output is correct |
19 |
Correct |
7 ms |
5092 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
15724 KB |
Output is correct |
2 |
Correct |
71 ms |
15468 KB |
Output is correct |
3 |
Correct |
41 ms |
13036 KB |
Output is correct |
4 |
Correct |
9 ms |
4716 KB |
Output is correct |
5 |
Correct |
9 ms |
4076 KB |
Output is correct |
6 |
Correct |
16 ms |
5868 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
15724 KB |
Output is correct |
2 |
Correct |
71 ms |
15468 KB |
Output is correct |
3 |
Correct |
41 ms |
13036 KB |
Output is correct |
4 |
Correct |
9 ms |
4716 KB |
Output is correct |
5 |
Correct |
9 ms |
4076 KB |
Output is correct |
6 |
Correct |
16 ms |
5868 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |