#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*2];
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[]){
assert(max(s1,s2)<=N*2);
for(int i = 0;i<=s1-s2;i++){
for(int j = 0;j<=s2;j++){
rdp[i+j] = min({rdp[i+j],dp1[i]+dp2[j]});
}
}
return;
}
bool inside(int a,int b){//a in b
return eul[a].fs>=eul[b].fs&&eul[a].sc<=eul[b].sc;
}
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);
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;
sort(tree[now].begin(),tree[now].end(),[](pii a,pii b){return inside(Y,a.fs)>inside(Y,b.fs);});
for(auto [nxt,w]:tree[now]){
if(nxt == par)continue;
dfs3(nxt,now);
sz[now] += sz[nxt];
for(int i = 0;i<4;i++){
fill(tmp[i],tmp[i]+mxn*2,inf);
for(int j = 0;j<=sz[now];j++)tmp[i][j] = dp[now][i][j];
}
bool chain = false;
if(eul[Y].fs>=eul[nxt].fs&&eul[Y].sc<=eul[nxt].sc)chain = true;
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]);
for(int i = 0;i<4;i++){
for(int j = 0;j<=sz[now];j++)dp[now][i][j] = min(dp[now][i][j],tmp[i][j]);
}
}
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[1]);
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:139:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int i = 0;i<U.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
50 ms |
11604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
10784 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
10784 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
10784 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4696 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
10784 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
10784 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
10784 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
10784 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Incorrect |
2 ms |
10844 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |