#include <bits/stdc++.h>
#include <dreaming.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
vector<pair<long long int, long long int> > graph[N];
vector<int> diameters, connected;
int mx, node, degree[N], mx_lvl, level[N];
queue<int> q;
set<int> centres;
bool vis[N];
void dfs(int source, int longest){
vis[source] = 1;
if(longest > mx){
mx = longest;
node = source;
}
connected.push_back(source);
for(auto k : graph[source])
if(!vis[k.first])
dfs(k.first, longest + k.second);
}
void bfs(){
while(!q.empty()){
int node = q.front();
q.pop();
for(auto k : graph[node]){
degree[k.first]--;
if(degree[k.first] == 1){
level[k.first] = level[node] + 1;
q.push(k.first);
mx_lvl = max(mx_lvl, level[k.first]);
}
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for(int i = 0; i < M; ++i){
long long int u = A[i], v = B[i], c = T[i];
graph[u].push_back({v, c});
graph[v].push_back({u, c});
degree[u]++;
degree[v]++;
}
vector<int> diameters;
for(int i = 0; i < N; ++i){
if(!vis[i]){
node = i;
connected.clear();
dfs(i, 0);
for(auto k : connected){
if(degree[k] == 1)
q.push(k);
vis[k] = 0;
}
mx = 0;
dfs(node, 0);
diameters.push_back(mx);
bfs();
for(auto k : connected){
if(level[k] == mx_lvl){
centres.insert(k);
break;
}
}
mx_lvl = 0;
}
}
vector<int> centre;
for(auto k : centres)
centre.push_back(k);
int u = centre[0];
for(int i = 1; i < centre.size(); ++i){
int v = centre[i];
graph[u].push_back({v, L});
graph[v].push_back({u, L});
}
sort(diameters.rbegin(), diameters.rend());
memset(vis, 0, sizeof(vis));
dfs(0, 0);
memset(vis, 0, sizeof(vis));
mx = 0;
dfs(node, 0);
return max(diameters[0], mx);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 1; i < centre.size(); ++i){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37472 KB |
Output is correct |
2 |
Correct |
89 ms |
37344 KB |
Output is correct |
3 |
Correct |
62 ms |
33124 KB |
Output is correct |
4 |
Correct |
28 ms |
26732 KB |
Output is correct |
5 |
Correct |
24 ms |
26092 KB |
Output is correct |
6 |
Correct |
33 ms |
27752 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24812 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37472 KB |
Output is correct |
2 |
Correct |
89 ms |
37344 KB |
Output is correct |
3 |
Correct |
62 ms |
33124 KB |
Output is correct |
4 |
Correct |
28 ms |
26732 KB |
Output is correct |
5 |
Correct |
24 ms |
26092 KB |
Output is correct |
6 |
Correct |
33 ms |
27752 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24812 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37472 KB |
Output is correct |
2 |
Correct |
89 ms |
37344 KB |
Output is correct |
3 |
Correct |
62 ms |
33124 KB |
Output is correct |
4 |
Correct |
28 ms |
26732 KB |
Output is correct |
5 |
Correct |
24 ms |
26092 KB |
Output is correct |
6 |
Correct |
33 ms |
27752 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24812 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
34912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37472 KB |
Output is correct |
2 |
Correct |
89 ms |
37344 KB |
Output is correct |
3 |
Correct |
62 ms |
33124 KB |
Output is correct |
4 |
Correct |
28 ms |
26732 KB |
Output is correct |
5 |
Correct |
24 ms |
26092 KB |
Output is correct |
6 |
Correct |
33 ms |
27752 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24812 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
37472 KB |
Output is correct |
2 |
Correct |
89 ms |
37344 KB |
Output is correct |
3 |
Correct |
62 ms |
33124 KB |
Output is correct |
4 |
Correct |
28 ms |
26732 KB |
Output is correct |
5 |
Correct |
24 ms |
26092 KB |
Output is correct |
6 |
Correct |
33 ms |
27752 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24812 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |