#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int maxn = 1e5 + 5;
bool visited[maxn] = { 0 };
int dist[maxn] = { 0 }; // Max distance to another node
basic_string <pii> adj[maxn];
pii fnd_furthest(int p, int parent)
{
// cout << p << "\n";
visited[p] = true;
pii maxd = pii(p, 0);
pii a;
for (const pii &i : adj[p])
{
// cout << p << " " << i.first << "\n";
if (i.first == parent) continue;
a = fnd_furthest(i.first, p);
a.second += i.second;
if (a.second > maxd.second) {
maxd = a;
}
}
dist[p] = max(dist[p], maxd.second);
return maxd;
}
// Finds the node in the component with the minimal maximum distance
int fnd_min_max_dist(int p, int parent)
{
int mind = dist[p];
for (pii &i : adj[p])
{
if (i.first == parent) continue;
if (dist[i.first] >= mind || dist[i.first] == 0) continue;
mind = fnd_min_max_dist(i.first, p);
}
return mind;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int n = N, m = M;
for (int i = 0; i < m; i++)
{
adj[A[i]].push_back(pii(B[i], T[i]));
adj[B[i]].push_back(pii(A[i], T[i]));
}
vector <int> distances;
int max_comp = 0;
int components = 0;
pii a, b;
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
components ++;
a = fnd_furthest(i, -1);
b = fnd_furthest(a.first, -1);
a = fnd_furthest(b.first, -1);
// cout << a.first << " " << b.first << " " << a.second << " " << fnd_min_max_dist(a.first, -1) << "\n";
// We have now the diameter by a, b
max_comp = max(max_comp, a.second);
distances.emplace_back(fnd_min_max_dist(a.first, -1));
}
}
// for (int i = 0; i < n; i++) cout << dist[i] << " ";
// cout << "\n";
sort(distances.begin(), distances.end(), greater<int>());
if (components == 1)
{
return max_comp;
}
if (components == 2)
{
return max({max_comp, distances[0] + distances[1] + L});
}
return max({max_comp, distances[0] + distances[1] + L, distances[2] + distances[1] + 2 * L});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
16076 KB |
Output is correct |
2 |
Correct |
55 ms |
16168 KB |
Output is correct |
3 |
Correct |
34 ms |
11928 KB |
Output is correct |
4 |
Correct |
8 ms |
5252 KB |
Output is correct |
5 |
Correct |
6 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6272 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
22 ms |
7696 KB |
Output is correct |
9 |
Correct |
32 ms |
9684 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
51 ms |
11584 KB |
Output is correct |
12 |
Correct |
56 ms |
13752 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
16076 KB |
Output is correct |
2 |
Correct |
55 ms |
16168 KB |
Output is correct |
3 |
Correct |
34 ms |
11928 KB |
Output is correct |
4 |
Correct |
8 ms |
5252 KB |
Output is correct |
5 |
Correct |
6 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6272 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
22 ms |
7696 KB |
Output is correct |
9 |
Correct |
32 ms |
9684 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
51 ms |
11584 KB |
Output is correct |
12 |
Correct |
56 ms |
13752 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3444 KB |
Output is correct |
16 |
Incorrect |
2 ms |
3444 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
4992 KB |
Output is correct |
2 |
Correct |
15 ms |
5000 KB |
Output is correct |
3 |
Correct |
16 ms |
4996 KB |
Output is correct |
4 |
Correct |
18 ms |
5000 KB |
Output is correct |
5 |
Correct |
15 ms |
5076 KB |
Output is correct |
6 |
Correct |
19 ms |
5328 KB |
Output is correct |
7 |
Correct |
18 ms |
5044 KB |
Output is correct |
8 |
Correct |
17 ms |
5004 KB |
Output is correct |
9 |
Correct |
17 ms |
4940 KB |
Output is correct |
10 |
Correct |
19 ms |
5052 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Correct |
6 ms |
4600 KB |
Output is correct |
13 |
Correct |
8 ms |
4608 KB |
Output is correct |
14 |
Correct |
6 ms |
4560 KB |
Output is correct |
15 |
Correct |
9 ms |
4492 KB |
Output is correct |
16 |
Correct |
8 ms |
4560 KB |
Output is correct |
17 |
Correct |
5 ms |
4432 KB |
Output is correct |
18 |
Correct |
6 ms |
4560 KB |
Output is correct |
19 |
Correct |
6 ms |
4516 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3448 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
6 ms |
4560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3444 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
16076 KB |
Output is correct |
2 |
Correct |
55 ms |
16168 KB |
Output is correct |
3 |
Correct |
34 ms |
11928 KB |
Output is correct |
4 |
Correct |
8 ms |
5252 KB |
Output is correct |
5 |
Correct |
6 ms |
4436 KB |
Output is correct |
6 |
Correct |
13 ms |
6272 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
22 ms |
7696 KB |
Output is correct |
9 |
Correct |
32 ms |
9684 KB |
Output is correct |
10 |
Correct |
2 ms |
3540 KB |
Output is correct |
11 |
Correct |
51 ms |
11584 KB |
Output is correct |
12 |
Correct |
56 ms |
13752 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3444 KB |
Output is correct |
16 |
Incorrect |
2 ms |
3444 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |