#include<bits/stdc++.h>
#include "dreaming.h"
#define int long long
using namespace std;
int N, M;
vector<vector<pair<int,int>>> adj (1e5), distances (1e5);
vector<int> mxdist (1e5), mxinnerdist(1e5);
vector<vector<int>> getcomps(){
vector<bool> v (N);
vector<vector<int>> res;
for(int i = 0; i < N; i++){
if(!v[i]){
queue<int> q;
q.push(i);
res.push_back({});
while(!q.empty()){
int curr = q.front(); q.pop();
if(v[curr]) continue;
v[curr] = 1;
res[res.size() - 1].push_back(curr);
for(auto e : adj[curr]) q.push(e.first);
}
}
}
return res;
}
int dfs1(int node, int p){
for(int i = 0; i < adj[node].size(); i++){
if(adj[node][i].first == p) continue;
distances[node][i].first = dfs1(adj[node][i].first, node) + adj[node][i].second;
distances[node][i].second = i;
}
sort(distances[node].rbegin(), distances[node].rend());
if(distances[node].size() < 2) return 0; // right?
return distances[node][0].first;
}
void dfs2(int node, int p, int mxp){
if(p == -1){
if(adj[node].size() > 0) mxdist[node] = distances[node][0].first;
else mxdist[node] = 0;
if(adj[node].size() >= 2){
mxinnerdist[node] = distances[node][0].first + distances[node][1].first;
}else if(adj[node].size() == 1){
mxinnerdist[node] = distances[node][0].first;
}
}else if(adj[node].size() == 1){
mxdist[node] = mxp;
mxinnerdist[node] = mxp;
}else{
mxdist[node] = max(mxp, distances[node][0].first); // right?
mxinnerdist[node] = mxp + distances[node][0].first;
if(adj[node].size() > 2){
mxinnerdist[node] = max(mxinnerdist[node], distances[node][0].first + distances[node][1].first);
}
}
// cout << mxdist[node] << " " << node << endl;
for(int i = 0; i < adj[node].size(); i++){
if(adj[node][i].first == p) continue;
int t = mxp;
if(distances[node][0].second == i){
if(distances[node].size() >= 3) t = max(t, distances[node][1].first);
}else{
t = max(t, distances[node][0].first); //right?
}
t += adj[node][i].second;
dfs2(adj[node][i].first, node, t);
}
}
signed travelTime(signed _N, signed _M, signed L, signed A[], signed B[], signed T[]) {
N = _N;
M = _M;
for(int i = 0; i < M; i++){
adj[A[i]].push_back({B[i],T[i]});
adj[B[i]].push_back({A[i],T[i]});
distances[A[i]].push_back({-1,-1});
distances[B[i]].push_back({-1,-1});
}
vector<vector<int>> components = getcomps();
int C = components.size();
vector<int> compdists (C, INT_MAX);
int res = 0;
for(int i = 0; i < C; i++){
// cout << "root" << components[i][0] << endl;
dfs1(components[i][0], -1);
dfs2(components[i][0], -1, 0);
for(int j = 0; j < components[i].size(); j++){
compdists[i] = min(compdists[i], mxdist[components[i][j]]);
res = max(res, mxinnerdist[components[i][j]]);
}
}
sort(compdists.rbegin(), compdists.rend());
if(C == 1){
return res;
}else if(C == 2){
return max(L + compdists[0] + compdists[1], res);
}else{
return max(res, max(L + compdists[0] + compdists[1], 2 * L + compdists[1] + compdists[2]));
}
}
Compilation message
dreaming.cpp: In function 'long long int dfs1(long long int, long long int)':
dreaming.cpp:28:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < adj[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(long long int, long long int, long long int)':
dreaming.cpp:57:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < adj[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:86:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int j = 0; j < components[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
21488 KB |
Output is correct |
2 |
Correct |
115 ms |
21996 KB |
Output is correct |
3 |
Correct |
69 ms |
18452 KB |
Output is correct |
4 |
Correct |
15 ms |
8960 KB |
Output is correct |
5 |
Correct |
15 ms |
8320 KB |
Output is correct |
6 |
Correct |
26 ms |
10356 KB |
Output is correct |
7 |
Incorrect |
5 ms |
6656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
21488 KB |
Output is correct |
2 |
Correct |
115 ms |
21996 KB |
Output is correct |
3 |
Correct |
69 ms |
18452 KB |
Output is correct |
4 |
Correct |
15 ms |
8960 KB |
Output is correct |
5 |
Correct |
15 ms |
8320 KB |
Output is correct |
6 |
Correct |
26 ms |
10356 KB |
Output is correct |
7 |
Incorrect |
5 ms |
6656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
21488 KB |
Output is correct |
2 |
Correct |
115 ms |
21996 KB |
Output is correct |
3 |
Correct |
69 ms |
18452 KB |
Output is correct |
4 |
Correct |
15 ms |
8960 KB |
Output is correct |
5 |
Correct |
15 ms |
8320 KB |
Output is correct |
6 |
Correct |
26 ms |
10356 KB |
Output is correct |
7 |
Incorrect |
5 ms |
6656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
15148 KB |
Output is correct |
2 |
Correct |
92 ms |
15276 KB |
Output is correct |
3 |
Correct |
68 ms |
15172 KB |
Output is correct |
4 |
Correct |
76 ms |
15288 KB |
Output is correct |
5 |
Correct |
69 ms |
15148 KB |
Output is correct |
6 |
Correct |
93 ms |
16696 KB |
Output is correct |
7 |
Correct |
88 ms |
15404 KB |
Output is correct |
8 |
Correct |
79 ms |
14860 KB |
Output is correct |
9 |
Correct |
73 ms |
14764 KB |
Output is correct |
10 |
Correct |
95 ms |
15276 KB |
Output is correct |
11 |
Correct |
5 ms |
6656 KB |
Output is correct |
12 |
Correct |
30 ms |
12848 KB |
Output is correct |
13 |
Correct |
28 ms |
12972 KB |
Output is correct |
14 |
Correct |
29 ms |
12968 KB |
Output is correct |
15 |
Correct |
29 ms |
12976 KB |
Output is correct |
16 |
Correct |
30 ms |
12952 KB |
Output is correct |
17 |
Correct |
29 ms |
12840 KB |
Output is correct |
18 |
Correct |
29 ms |
12968 KB |
Output is correct |
19 |
Correct |
27 ms |
12968 KB |
Output is correct |
20 |
Correct |
5 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6656 KB |
Output is correct |
22 |
Correct |
6 ms |
6836 KB |
Output is correct |
23 |
Correct |
32 ms |
12968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
21488 KB |
Output is correct |
2 |
Correct |
115 ms |
21996 KB |
Output is correct |
3 |
Correct |
69 ms |
18452 KB |
Output is correct |
4 |
Correct |
15 ms |
8960 KB |
Output is correct |
5 |
Correct |
15 ms |
8320 KB |
Output is correct |
6 |
Correct |
26 ms |
10356 KB |
Output is correct |
7 |
Incorrect |
5 ms |
6656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
21488 KB |
Output is correct |
2 |
Correct |
115 ms |
21996 KB |
Output is correct |
3 |
Correct |
69 ms |
18452 KB |
Output is correct |
4 |
Correct |
15 ms |
8960 KB |
Output is correct |
5 |
Correct |
15 ms |
8320 KB |
Output is correct |
6 |
Correct |
26 ms |
10356 KB |
Output is correct |
7 |
Incorrect |
5 ms |
6656 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |