#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> graph;
vector<pair<int,int>> dum;
vector<int> radi, parent, stac, sum;
vector<bool> visited;
void recur(int a) {
for (pair<int,int> i: graph[a]) {
if (i.first != parent[a]) {
recur(i.first);
sum[a] = max(sum[a], sum[i.first] + i.second);
}
}
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
int node, radius, prevsum, next, radiusprev, temp, temp2;
for (int i=0;i<n;i++) {
visited.push_back(false);
sum.push_back(0);
graph.push_back(dum);
parent.push_back(0);
}
for (int i=0;i<m;i++) {
graph[a[i]].push_back(make_pair(b[i], t[i]));
graph[b[i]].push_back(make_pair(a[i], t[i]));
}
for (int i=0;i<n;i++) {
if (visited[i]) {
continue;
}
if (graph[i].size() == 0) {
visited[i] = true;
radi.push_back(0);
continue;
}
stac = {i}; parent[i] = -1;
while (stac.size() > 0) {
node = stac.back(); stac.pop_back();
visited[node] = true;
for (pair<int,int> j: graph[node]) {
if (j.first != parent[node]) {
parent[j.first] = node;
stac.push_back(j.first);
}
}
}
recur(i);
//for (int i=0;i<n;i++) {
// cout << sum [i] << ' ';
//}
//cout << '\n';
radius = sum[i]; prevsum = 0; radiusprev = INT_MAX; node = i;
while (radiusprev > radius) {
for (pair<int,int> j: graph[node]) {
if (j.first == parent[node]) {
continue;
}
if (sum[j.first] + j.second == sum[node]) {
next = j.first;
temp2 = j.second;
}
}
temp = 0;
for (pair<int,int> j: graph[node]) {
if (j.first == parent[node] || j.first == next) {
continue;
}
temp = max(temp, sum[j.first] + j.second);
}
prevsum = max(prevsum + temp2, temp + temp2);
radiusprev = radius; radius = max(prevsum, sum[next]); node = next;
//cout << radiusprev << ' ' << radius << ' ' << prevsum << '\n';
}
//cout << radiusprev << '\n' << '\n';
radi.push_back(radiusprev);
}
//for (int i: radi) {
// cout << i << ' ';
//}
//cout << '\n';
sort(radi.begin(), radi.end(), greater<int>());
if (radi.size() == 1) {
return radi[0];
} else if (radi.size() == 2) {
return radi[0] + radi[1] + l;
}
return max(radi[0] + radi[1], radi[1] + radi[2] + l) + l;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:81:49: warning: 'temp2' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | prevsum = max(prevsum + temp2, temp + temp2);
| ~~~~~^~~~~~~
dreaming.cpp:76:45: warning: 'next' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if (j.first == parent[node] || j.first == next) {
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
11264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
11264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6412 KB |
Output is correct |
2 |
Correct |
12 ms |
6828 KB |
Output is correct |
3 |
Correct |
11 ms |
7204 KB |
Output is correct |
4 |
Correct |
12 ms |
6668 KB |
Output is correct |
5 |
Correct |
15 ms |
6548 KB |
Output is correct |
6 |
Correct |
13 ms |
7212 KB |
Output is correct |
7 |
Correct |
12 ms |
6924 KB |
Output is correct |
8 |
Correct |
12 ms |
6668 KB |
Output is correct |
9 |
Correct |
12 ms |
7436 KB |
Output is correct |
10 |
Correct |
12 ms |
6924 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
5648 KB |
Output is correct |
13 |
Correct |
4 ms |
4624 KB |
Output is correct |
14 |
Correct |
4 ms |
5136 KB |
Output is correct |
15 |
Correct |
4 ms |
4624 KB |
Output is correct |
16 |
Correct |
4 ms |
4624 KB |
Output is correct |
17 |
Correct |
4 ms |
6156 KB |
Output is correct |
18 |
Correct |
4 ms |
5392 KB |
Output is correct |
19 |
Correct |
4 ms |
4624 KB |
Output is correct |
20 |
Correct |
0 ms |
444 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
4 ms |
4624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
11264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |