#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];
vector<pii> graph[MAX];
ll dist1[MAX], dist2[MAX];
void dfs1(int node) {
visited[node] = 1;
for(auto [v, w] : graph[node]) {
if(visited[v]) continue;
dist1[v] = dist1[node] + w;
dfs1(v);
}
}
void dfs2(int node) {
visited[node] = 1;
for(auto [v, w] : graph[node]) {
if(visited[v]) continue;
dist2[v] = dist2[node] + w;
dfs2(v);
}
}
ll c[MAX];
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
for(int i = 0; i < N; ++i) graph[i].clear();
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]});
}
for(int i = 0; i < N; ++i) visited[i] = dist1[i] = 0;
dfs1(X);
for(int i = 0; i < N; ++i) visited[i] = dist2[i] = 0;
dfs2(Y);
int ans = 0;
for(int p = X; p < N; ++p) for(int q = 0; q <= Y; ++q) {
for(int i = 0; i < N; ++i) c[i] = 0;
for(int i = X; i <= p; ++i) c[i] = max(c[i], dist1[i]);
for(int i = q; i <= Y; ++i) c[i] = max(c[i], dist2[i]);
int cur = p - X + Y - q + 2;
ll sum = 0;
for(int i = 0; i < N; ++i) sum += c[i];
if(sum > K) continue;
int a = X, b = Y;
while(true) {
if(a == 0 && b == N - 1) break;
if(a == 0) {
ll val = max(0ll, dist2[b + 1] - c[b + 1]);
if(sum + val <= K) sum += val, ++cur, ++b;
else break;
continue;
}
if(b == N - 1) {
ll val = max(0ll, dist1[a - 1] - c[a - 1]);
if(sum + val <= K) sum += val, ++cur, --a;
else break;
continue;
}
ll val1 = max(0ll, dist1[a - 1] - c[a - 1]);
ll val2 = max(0ll, dist2[b + 1] - c[b + 1]);
if(val1 <= val2) {
if(sum + val1 <= K) sum += val1, --a;
else break;
}
else {
if(sum + val2 <= K) sum += val2, ++b;
else break;
}
}
ans = max(ans, cur);
}
return ans;
}
/*
2
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
4 0 3 20
0 1 18
1 2 1
2 3 19
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1033 ms |
25936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '30', found: '21' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '30', found: '21' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '30', found: '21' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
9560 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |