This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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(int i = 1;i<tree[now].size();i++)assert(inside(Y,tree[now][i-1].fs)>=inside(Y,tree[now][i].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);
}
bool chain = inside(Y,nxt);
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;
}
}
*/
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 (stderr)
closing.cpp: In function 'void dfs3(int, int)':
closing.cpp:74:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 1;i<tree[now].size();i++)assert(inside(Y,tree[now][i-1].fs)>=inside(Y,tree[now][i].fs));
| ~^~~~~~~~~~~~~~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:137:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int i = 0;i<U.size();i++){
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |