#include "dreaming.h"
#include "bits/stdc++.h"
#define pb push_back
#define pii pair<int, int>
using namespace std;
using ll = long long;
const int MAXN = 100005;
vector<vector<pair<int, int> > > adj(MAXN);
bool vis[MAXN];
pair<ll, int> down[MAXN][2];
void dfs_down(int v, int p)
{
vis[v] = true;
down[v][0].second = down[v][1].second = -1;
//cout << v << "\n";
for(pii e : adj[v])
{
//cout << e.first << " " << e.second << "\n";
if(e.first == p)
continue;
dfs_down(e.first, v);
ll val = down[e.first][0].first + e.second;
if(down[v][0].first < val)
{
down[v][0].first = val;
down[v][0].second = e.first;
} else if(down[v][1].first < val)
{
down[v][1].first = val;
down[v][1].second = e.first;
}
}
//cout << v << " " << down[v] << "\n";
}
pair<ll, int> dfs_up(int v, int p, ll top)
{
vis[v] = true;
pair<ll, int> curr = {max(top, down[v][0].first), v};
if(down[v][0].second != -1)
{
pii e;
for(pii tmp : adj[v])
{
if(down[v][0].second == tmp.first)
e = tmp;
}
pair<ll, int> temp = dfs_up(down[v][0].second, v, max(top, down[v][1].first) + e.second);
if(curr.first > temp.first)
curr = temp;
}
return curr;
}
void dfs_check(int v, int p)
{
vis[v] = true;
for(pii e : adj[v])
{
if(e.first == p)
continue;
dfs_check(e.first, v);
}
}
ll ans = 0;
ll dfs_long(int v, int p)
{
ll max_temp = 0, max_1 = 0;
for(pii e : adj[v])
{
if(e.first == p)
continue;
ll temp = dfs_long(e.first, v) + e.second;
max_temp = max(max_temp, max_1 + temp);
max_1 = max(max_1, temp);
}
ans = max(ans, max_temp);
return max_1;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++)
{
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
for(int i = 0; i < N; i++)
{
//cout << i << "\n";
if(!vis[i])
dfs_down(i, -1);
//cout << i << "\n";
}
for(int i = 0; i < N; i++)
vis[i] = false;
vector< pair<ll, int> > root_nodes;
for(int i = 0; i < N; i++)
{
if(!vis[i])
{
//cout << i << "\n";
pair<ll, int> temp = dfs_up(i, -1, 0);
dfs_check(i, -1);
//cout << i << " " << temp.first << " " << temp.second << "\n";
root_nodes.pb(temp);
}
}
sort(root_nodes.begin(), root_nodes.end());
reverse(root_nodes.begin(), root_nodes.end());
int from = root_nodes[0].second;
//cout << root_nodes.size() << "\n";
for(int i = 1; i < root_nodes.size(); i++)
{
int to = root_nodes[i].second;
adj[from].pb({to, L});
adj[to].pb({from, L});
//cout << from << " " << to << "\n";
}
dfs_long(0, -1);
return ans;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:124:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i = 1; i < root_nodes.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
14664 KB |
Output is correct |
2 |
Correct |
45 ms |
14356 KB |
Output is correct |
3 |
Correct |
33 ms |
12084 KB |
Output is correct |
4 |
Correct |
8 ms |
4436 KB |
Output is correct |
5 |
Correct |
6 ms |
3808 KB |
Output is correct |
6 |
Correct |
12 ms |
5332 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
14664 KB |
Output is correct |
2 |
Correct |
45 ms |
14356 KB |
Output is correct |
3 |
Correct |
33 ms |
12084 KB |
Output is correct |
4 |
Correct |
8 ms |
4436 KB |
Output is correct |
5 |
Correct |
6 ms |
3808 KB |
Output is correct |
6 |
Correct |
12 ms |
5332 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
10852 KB |
Output is correct |
2 |
Correct |
36 ms |
10896 KB |
Output is correct |
3 |
Correct |
33 ms |
10832 KB |
Output is correct |
4 |
Correct |
38 ms |
10884 KB |
Output is correct |
5 |
Correct |
33 ms |
10820 KB |
Output is correct |
6 |
Correct |
39 ms |
11844 KB |
Output is correct |
7 |
Correct |
41 ms |
11160 KB |
Output is correct |
8 |
Correct |
53 ms |
10736 KB |
Output is correct |
9 |
Correct |
30 ms |
10584 KB |
Output is correct |
10 |
Correct |
37 ms |
11184 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
25 ms |
11360 KB |
Output is correct |
13 |
Correct |
21 ms |
11360 KB |
Output is correct |
14 |
Correct |
21 ms |
11324 KB |
Output is correct |
15 |
Correct |
20 ms |
11332 KB |
Output is correct |
16 |
Correct |
23 ms |
11328 KB |
Output is correct |
17 |
Correct |
25 ms |
11320 KB |
Output is correct |
18 |
Correct |
21 ms |
11360 KB |
Output is correct |
19 |
Correct |
26 ms |
11264 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2664 KB |
Output is correct |
22 |
Correct |
2 ms |
2932 KB |
Output is correct |
23 |
Correct |
20 ms |
11308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2672 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
14664 KB |
Output is correct |
2 |
Correct |
45 ms |
14356 KB |
Output is correct |
3 |
Correct |
33 ms |
12084 KB |
Output is correct |
4 |
Correct |
8 ms |
4436 KB |
Output is correct |
5 |
Correct |
6 ms |
3808 KB |
Output is correct |
6 |
Correct |
12 ms |
5332 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |