#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
vector< pair< int,int > > g[N];
bool mark[N];
int ans;
int dp[N][2];
int all[N];
void dfs(int u,int p = -1)
{
mark[u] = true;
for(auto &x:g[u])
{
if(x.first==p) continue;
dfs(x.first,u);
if(dp[x.first][0]+x.second>dp[u][0]) dp[u][0] = dp[x.first][0]+x.second;
else if(dp[x.first][0]+x.second>dp[u][1]) dp[u][1] = dp[x.first][0]+x.second;
}
}
int reroot(int u,int p = -1)
{
int mn = dp[u][0];
for(auto &x:g[u])
{
if(x.first==p) continue;
int nowv = x.second+dp[u][0];
if(x.second+dp[x.first][0]==dp[u][0]) nowv = dp[u][1]+x.second;
if(nowv>dp[x.first][0]) dp[x.first][0] = nowv;
else if(nowv>dp[x.first][1]) dp[x.first][1] = nowv;
mn = min(mn,reroot(x.first,u));
}
ans = max(ans,dp[u][0]+dp[u][1]);
return mn;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[])
{
int cnt = 0;
for(int i=0;i<m;i++) g[a[i]].push_back({b[i],t[i]}),g[b[i]].push_back({a[i],t[i]});
for(int i=0;i<n;i++)
{
if(!mark[i])
{
++cnt;
dfs(i);
all[cnt] = reroot(i);
}
}
sort(all+1,all+cnt+1);
if(cnt>=2) ans = max(ans,all[cnt]+all[cnt-1]+l);
if(cnt>=3) ans = max(ans,all[cnt-1]+all[cnt-2]+2*l);
return ans;
}
/*
12 8 2
0 8 4
8 2 2
2 7 4
5 11 3
5 1 7
1 3 1
1 9 5
10 6 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11768 KB |
Output is correct |
2 |
Correct |
57 ms |
11512 KB |
Output is correct |
3 |
Correct |
41 ms |
9976 KB |
Output is correct |
4 |
Correct |
13 ms |
3968 KB |
Output is correct |
5 |
Correct |
11 ms |
3456 KB |
Output is correct |
6 |
Correct |
18 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11768 KB |
Output is correct |
2 |
Correct |
57 ms |
11512 KB |
Output is correct |
3 |
Correct |
41 ms |
9976 KB |
Output is correct |
4 |
Correct |
13 ms |
3968 KB |
Output is correct |
5 |
Correct |
11 ms |
3456 KB |
Output is correct |
6 |
Correct |
18 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11768 KB |
Output is correct |
2 |
Correct |
57 ms |
11512 KB |
Output is correct |
3 |
Correct |
41 ms |
9976 KB |
Output is correct |
4 |
Correct |
13 ms |
3968 KB |
Output is correct |
5 |
Correct |
11 ms |
3456 KB |
Output is correct |
6 |
Correct |
18 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
6528 KB |
Output is correct |
2 |
Correct |
28 ms |
6528 KB |
Output is correct |
3 |
Correct |
31 ms |
6492 KB |
Output is correct |
4 |
Correct |
35 ms |
6776 KB |
Output is correct |
5 |
Correct |
28 ms |
6520 KB |
Output is correct |
6 |
Correct |
29 ms |
6784 KB |
Output is correct |
7 |
Correct |
29 ms |
6656 KB |
Output is correct |
8 |
Correct |
26 ms |
6400 KB |
Output is correct |
9 |
Correct |
26 ms |
6400 KB |
Output is correct |
10 |
Correct |
27 ms |
6656 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
9 ms |
3968 KB |
Output is correct |
13 |
Correct |
9 ms |
3968 KB |
Output is correct |
14 |
Correct |
8 ms |
3968 KB |
Output is correct |
15 |
Correct |
9 ms |
3968 KB |
Output is correct |
16 |
Correct |
9 ms |
3840 KB |
Output is correct |
17 |
Correct |
8 ms |
3456 KB |
Output is correct |
18 |
Correct |
9 ms |
3968 KB |
Output is correct |
19 |
Correct |
8 ms |
3840 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
8 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11768 KB |
Output is correct |
2 |
Correct |
57 ms |
11512 KB |
Output is correct |
3 |
Correct |
41 ms |
9976 KB |
Output is correct |
4 |
Correct |
13 ms |
3968 KB |
Output is correct |
5 |
Correct |
11 ms |
3456 KB |
Output is correct |
6 |
Correct |
18 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11768 KB |
Output is correct |
2 |
Correct |
57 ms |
11512 KB |
Output is correct |
3 |
Correct |
41 ms |
9976 KB |
Output is correct |
4 |
Correct |
13 ms |
3968 KB |
Output is correct |
5 |
Correct |
11 ms |
3456 KB |
Output is correct |
6 |
Correct |
18 ms |
4736 KB |
Output is correct |
7 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |