#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
// THE ANSWER WAS THAT WE FIND THE REPRESENTATIVE FOR EACH COMPONENT
// THE LONGEST DISTANCE WILL EITHER BE C1 + C2 + L OR 2L + C2 + C3
// WHERE C[] ARE OUR COMPONENTS SORTED BY MIN MAX LENGTH TO LEAF (DESCENDING)
/* 1. PROCESS EACH COMPONENT {
STORE 2 MAX LENGTHS
KEEP AN ARRAY TO STORE THE PARENTS
POSSIBLE STORE MAX LENGTH IN PAIRS WHERE WE STORE THE LENGTH AND THE CHILD
}
*/
#define pi pair<int, int>
#define pb push_back
#define f first
#define s second
const int MAXN = 100010;
vector<pi> adj[MAXN];
bool vis[MAXN];
pi dist[MAXN][2];
pi par[MAXN];
vector<int> comp, totals;
const int INF = 1e7;
void dfs(int v, int p)
{
if (vis[v]) return;
comp.pb(v);
vis[v] = 1;
dist[v][0] = {-1, 0};
dist[v][1] = {-1, 0};
// we want to store the 2 longest distances from node v to a leaf
for (pi child : adj[v])
{
if (child.f == p) {
par[v] = {p, child.s};
continue;
}
dfs(child.f, v);
int prop = dist[child.f][0].s + child.s;
if (prop > dist[v][1].s)
{
if (prop >= dist[v][0].s)
{
// shift
pi temp = dist[v][0];
dist[v][0] = {child.f, prop};
dist[v][1] = temp;
} else {
// replace max2
dist[v][1] = {child.f, prop};
}
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
// returns the min max travel time
// init the adj list
for (int i = 0; i < M; i++)
{
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
// we can perform dfs on this for each component
for (int i = 0; i < N; i++)
{
if (!vis[i])
{
// perform DFS to find 2 Max Lengths
dfs(i, i);
// we now have all the downward max lengths.
// process each node in the component and query for the node with the min max dist
int imax = INF;
// cout << "starting node: " << i << "\n";
for (int j = 0; j < comp.size() ; j++)
{
int node = comp[j];
int len = dist[node][0].s;
int source = dist[node][0].f;
if (node != i)
{
// add parent distance
int d;
if (dist[par[node].f][0].f == node) {
d = dist[par[node].f][1].s + par[node].s;
} else {
d = dist[par[node].f][0].s + par[node].s;
}
if (d > dist[node][1].s)
{
if (d >= dist[node][0].s)
{
pi temp = dist[node][0];
dist[node][0] = {par[node].f, d};
dist[node][1] = temp;
} else {
dist[node][1] = {par[node].f, d};
}
}
len = dist[node][0].s;
}
imax = min(imax, len);
// print out the distances for each of our nodes
//cout << node << " : { " << dist[node][0].f << ", " << dist[node][0].s << "} {" << dist[node][1].f << "," << dist[node][1].s << "}\n";
}
totals.pb(imax);
comp.clear();
}
}
int val = totals.size();
sort(totals.begin(), totals.end(), greater<int>());
int ans;
//cout << "Totals size: " << to_string(totals.size()) << "\n";
//for (int i = 0; i < totals.size(); i++) cout << "t" << i << ": " << totals[i] << "\n";
if (totals.size() > 2) ans = max(L + totals[0] + totals[1], 2 * L + totals[1] + totals[2]);
else if (totals.size() == 2) ans = L + totals[0] + totals[1];
else ans = totals[0];
return ans;
}
//void solve()
//{
// // take in input and call travelTime
// int n, m, l;
//
// cin >> n >> m >> l;
//
// int A[m], B[m], T[m];
//
// for (int i = 0; i < m; i++) cin >> A[i];
// for (int i = 0; i < m; i++) cin >> B[i];
// for (int i = 0; i < m; i++) cin >> T[i];
//
// cout << travelTime(n, m, l, A, B, T) << "\n";
//
// return;
//}
//
//int main()
//{
// auto start = chrono::high_resolution_clock::now();
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
// int t = 1;
// // cin >> t;
// for(int i = 0; i < t; i++){
// // cout << "Case #" << i + 1 << ": ";
// solve();
// }
// auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// // cout <<"Time taken : "<<duration.count() <<" milliseconds\n";
//}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:93:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int j = 0; j < comp.size() ; j++)
| ~~^~~~~~~~~~~~~
dreaming.cpp:98:21: warning: unused variable 'source' [-Wunused-variable]
98 | int source = dist[node][0].f;
| ^~~~~~
dreaming.cpp:138:9: warning: unused variable 'val' [-Wunused-variable]
138 | int val = totals.size();
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
14596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
14596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
8024 KB |
Output is correct |
2 |
Correct |
24 ms |
8020 KB |
Output is correct |
3 |
Correct |
30 ms |
8024 KB |
Output is correct |
4 |
Correct |
29 ms |
8008 KB |
Output is correct |
5 |
Correct |
20 ms |
7980 KB |
Output is correct |
6 |
Correct |
21 ms |
8664 KB |
Output is correct |
7 |
Correct |
31 ms |
8260 KB |
Output is correct |
8 |
Correct |
20 ms |
7888 KB |
Output is correct |
9 |
Correct |
21 ms |
7864 KB |
Output is correct |
10 |
Correct |
23 ms |
8280 KB |
Output is correct |
11 |
Correct |
2 ms |
2672 KB |
Output is correct |
12 |
Correct |
7 ms |
5324 KB |
Output is correct |
13 |
Correct |
6 ms |
5488 KB |
Output is correct |
14 |
Correct |
5 ms |
5324 KB |
Output is correct |
15 |
Correct |
5 ms |
5324 KB |
Output is correct |
16 |
Correct |
5 ms |
5184 KB |
Output is correct |
17 |
Correct |
5 ms |
4940 KB |
Output is correct |
18 |
Correct |
6 ms |
5584 KB |
Output is correct |
19 |
Correct |
5 ms |
5324 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
14596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |