#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);
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;
}else if(adj[node].size() == 1){
mxdist[node] = mxp;
}else{
mxdist[node] = max(mxp, distances[node][0].first); // right?
}
// 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);
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]]);
}
}
sort(compdists.rbegin(), compdists.rend());
if(C == 1){
return compdists[0];
}else if(C == 2){
return L + compdists[0] + compdists[1];
}else{
return 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:47: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]
47 | for(int i = 0; i < adj[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:75: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]
75 | for(int j = 0; j < components[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
20592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
20592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
20592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
14232 KB |
Output is correct |
2 |
Correct |
65 ms |
14252 KB |
Output is correct |
3 |
Correct |
64 ms |
14252 KB |
Output is correct |
4 |
Correct |
64 ms |
14252 KB |
Output is correct |
5 |
Correct |
63 ms |
14124 KB |
Output is correct |
6 |
Correct |
78 ms |
15576 KB |
Output is correct |
7 |
Correct |
68 ms |
14380 KB |
Output is correct |
8 |
Correct |
63 ms |
13872 KB |
Output is correct |
9 |
Correct |
62 ms |
13740 KB |
Output is correct |
10 |
Correct |
69 ms |
14252 KB |
Output is correct |
11 |
Correct |
4 ms |
5888 KB |
Output is correct |
12 |
Correct |
27 ms |
12072 KB |
Output is correct |
13 |
Correct |
27 ms |
12208 KB |
Output is correct |
14 |
Correct |
30 ms |
12072 KB |
Output is correct |
15 |
Correct |
27 ms |
12072 KB |
Output is correct |
16 |
Correct |
28 ms |
12080 KB |
Output is correct |
17 |
Correct |
26 ms |
12080 KB |
Output is correct |
18 |
Correct |
27 ms |
12200 KB |
Output is correct |
19 |
Correct |
27 ms |
12072 KB |
Output is correct |
20 |
Correct |
4 ms |
5760 KB |
Output is correct |
21 |
Correct |
5 ms |
5760 KB |
Output is correct |
22 |
Correct |
5 ms |
6016 KB |
Output is correct |
23 |
Correct |
27 ms |
12080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
20592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
121 ms |
20592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |