#include "bits/stdc++.h"
#include "dreaming.h"
using namespace std;
const int maxn = 100001;
vector<pair<int,int>> adj[maxn];
/*
answer can be:
1- biggest simple path which exists
2- 2 biggest path from roots + L
3- 2nd and 3rd biggest paths from roots + 2L
*/
int A1 = 0, A2 = 0, A3 = 0;
int id = 1;
int cmp[maxn];
int dist[maxn];
int distu[maxn];
void dfs (int cur, int par) {
set<int> mx;
for (auto [j, w] : adj[cur]) {
if (!cmp[j]) {
cmp[j] = id;
dfs(j, cur);
dist[cur] = max(dist[cur], dist[j] + w);
mx.insert(dist[j] + w);
if (mx.size() > 2) mx.erase(mx.begin());
if (mx.size() == 2) {
A1 = max(A1, *mx.begin() + *++mx.begin());
}
}
}
}
void dfs2 (int cur, int par) {
multiset<int> mxs;
for (auto [j, w] : adj[cur]) {
mxs.insert(dist[j] + w);
}
for (auto [j, w] : adj[cur]) {
if (j != par) {
int v = dist[j] + w;
mxs.erase(mxs.find(v));
if (mxs.size()) distu[j] = max(distu[j], *--mxs.end() + w);
distu[j] = max(distu[j], distu[cur] + w);
mxs.insert(v);
}
}
for (auto [j, w] : adj[cur]) {
if (j != par) dfs2(j, cur);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0 ; i < M ; i++) {
adj[A[i]].emplace_back(B[i], T[i]);
adj[B[i]].emplace_back(A[i], T[i]);
}
for (int i = 0 ; i < N ; i++) {
if (cmp[i] == 0) {
cmp[i] = id;
dfs(i, -1);
dfs2(i, -1);
id++;
}
}
int vals[id];
memset(vals, -1, sizeof vals);
for (int i = 0 ; i < N ; i++) {
// cerr << "debug dist[" << i << "]: " << dist[i] << endl;
if (vals[cmp[i]] == -1) {
vals[cmp[i]] = max(distu[i],dist[i]);
} else {
vals[cmp[i]] = min(vals[cmp[i]], max(distu[i],dist[i]));
}
}
sort(vals + 1, vals + id, greater<int>());
if (id > 2) {
A2 = vals[1] + vals[2] + L;
}
if (id > 3) {
A3 = vals[2] + vals[3] + L + L;
}
return max({A1, A2, A3});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
21580 KB |
Output is correct |
2 |
Correct |
83 ms |
20452 KB |
Output is correct |
3 |
Correct |
60 ms |
19608 KB |
Output is correct |
4 |
Correct |
12 ms |
5360 KB |
Output is correct |
5 |
Correct |
7 ms |
3952 KB |
Output is correct |
6 |
Correct |
14 ms |
6764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
21580 KB |
Output is correct |
2 |
Correct |
83 ms |
20452 KB |
Output is correct |
3 |
Correct |
60 ms |
19608 KB |
Output is correct |
4 |
Correct |
12 ms |
5360 KB |
Output is correct |
5 |
Correct |
7 ms |
3952 KB |
Output is correct |
6 |
Correct |
14 ms |
6764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
6688 KB |
Output is correct |
2 |
Correct |
23 ms |
6732 KB |
Output is correct |
3 |
Correct |
24 ms |
6628 KB |
Output is correct |
4 |
Correct |
28 ms |
6632 KB |
Output is correct |
5 |
Correct |
26 ms |
6604 KB |
Output is correct |
6 |
Correct |
23 ms |
6864 KB |
Output is correct |
7 |
Correct |
26 ms |
6848 KB |
Output is correct |
8 |
Correct |
24 ms |
6664 KB |
Output is correct |
9 |
Correct |
21 ms |
6584 KB |
Output is correct |
10 |
Correct |
25 ms |
6832 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
5 ms |
4052 KB |
Output is correct |
13 |
Correct |
6 ms |
4196 KB |
Output is correct |
14 |
Correct |
6 ms |
4060 KB |
Output is correct |
15 |
Correct |
6 ms |
4052 KB |
Output is correct |
16 |
Correct |
6 ms |
3936 KB |
Output is correct |
17 |
Correct |
5 ms |
3668 KB |
Output is correct |
18 |
Correct |
6 ms |
4200 KB |
Output is correct |
19 |
Correct |
5 ms |
4052 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2648 KB |
Output is correct |
23 |
Correct |
5 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
21580 KB |
Output is correct |
2 |
Correct |
83 ms |
20452 KB |
Output is correct |
3 |
Correct |
60 ms |
19608 KB |
Output is correct |
4 |
Correct |
12 ms |
5360 KB |
Output is correct |
5 |
Correct |
7 ms |
3952 KB |
Output is correct |
6 |
Correct |
14 ms |
6764 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |