#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, dp[N], degree[N], mx_lvl = 0, level[N];
queue<int> q;
set<int> centres;
bool vis[N];
void bfs(){
while(!q.empty()){
int node = q.front();
q.pop();
for(auto k : graph[node]){
degree[k.first]--;
dp[k.first] = dp[node] + k.second;
if(degree[k.first] == 1){
level[k.first] = level[node] + 1;
q.push(k.first);
mx_lvl = max(mx_lvl, level[k.first]);
}
}
}
int ans = INT_MAX, mn = INT_MAX;
for(auto k : connected){
if(level[k] == mx_lvl){
// cout << k << ' ' << level[k] << ' ' << dp[k] << '\n';
if(dp[k] < mn){
ans = k;
mn = dp[k];
}
}
}
centres.insert(ans);
// cout << ans << '\n';
// cout << "----------------\n";
}
void dfs(int source, int longest){
vis[source] = 1;
connected.push_back(source);
if(longest > mx){
mx = longest;
node = source;
}
for(auto k : graph[source])
if(!vis[k.first])
dfs(k.first, longest + k.second);
}
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;
// cout << degree[1] << ' ';
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;
connected.clear();
dfs(node, 0);
diameters.push_back(mx);
bfs();
mx_lvl = 0, mx = 0;
}
}
// cout << level[1] << ' ';
vector<int> centre;
for(auto k : centres)
centre.push_back(k);
int u = centre[0];
// cout << u << ' ';
for(int i = 1; i < centre.size(); ++i){
// cout << centre[i] << ' ';
int v = centre[i];
graph[u].push_back({v, L});
graph[v].push_back({u, L});
}
mx = 0;
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:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 1; i < centre.size(); ++i){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
38096 KB |
Output is correct |
2 |
Correct |
113 ms |
38116 KB |
Output is correct |
3 |
Correct |
78 ms |
33384 KB |
Output is correct |
4 |
Correct |
24 ms |
26860 KB |
Output is correct |
5 |
Correct |
24 ms |
26220 KB |
Output is correct |
6 |
Correct |
31 ms |
28012 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24916 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
38096 KB |
Output is correct |
2 |
Correct |
113 ms |
38116 KB |
Output is correct |
3 |
Correct |
78 ms |
33384 KB |
Output is correct |
4 |
Correct |
24 ms |
26860 KB |
Output is correct |
5 |
Correct |
24 ms |
26220 KB |
Output is correct |
6 |
Correct |
31 ms |
28012 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24916 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
38096 KB |
Output is correct |
2 |
Correct |
113 ms |
38116 KB |
Output is correct |
3 |
Correct |
78 ms |
33384 KB |
Output is correct |
4 |
Correct |
24 ms |
26860 KB |
Output is correct |
5 |
Correct |
24 ms |
26220 KB |
Output is correct |
6 |
Correct |
31 ms |
28012 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24916 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
91 ms |
35544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
38096 KB |
Output is correct |
2 |
Correct |
113 ms |
38116 KB |
Output is correct |
3 |
Correct |
78 ms |
33384 KB |
Output is correct |
4 |
Correct |
24 ms |
26860 KB |
Output is correct |
5 |
Correct |
24 ms |
26220 KB |
Output is correct |
6 |
Correct |
31 ms |
28012 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24916 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
38096 KB |
Output is correct |
2 |
Correct |
113 ms |
38116 KB |
Output is correct |
3 |
Correct |
78 ms |
33384 KB |
Output is correct |
4 |
Correct |
24 ms |
26860 KB |
Output is correct |
5 |
Correct |
24 ms |
26220 KB |
Output is correct |
6 |
Correct |
31 ms |
28012 KB |
Output is correct |
7 |
Incorrect |
17 ms |
24916 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |