#include "closing.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
const int mxn = 3020;
const ll inf = 2e18;
vector<pii> tree[mxn];
pii eul[mxn];
int ptr = 0;
ll dp[mxn][4][mxn*2];
ll arr[mxn],brr[mxn],crr[mxn];
ll dist[mxn];
int N;
int X,Y;
ll K;
int sz[mxn];
ll tmp[4][mxn];
void init(){
ptr = 0;
for(int i = 0;i<=N;i++)tree[i].clear();
}
void dfs(int now,int par){
if(now == par)dist[now] = 0;
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dist[nxt] = dist[now]+w;
dfs(nxt,now);
}
return;
}
void dfs2(int now,int par){
eul[now].fs = ++ptr;
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dfs2(nxt,now);
}
eul[now].sc = ptr;
return;
}
void calc(ll dp1[],ll dp2[],int s1,int s2,ll rdp[]){
for(int i = s1;i>=0;i--){
for(int j = min(i,s2);i-j<=s1-s2&&i-j>=0&&j>=0;j--){
rdp[i] = min({dp1[i],dp1[i-j]+dp2[j],rdp[i]});
}
}
return;
}
void dfs3(int now,int par){
sz[now] = 2;
for(int i = 0;i<4;i++)fill(dp[now][i],dp[now][i]+mxn*2,inf);
bool chain = false;
if(eul[Y].fs>=eul[now].fs&&eul[Y].sc<=eul[now].sc)chain = true;
dp[now][0][0] = 0;
dp[now][1][1] = arr[now];
dp[now][2][1] = brr[now];
dp[now][3][2] = crr[now];
//cerr<<now<<":"<<arr[now]<<' '<<brr[now]<<' '<<crr[now]<<' '<<chain<<endl;
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dfs3(nxt,now);
sz[now] += sz[nxt];
for(int i = 0;i<4;i++){
for(int j = 0;j<=sz[now];j++)tmp[i][j] = dp[now][i][j];
}
if(!chain){
calc(dp[now][1],dp[nxt][1],sz[now],sz[nxt],tmp[1]);
calc(dp[now][3],dp[nxt][1],sz[now],sz[nxt],tmp[3]);
calc(dp[now][2],dp[nxt][2],sz[now],sz[nxt],tmp[2]);
calc(dp[now][3],dp[nxt][2],sz[now],sz[nxt],tmp[3]);
calc(dp[now][3],dp[nxt][3],sz[now],sz[nxt],tmp[3]);
}
else{
calc(dp[now][0],dp[nxt][0],sz[now],sz[nxt],tmp[0]);
calc(dp[now][1],dp[nxt][0],sz[now],sz[nxt],tmp[1]);
calc(dp[now][1],dp[nxt][1],sz[now],sz[nxt],tmp[1]);
calc(dp[now][0],dp[nxt][2],sz[now],sz[nxt],tmp[0]);
calc(dp[now][1],dp[nxt][2],sz[now],sz[nxt],tmp[0]);
calc(dp[now][2],dp[nxt][2],sz[now],sz[nxt],tmp[2]);
calc(dp[now][3],dp[nxt][2],sz[now],sz[nxt],tmp[3]);
calc(dp[now][1],dp[nxt][3],sz[now],sz[nxt],tmp[1]);
calc(dp[now][3],dp[nxt][3],sz[now],sz[nxt],tmp[3]);
}
for(int i = 0;i<4;i++){
for(int j = 0;j<=sz[now];j++)dp[now][i][j] = tmp[i][j];
}
}
/*
for(int i =0;i<4;i++){
for(int j = 0;j<=sz[now];j++){
cerr<<now<<' '<<i<<' '<<j<<":"<<dp[now][i][j]<<endl;
}
}
*/
for(int i = 0;i<4;i++)dp[now][i][0] = 0;
return;
}
int max_score(int NN, int XX, int YY, long long KK,
std::vector<int> U, std::vector<int> V, std::vector<int> W){
X = XX,Y = YY;
K = KK;
init();
N = NN;
for(int i = 0;i<U.size();i++){
int a = U[i],b = V[i],w = W[i];
tree[a].push_back(pii(b,w));
tree[b].push_back(pii(a,w));
}
dfs(X,X);
for(int i = 0;i<N;i++)arr[i] = dist[i];
dfs(Y,Y);
for(int i = 0;i<N;i++)brr[i] = dist[i];
for(int i = 0;i<N;i++)crr[i] = max(arr[i],brr[i]);
dfs2(X,X);
dfs3(X,X);
int ans = 0;
for(int i =0;i<4;i++){
for(int j = 0;j<=N*2;j++){
if(dp[X][i][j]<=K)ans = max(ans,j);
}
}
return ans;
}
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:128:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for(int i = 0;i<U.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
40 ms |
5200 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4444 KB |
1st lines differ - on the 1st token, expected: '11', found: '13' |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10588 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |