#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<pair<ll,ll>>>graph;
vector<bool> vis;
void dfs1(vector<ll> &dist, ll x, ll d){
vis[x] = true;
dist[x] = d;
for (auto [y, w] : graph[x]){
if (!vis[y]){
dfs1(dist, y, d + w);
}
}
}
void dfs2(vector<pair<ll,ll>>dist, ll x, ll d){
vis[x] = true;
dist[x].first= d;
for (auto [y, w] : graph[x]){
if (!vis[y]){
dfs2(dist, y, d + w);
}
}
}
ll max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W){
graph.assign(N,vector<pair<ll,ll>>());
for(ll i=0;i<(N-1);i++){
graph[U[i]].push_back({V[i],W[i]});
graph[V[i]].push_back({U[i],W[i]});
}
vis.assign(N,false);
vector<ll>distX(N);
dfs1(distX,X,0);
vis.assign(N,false);
vector<ll>distY(N);
dfs1(distY,Y,0);
vis.assign(N,false);
vector<pair<ll,ll>>orden;
for(ll i=0;i<N;i++){
pair<ll,ll>par;
par.first=0;
par.second=i;
orden.push_back(par);
}
dfs2(orden,max_element(distX.begin(),distX.end())-distX.begin(),0);
sort(orden.begin(),orden.end());
ll pos_x;
ll pos_y;
for(ll i=0;i<N;i++){
if(orden[i].second==X){
pos_x=i;
}
else if(orden[i].second==Y){
pos_y=i;
}
}
pos_x++;
pos_y++;
vector<ll>prefix1(N+1,0);
for(ll i=0;i<N;i++){
prefix1[i+1]=prefix1[i]+distX[orden[i].second];
}
vector<ll>prefix2(N+1,0);
for(ll i=0;i<N;i++){
prefix2[i+1]=prefix2[i]+distY[orden[i].second];
}
vector<ll>prefixMax(N+1,0);
for(ll i=0;i<N;i++){
prefixMax[i+1]=prefixMax[i]+min(distY[orden[i].second],distX[orden[i].second]);
}
ll ans=0;
for(ll i=0;i<pos_x;i++){
for(ll j=pos_x;j<=N;j++){
for(ll k=0;k<pos_y;k++){
for(ll l=pos_y;l<=N;l++){
ll suma=(prefix1[j]-prefix1[i]+prefix2[l]-prefix2[k]);
if(k>=i&&l<=j){
suma-=(prefixMax[l]-prefixMax[k]);
}
else if(i>=k&&j<=l){
suma-=(prefixMax[j]-prefixMax[i]);
}
else if(k>=i&&k<=j){
suma-=(prefixMax[j]-prefixMax[k]);
}
else if(i>=k&&i<=l){
suma-=(prefixMax[l]-prefixMax[i]);
}
if(suma<=K){
ans=max((j-i+l-k),ans);
}
}
}
}
}
return ans;
}
Compilation message
closing.cpp: In function 'll max_score(int, int, int, ll, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:56:7: warning: 'pos_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
56 | pos_x++;
| ~~~~~^~
closing.cpp:57:7: warning: 'pos_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
57 | pos_y++;
| ~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 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 |
1078 ms |
2097152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
580 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Execution timed out |
1058 ms |
4444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
580 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Execution timed out |
1058 ms |
4444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 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 |
0 ms |
348 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 |
0 ms |
348 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 |
0 ms |
348 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 |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |