#include "closing.h"
#include <bits/stdc++.h>
#define MAX 200005
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int visited[MAX], parent[MAX];
vector<pii> graph[MAX];
ll tmp[MAX];
ll cost1[MAX], cost2[MAX];
ll dist1[MAX], dist2[MAX];
void init(int node, int par) {
parent[node] = par;
for(auto [v, w] : graph[node]) if(v != par) init(v, node);
}
void dfs(int node, int par, ll dist[]) {
for(auto [v, w] : graph[node]) if(v != par) {
dist[v] = dist[node] + w;
dfs(v, node, dist);
}
}
ll c[MAX];
vector<int> path;
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
path.clear();
for(int i = 0; i < N; ++i) graph[i].clear();
for(int i = 0; i < N; ++i) dist1[i] = dist2[i] = visited[i] = c[i] = 0;
for(int i = 0; i < N - 1; ++i) {
graph[U[i]].push_back({V[i], W[i]});
graph[V[i]].push_back({U[i], W[i]});
}
init(0, -1);
dfs(X, -1, dist1);
dfs(Y, -1, dist2);
int node = X;
while(node != -1) visited[node]++, node = parent[node];
node = Y; int flag = 0;
while(node != -1) {
if(visited[node] == 1 && !flag) flag = 1;
else if(flag) visited[node]--;
else visited[node]++;
node = parent[node];
}
node = X;
while(node != -1 && visited[node]) {
path.push_back(node);
node = parent[node];
}
path.pop_back();
vector<int> path2;
node = Y;
while(node != -1 && visited[node]) {
path2.push_back(node);
node = parent[node];
}
reverse(path2.begin(), path2.end());
for(auto v : path2) path.push_back(v);
priority_queue<pair<ll, int> > pq;
for(int i = 1; i <= 2 * N; ++i) cost1[i] = (ll)(2e18);
int p = 0, q = path.size() - 1;
for(int i = 1; i <= 2 * N; ++i) {
if(p == path.size() && q == -1) break;
if(p == path.size()) {
cost1[i] = cost1[i - 1] + dist2[path[q]] - c[path[q]];
c[path[q]] = dist2[path[q]]; --q;
}
else if(q == -1) {
cost1[i] = cost1[i - 1] + dist1[path[p]] - c[path[p]];
c[path[p]] = dist1[path[p]]; ++p;
}
else if(dist1[path[p]] - c[path[p]] <= dist2[path[q]] - c[path[q]]) {
cost1[i] = cost1[i - 1] + dist1[path[p]] - c[path[p]];
c[path[p]] = dist1[path[p]]; ++p;
}
else {
cost1[i] = cost1[i - 1] + dist2[path[q]] - c[path[q]];
c[path[q]] = dist2[path[q]]; --q;
}
}
for(int i = 1; i <= 2 * N; ++i) cost2[i] = (ll)(2e18);
for(int i = 0; i < N; ++i) {
if(visited[i]) continue;
for(int j = 0; j <= 2 * N; ++j) tmp[j] = cost2[j];
for(int j = 1; j <= 2 * N; ++j) {
tmp[j] = min(tmp[j], cost2[j - 1] + min(dist1[i], dist2[i]));
if(j >= 2) tmp[j] = min(tmp[j], cost2[j - 2] + max(dist1[i], dist2[i]));
}
for(int j = 0; j <= 2 * N; ++j) cost2[j] = tmp[j];
}
int mx = 0;
for(int i = 2 * N, p = 0; i >= 0; --i) {
while(p < 2 * N && cost1[i] + cost2[p + 1] <= K) ++p;
if(cost1[i] + cost2[p] <= K) mx = max(mx, i + p);
}
return mx;
}
Compilation message
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:76:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | if(p == path.size() && q == -1) break;
| ~~^~~~~~~~~~~~~~
closing.cpp:77:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if(p == path.size()) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1052 ms |
32888 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
12756 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
2 ms |
14784 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
12756 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
2 ms |
14784 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14680 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
12732 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
2 ms |
12780 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14936 KB |
Output is correct |
4 |
Correct |
2 ms |
12756 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
2 ms |
14784 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14680 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
12732 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
2 ms |
12780 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
14680 KB |
Output is correct |
26 |
Correct |
32 ms |
14940 KB |
Output is correct |
27 |
Correct |
30 ms |
15188 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
13 ms |
15176 KB |
Output is correct |
30 |
Correct |
24 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
13148 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14936 KB |
Output is correct |
5 |
Correct |
2 ms |
12756 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14800 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Incorrect |
2 ms |
14692 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14936 KB |
Output is correct |
5 |
Correct |
2 ms |
12756 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14784 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
12732 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
2 ms |
14684 KB |
Output is correct |
22 |
Correct |
2 ms |
14684 KB |
Output is correct |
23 |
Correct |
2 ms |
14800 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
2 ms |
14684 KB |
Output is correct |
26 |
Correct |
2 ms |
14684 KB |
Output is correct |
27 |
Incorrect |
2 ms |
14692 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14936 KB |
Output is correct |
5 |
Correct |
2 ms |
12756 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14784 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
12732 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
12780 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
14684 KB |
Output is correct |
27 |
Correct |
2 ms |
14684 KB |
Output is correct |
28 |
Correct |
2 ms |
14684 KB |
Output is correct |
29 |
Correct |
2 ms |
14684 KB |
Output is correct |
30 |
Correct |
2 ms |
14800 KB |
Output is correct |
31 |
Correct |
2 ms |
14684 KB |
Output is correct |
32 |
Correct |
2 ms |
14684 KB |
Output is correct |
33 |
Correct |
2 ms |
14684 KB |
Output is correct |
34 |
Incorrect |
2 ms |
14692 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14936 KB |
Output is correct |
5 |
Correct |
2 ms |
12756 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14784 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
12732 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
12780 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
3 ms |
14680 KB |
Output is correct |
27 |
Correct |
32 ms |
14940 KB |
Output is correct |
28 |
Correct |
30 ms |
15188 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
13 ms |
15176 KB |
Output is correct |
31 |
Correct |
24 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
3 ms |
13148 KB |
Output is correct |
34 |
Correct |
2 ms |
14684 KB |
Output is correct |
35 |
Correct |
2 ms |
14684 KB |
Output is correct |
36 |
Correct |
2 ms |
14684 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
2 ms |
14800 KB |
Output is correct |
39 |
Correct |
2 ms |
14684 KB |
Output is correct |
40 |
Correct |
2 ms |
14684 KB |
Output is correct |
41 |
Correct |
2 ms |
14684 KB |
Output is correct |
42 |
Incorrect |
2 ms |
14692 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14936 KB |
Output is correct |
5 |
Correct |
2 ms |
12756 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
14784 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
12732 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
12780 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
3 ms |
14680 KB |
Output is correct |
27 |
Correct |
32 ms |
14940 KB |
Output is correct |
28 |
Correct |
30 ms |
15188 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
13 ms |
15176 KB |
Output is correct |
31 |
Correct |
24 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
3 ms |
13148 KB |
Output is correct |
34 |
Correct |
2 ms |
14684 KB |
Output is correct |
35 |
Correct |
2 ms |
14684 KB |
Output is correct |
36 |
Correct |
2 ms |
14684 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
2 ms |
14800 KB |
Output is correct |
39 |
Correct |
2 ms |
14684 KB |
Output is correct |
40 |
Correct |
2 ms |
14684 KB |
Output is correct |
41 |
Correct |
2 ms |
14684 KB |
Output is correct |
42 |
Incorrect |
2 ms |
14692 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
43 |
Halted |
0 ms |
0 KB |
- |