#include "dreaming.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
vector<pair<int, int> > adj[100005];
int go[100005];
int cost[1000005] = {0};
int vis[1000005] = {0};
int n;
int id = 0, max_path = 0;
vector<int> median;
void dfs(int u, int x, int path){
if(max_path <= path) {
id = u;
max_path = path;
}
vis[u] = 1;
for(int i = 0; i < adj[u].size(); i++){
int v, e;
v = adj[u][i].first;
e = adj[u][i].second;
if(v != x) {
dfs(v, u, path + e);
}
}
}
void dfss(int u, int x){
if(max_path <= cost[u]) {
id = u;
max_path = cost[u];
}
vis[u] = 1;
go[u] = x;
for(int i = 0; i < adj[u].size(); i++){
int v, e;
v = adj[u][i].first;
e = adj[u][i].second;
if(v != x) {
cost[v] = cost[u] + e;
dfss(v, u);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
memset(go, -1, sizeof(go));
for(int i = 0; i < M; i++){
adj[A[i]].pb(mp(B[i], T[i]));
adj[B[i]].pb(mp(A[i], T[i]));
}
for(int i = 0; i < n; i++)
if(vis[i] == 0){
id = 0;
max_path = 0;
dfs(i, -1, 0);
max_path = 0;
dfss(id, -1);
int diameter = max_path;
int u = id;
int path = 1e9;
while(u != -1){
path = min(path, max(diameter - cost[u], cost[u]));
u = go[u];
}
median.pb(path);
}
sort(median.begin(), median.end());
int sz = median.size();
int res = 0;
res = max(res, median[sz - 1] + median[sz - 2] + L);
if(sz > 2) res = max(res, median[sz - 2] + median[sz - 3] + 2 * L);
return res;
return 42;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfss(int, int)':
dreaming.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
14700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
14700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
14700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6896 KB |
Output is correct |
2 |
Correct |
29 ms |
7020 KB |
Output is correct |
3 |
Correct |
28 ms |
6896 KB |
Output is correct |
4 |
Correct |
28 ms |
6892 KB |
Output is correct |
5 |
Correct |
29 ms |
7148 KB |
Output is correct |
6 |
Correct |
31 ms |
7536 KB |
Output is correct |
7 |
Correct |
28 ms |
7280 KB |
Output is correct |
8 |
Correct |
27 ms |
6896 KB |
Output is correct |
9 |
Correct |
27 ms |
6888 KB |
Output is correct |
10 |
Correct |
29 ms |
7016 KB |
Output is correct |
11 |
Correct |
2 ms |
3052 KB |
Output is correct |
12 |
Correct |
7 ms |
4328 KB |
Output is correct |
13 |
Correct |
7 ms |
4456 KB |
Output is correct |
14 |
Correct |
7 ms |
4328 KB |
Output is correct |
15 |
Correct |
7 ms |
4476 KB |
Output is correct |
16 |
Correct |
9 ms |
4328 KB |
Output is correct |
17 |
Correct |
7 ms |
4200 KB |
Output is correct |
18 |
Correct |
7 ms |
4456 KB |
Output is correct |
19 |
Correct |
7 ms |
4328 KB |
Output is correct |
20 |
Incorrect |
2 ms |
3052 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
14700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
14700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |