#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fr first
#define sc second
vector<int> a, b, t, dp;
vector<pii> vec[100000];
vector<bool> f;
int dfs1(int nod, int papa)
{
f[nod] = true;
int doi = 0;
for(auto i : vec[nod])
if(i.fr != papa)
{
dfs1(i.fr, nod);
if(dp[nod] <= i.sc + dp[i.fr])
doi = dp[nod], dp[nod] = i.sc + dp[i.fr];
else
doi = max(doi, i.sc + dp[i.fr]);
}
return dp[nod] + doi;
}
int dfs2(int nod, int papa, int desus = 0)
{
int ans = desus, doi = 0;
for(auto i : vec[nod])
if(i.fr != papa)
{
if(ans <= i.sc + dp[i.fr])
doi = ans, ans = i.sc + dp[i.fr];
else
doi = max(doi, i.sc + dp[i.fr]);
}
int rasp = ans;
for(auto i : vec[nod])
if(i.fr != papa)
{
if(ans == i.sc + dp[i.fr])
rasp = min(rasp, dfs2(i.fr, nod, doi + i.sc));
else
rasp = min(rasp, dfs2(i.fr, nod, ans + i.sc));
}
return rasp;
}
int travelTime(int n, int m, int l, int A[], int B[], int T[])
{
for(int i = 0; i < m; i ++)
a.pb(A[i]);
for(int i = 0; i < m; i ++)
b.pb(B[i]);
for(int i = 0; i < m; i ++)
t.pb(T[i]);
for(int i = 0; i < m; i ++)
vec[a[i]].pb({b[i], t[i]}), vec[b[i]].pb({a[i], t[i]});
f.resize(n, false);
dp.resize(n, 0);
vector<int> vals, diams;
for(int i = 0; i < n; i ++)
if(!f[i])
diams.pb(dfs1(i, -1)), vals.pb(dfs2(i, -1));
sort(all(vals), [&](int a, int b){return a > b;});
sort(all(diams), [&](int a, int b){return a > b;});
int ans = 0;
if(vals.size() >= 2)
ans = vals[0] + vals[1] + l;
if(vals.size() >= 3)
ans = max(ans, vals[1] + vals[2] + 2 * l);
ans = max(ans, diams[0]);
return ans;
}
/*
int main()
{
int N, M, L, A[5000], B[5000], T[5000];
cin >> N >> M >> L;
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);
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14032 KB |
Output is correct |
2 |
Correct |
29 ms |
13536 KB |
Output is correct |
3 |
Correct |
22 ms |
12000 KB |
Output is correct |
4 |
Correct |
5 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
3800 KB |
Output is correct |
6 |
Correct |
9 ms |
5120 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
16 ms |
7488 KB |
Output is correct |
9 |
Correct |
21 ms |
9944 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
28 ms |
10988 KB |
Output is correct |
12 |
Correct |
31 ms |
12320 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2812 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2788 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14032 KB |
Output is correct |
2 |
Correct |
29 ms |
13536 KB |
Output is correct |
3 |
Correct |
22 ms |
12000 KB |
Output is correct |
4 |
Correct |
5 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
3800 KB |
Output is correct |
6 |
Correct |
9 ms |
5120 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
16 ms |
7488 KB |
Output is correct |
9 |
Correct |
21 ms |
9944 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
28 ms |
10988 KB |
Output is correct |
12 |
Correct |
31 ms |
12320 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2812 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2788 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
6792 KB |
Output is correct |
2 |
Correct |
15 ms |
6872 KB |
Output is correct |
3 |
Correct |
15 ms |
7048 KB |
Output is correct |
4 |
Correct |
14 ms |
6792 KB |
Output is correct |
5 |
Correct |
14 ms |
6792 KB |
Output is correct |
6 |
Correct |
17 ms |
7432 KB |
Output is correct |
7 |
Correct |
20 ms |
7124 KB |
Output is correct |
8 |
Correct |
15 ms |
6872 KB |
Output is correct |
9 |
Correct |
17 ms |
6868 KB |
Output is correct |
10 |
Correct |
17 ms |
7128 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
5 ms |
3976 KB |
Output is correct |
13 |
Correct |
5 ms |
4056 KB |
Output is correct |
14 |
Correct |
5 ms |
3976 KB |
Output is correct |
15 |
Correct |
5 ms |
3972 KB |
Output is correct |
16 |
Correct |
5 ms |
3976 KB |
Output is correct |
17 |
Correct |
5 ms |
4116 KB |
Output is correct |
18 |
Correct |
6 ms |
4056 KB |
Output is correct |
19 |
Correct |
5 ms |
3976 KB |
Output is correct |
20 |
Correct |
1 ms |
2664 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
5 ms |
4116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2812 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2788 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
14032 KB |
Output is correct |
2 |
Correct |
29 ms |
13536 KB |
Output is correct |
3 |
Correct |
22 ms |
12000 KB |
Output is correct |
4 |
Correct |
5 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
3800 KB |
Output is correct |
6 |
Correct |
9 ms |
5120 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
16 ms |
7488 KB |
Output is correct |
9 |
Correct |
21 ms |
9944 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
28 ms |
10988 KB |
Output is correct |
12 |
Correct |
31 ms |
12320 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2812 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2788 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |