#include<bits/stdc++.h>
#include"dreaming.h"
using namespace std;
typedef long long ll;
const int maxn = 1e5;
vector<vector<pair<int, int>>> g(maxn+5);
vector<bool> vis(maxn+5, false);
vector<ll> dt(maxn+5), ht(maxn+5, 0), group(maxn+5);
vector<multiset<ll>> S(maxn+5);
void dfs1(int v, int P){
group[v] = P;
vis[v] = 1;
int maxi1 = 0, maxi2 = 0;
for(pair<int, int> pp : g[v]){
int u = pp.first, w = pp.second;
if(vis[u])
continue;
dfs1(u, P);
ht[v] = max(ht[v], ht[u]+w);
if(ht[u]+w >= maxi1){
maxi2 = maxi1, maxi1 = ht[u]+w;
} else if(ht[u]+w >= maxi2){
maxi2 = ht[u]+w;
}
}
dt[v] = maxi1+maxi2;
}
void dfs2(int v, int p){
vis[v] = 1;
for(pair<int, int> pp : g[v]){
int u = pp.first, w = pp.second;
if(u == p)
continue;
S[v].insert(ht[u]+w);
}
for(pair<int, int> pp : g[v]){
int u = pp.first, w = pp.second;
if(u == p)
continue;
if(S[v].size() == 1){
S[u].insert(w);
} else if(!S[v].empty()){
S[v].erase(S[v].find(ht[u]+w));
int best = *--S[v].end();
S[u].insert(best+w);
S[v].insert(ht[u]+w);
}
}
if(!S[v].empty())
ht[v] = *--S[v].end();
for(pair<int, int> pp : g[v]){
int u = pp.first;
if(vis[u])
continue;
dfs2(u, v);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
for(int i = 0; i < M; i++){
g[A[i]].emplace_back(B[i], T[i]);
g[B[i]].emplace_back(A[i], T[i]);
}
for(int i = 0; i < N; i++)
if(!vis[i])
dfs1(i, i);
fill(vis.begin(), vis.end(), false);
for(int i = 0; i < N; i++)
if(!vis[i])
dfs2(i, -1);
long long ans = 0;
for(int i = 0; i < N; i++)
ans = max(ans, dt[i]);
vector<pair<int, int>> order;
order.reserve(N);
for(int i = 0; i < N; i++)
order.emplace_back(ht[i], group[i]);
sort(order.begin(), order.end());
vector<int> G;
G.reserve(N);
vector<bool> used(N+5, false);
for(int i = 0; i < N; i++){
if(used[order[i].second])
continue;
G.emplace_back(order[i].first);
used[order[i].second] = true;
}
int GN = G.size();
sort(G.begin(), G.end(), [&](int n1, int n2){
return n1 > n2;
});
for(int i = GN-1; i > 0; i--)
ans = max(ans, (long long) G[0] + (long long) G[i] + i*1ll*L);
return ans;
}
// int main(){}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
29236 KB |
Output is correct |
2 |
Correct |
83 ms |
28704 KB |
Output is correct |
3 |
Correct |
52 ms |
24672 KB |
Output is correct |
4 |
Correct |
14 ms |
12568 KB |
Output is correct |
5 |
Correct |
12 ms |
11476 KB |
Output is correct |
6 |
Correct |
19 ms |
13908 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
39 ms |
17956 KB |
Output is correct |
9 |
Correct |
51 ms |
21788 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
84 ms |
23944 KB |
Output is correct |
12 |
Correct |
85 ms |
26500 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9636 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9760 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9668 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9708 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9720 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
4 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
29236 KB |
Output is correct |
2 |
Correct |
83 ms |
28704 KB |
Output is correct |
3 |
Correct |
52 ms |
24672 KB |
Output is correct |
4 |
Correct |
14 ms |
12568 KB |
Output is correct |
5 |
Correct |
12 ms |
11476 KB |
Output is correct |
6 |
Correct |
19 ms |
13908 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
39 ms |
17956 KB |
Output is correct |
9 |
Correct |
51 ms |
21788 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
84 ms |
23944 KB |
Output is correct |
12 |
Correct |
85 ms |
26500 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9636 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9760 KB |
Output is correct |
23 |
Correct |
4 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9668 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
4 ms |
9708 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9720 KB |
Output is correct |
31 |
Correct |
4 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
5 ms |
9684 KB |
Output is correct |
35 |
Correct |
4 ms |
9684 KB |
Output is correct |
36 |
Correct |
5 ms |
9684 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
5 ms |
9684 KB |
Output is correct |
39 |
Correct |
83 ms |
29136 KB |
Output is correct |
40 |
Correct |
85 ms |
28656 KB |
Output is correct |
41 |
Correct |
53 ms |
24684 KB |
Output is correct |
42 |
Correct |
13 ms |
12500 KB |
Output is correct |
43 |
Correct |
5 ms |
9684 KB |
Output is correct |
44 |
Correct |
4 ms |
9684 KB |
Output is correct |
45 |
Correct |
5 ms |
9684 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9684 KB |
Output is correct |
48 |
Correct |
5 ms |
9696 KB |
Output is correct |
49 |
Correct |
5 ms |
9712 KB |
Output is correct |
50 |
Correct |
4 ms |
9684 KB |
Output is correct |
51 |
Correct |
5 ms |
9724 KB |
Output is correct |
52 |
Correct |
5 ms |
9684 KB |
Output is correct |
53 |
Correct |
5 ms |
9684 KB |
Output is correct |
54 |
Correct |
5 ms |
9684 KB |
Output is correct |
55 |
Correct |
5 ms |
9684 KB |
Output is correct |
56 |
Correct |
4 ms |
9684 KB |
Output is correct |
57 |
Correct |
115 ms |
25032 KB |
Output is correct |
58 |
Correct |
106 ms |
24980 KB |
Output is correct |
59 |
Correct |
109 ms |
25028 KB |
Output is correct |
60 |
Correct |
107 ms |
24976 KB |
Output is correct |
61 |
Correct |
109 ms |
24804 KB |
Output is correct |
62 |
Correct |
113 ms |
24900 KB |
Output is correct |
63 |
Correct |
103 ms |
24752 KB |
Output is correct |
64 |
Correct |
101 ms |
24712 KB |
Output is correct |
65 |
Correct |
106 ms |
24740 KB |
Output is correct |
66 |
Correct |
109 ms |
24668 KB |
Output is correct |
67 |
Correct |
109 ms |
25052 KB |
Output is correct |
68 |
Correct |
117 ms |
25280 KB |
Output is correct |
69 |
Correct |
109 ms |
25072 KB |
Output is correct |
70 |
Correct |
110 ms |
25012 KB |
Output is correct |
71 |
Correct |
4 ms |
9684 KB |
Output is correct |
72 |
Correct |
9 ms |
10092 KB |
Output is correct |
73 |
Correct |
7 ms |
10196 KB |
Output is correct |
74 |
Correct |
7 ms |
10196 KB |
Output is correct |
75 |
Correct |
6 ms |
10068 KB |
Output is correct |
76 |
Correct |
7 ms |
10120 KB |
Output is correct |
77 |
Correct |
7 ms |
10196 KB |
Output is correct |
78 |
Correct |
7 ms |
10184 KB |
Output is correct |
79 |
Correct |
9 ms |
10196 KB |
Output is correct |
80 |
Correct |
109 ms |
24908 KB |
Output is correct |
81 |
Correct |
110 ms |
24908 KB |
Output is correct |
82 |
Correct |
110 ms |
24860 KB |
Output is correct |
83 |
Correct |
108 ms |
24920 KB |
Output is correct |
84 |
Correct |
6 ms |
10068 KB |
Output is correct |
85 |
Correct |
6 ms |
10068 KB |
Output is correct |
86 |
Correct |
7 ms |
10068 KB |
Output is correct |
87 |
Correct |
9 ms |
10068 KB |
Output is correct |
88 |
Correct |
6 ms |
10068 KB |
Output is correct |
89 |
Correct |
6 ms |
10068 KB |
Output is correct |
90 |
Correct |
6 ms |
10016 KB |
Output is correct |
91 |
Correct |
7 ms |
10068 KB |
Output is correct |
92 |
Correct |
6 ms |
10068 KB |
Output is correct |
93 |
Correct |
6 ms |
10068 KB |
Output is correct |
94 |
Correct |
5 ms |
9700 KB |
Output is correct |
95 |
Correct |
5 ms |
9684 KB |
Output is correct |
96 |
Correct |
5 ms |
9684 KB |
Output is correct |
97 |
Correct |
5 ms |
9684 KB |
Output is correct |
98 |
Correct |
5 ms |
9684 KB |
Output is correct |
99 |
Correct |
5 ms |
9684 KB |
Output is correct |
100 |
Correct |
5 ms |
9716 KB |
Output is correct |
101 |
Correct |
5 ms |
9684 KB |
Output is correct |
102 |
Correct |
6 ms |
9684 KB |
Output is correct |
103 |
Correct |
5 ms |
9684 KB |
Output is correct |
104 |
Correct |
11 ms |
11476 KB |
Output is correct |
105 |
Correct |
20 ms |
13908 KB |
Output is correct |
106 |
Correct |
5 ms |
9684 KB |
Output is correct |
107 |
Correct |
38 ms |
17932 KB |
Output is correct |
108 |
Correct |
51 ms |
21780 KB |
Output is correct |
109 |
Correct |
6 ms |
9812 KB |
Output is correct |
110 |
Correct |
74 ms |
23936 KB |
Output is correct |
111 |
Correct |
87 ms |
26444 KB |
Output is correct |
112 |
Correct |
5 ms |
9884 KB |
Output is correct |
113 |
Correct |
117 ms |
36528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
15968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9636 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9760 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9668 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9708 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9720 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
4 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Incorrect |
5 ms |
9804 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
29236 KB |
Output is correct |
2 |
Correct |
83 ms |
28704 KB |
Output is correct |
3 |
Correct |
52 ms |
24672 KB |
Output is correct |
4 |
Correct |
14 ms |
12568 KB |
Output is correct |
5 |
Correct |
12 ms |
11476 KB |
Output is correct |
6 |
Correct |
19 ms |
13908 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
39 ms |
17956 KB |
Output is correct |
9 |
Correct |
51 ms |
21788 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
84 ms |
23944 KB |
Output is correct |
12 |
Correct |
85 ms |
26500 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9636 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9760 KB |
Output is correct |
23 |
Correct |
4 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9668 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9684 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
4 ms |
9708 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9720 KB |
Output is correct |
31 |
Correct |
4 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
5 ms |
9684 KB |
Output is correct |
35 |
Correct |
4 ms |
9684 KB |
Output is correct |
36 |
Correct |
5 ms |
9684 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
5 ms |
9684 KB |
Output is correct |
39 |
Correct |
83 ms |
29136 KB |
Output is correct |
40 |
Correct |
85 ms |
28656 KB |
Output is correct |
41 |
Correct |
53 ms |
24684 KB |
Output is correct |
42 |
Correct |
13 ms |
12500 KB |
Output is correct |
43 |
Correct |
5 ms |
9684 KB |
Output is correct |
44 |
Correct |
4 ms |
9684 KB |
Output is correct |
45 |
Correct |
5 ms |
9684 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9684 KB |
Output is correct |
48 |
Correct |
5 ms |
9696 KB |
Output is correct |
49 |
Correct |
5 ms |
9712 KB |
Output is correct |
50 |
Correct |
4 ms |
9684 KB |
Output is correct |
51 |
Correct |
5 ms |
9724 KB |
Output is correct |
52 |
Correct |
5 ms |
9684 KB |
Output is correct |
53 |
Correct |
5 ms |
9684 KB |
Output is correct |
54 |
Correct |
5 ms |
9684 KB |
Output is correct |
55 |
Correct |
5 ms |
9684 KB |
Output is correct |
56 |
Correct |
4 ms |
9684 KB |
Output is correct |
57 |
Correct |
115 ms |
25032 KB |
Output is correct |
58 |
Correct |
106 ms |
24980 KB |
Output is correct |
59 |
Correct |
109 ms |
25028 KB |
Output is correct |
60 |
Correct |
107 ms |
24976 KB |
Output is correct |
61 |
Correct |
109 ms |
24804 KB |
Output is correct |
62 |
Correct |
113 ms |
24900 KB |
Output is correct |
63 |
Correct |
103 ms |
24752 KB |
Output is correct |
64 |
Correct |
101 ms |
24712 KB |
Output is correct |
65 |
Correct |
106 ms |
24740 KB |
Output is correct |
66 |
Correct |
109 ms |
24668 KB |
Output is correct |
67 |
Correct |
109 ms |
25052 KB |
Output is correct |
68 |
Correct |
117 ms |
25280 KB |
Output is correct |
69 |
Correct |
109 ms |
25072 KB |
Output is correct |
70 |
Correct |
110 ms |
25012 KB |
Output is correct |
71 |
Correct |
4 ms |
9684 KB |
Output is correct |
72 |
Correct |
9 ms |
10092 KB |
Output is correct |
73 |
Correct |
7 ms |
10196 KB |
Output is correct |
74 |
Correct |
7 ms |
10196 KB |
Output is correct |
75 |
Correct |
6 ms |
10068 KB |
Output is correct |
76 |
Correct |
7 ms |
10120 KB |
Output is correct |
77 |
Correct |
7 ms |
10196 KB |
Output is correct |
78 |
Correct |
7 ms |
10184 KB |
Output is correct |
79 |
Correct |
9 ms |
10196 KB |
Output is correct |
80 |
Correct |
109 ms |
24908 KB |
Output is correct |
81 |
Correct |
110 ms |
24908 KB |
Output is correct |
82 |
Correct |
110 ms |
24860 KB |
Output is correct |
83 |
Correct |
108 ms |
24920 KB |
Output is correct |
84 |
Correct |
6 ms |
10068 KB |
Output is correct |
85 |
Correct |
6 ms |
10068 KB |
Output is correct |
86 |
Correct |
7 ms |
10068 KB |
Output is correct |
87 |
Correct |
9 ms |
10068 KB |
Output is correct |
88 |
Correct |
6 ms |
10068 KB |
Output is correct |
89 |
Correct |
6 ms |
10068 KB |
Output is correct |
90 |
Correct |
6 ms |
10016 KB |
Output is correct |
91 |
Correct |
7 ms |
10068 KB |
Output is correct |
92 |
Correct |
6 ms |
10068 KB |
Output is correct |
93 |
Correct |
6 ms |
10068 KB |
Output is correct |
94 |
Correct |
5 ms |
9700 KB |
Output is correct |
95 |
Correct |
5 ms |
9684 KB |
Output is correct |
96 |
Correct |
5 ms |
9684 KB |
Output is correct |
97 |
Correct |
5 ms |
9684 KB |
Output is correct |
98 |
Correct |
5 ms |
9684 KB |
Output is correct |
99 |
Correct |
5 ms |
9684 KB |
Output is correct |
100 |
Correct |
5 ms |
9716 KB |
Output is correct |
101 |
Correct |
5 ms |
9684 KB |
Output is correct |
102 |
Correct |
6 ms |
9684 KB |
Output is correct |
103 |
Correct |
5 ms |
9684 KB |
Output is correct |
104 |
Correct |
11 ms |
11476 KB |
Output is correct |
105 |
Correct |
20 ms |
13908 KB |
Output is correct |
106 |
Correct |
5 ms |
9684 KB |
Output is correct |
107 |
Correct |
38 ms |
17932 KB |
Output is correct |
108 |
Correct |
51 ms |
21780 KB |
Output is correct |
109 |
Correct |
6 ms |
9812 KB |
Output is correct |
110 |
Correct |
74 ms |
23936 KB |
Output is correct |
111 |
Correct |
87 ms |
26444 KB |
Output is correct |
112 |
Correct |
5 ms |
9884 KB |
Output is correct |
113 |
Correct |
117 ms |
36528 KB |
Output is correct |
114 |
Incorrect |
40 ms |
15968 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |