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 <bits/stdc++.h>
using namespace std;
const long long INF = 1e18+1;
int max_score(int N,int X,int Y,long long K,
vector<int> U,vector<int> V,vector<int> W){
vector<vector<pair<int,long long>>> adj(N);
for(int i=0;i<N-1;i++) {
adj[U[i]].emplace_back(V[i],W[i]);
adj[V[i]].emplace_back(U[i],W[i]);
}
vector<long long> distX(N);
vector<long long> distY(N);
auto dijkastra = [&](int x) {
priority_queue<pair<long long,int>> pq;
pq.emplace(0,x);
vector<bool> visited(N);
while(!pq.empty()) {
auto [dist,idx] = pq.top();pq.pop();dist=-dist;
if(visited[idx])continue;
visited[idx]=true;
distX[idx]=dist;
for(auto[v,c]:adj[idx])if(!visited[v])pq.emplace(-dist-c,v);
}
};
dijkastra(Y);
swap(distX,distY);
dijkastra(X);
vector cost1(N+1,0ll);
vector cost2(N+1,0ll);
for(int i=0;i<N;i++) {
long long mini = min(distX[i],distY[i]);
long long maxi = max(distX[i],distY[i]);
cost1[i+1] = mini;
cost2[i+1] = maxi;
}
int simple_ans = 0;
{
auto c = cost1;
sort(c.begin(), c.end());
long long sum = 0;
for(int i=1;i<=N;i++) {
sum+=c[i];
if(sum<=K)simple_ans=i;
}
}
vector onPath(N,false);
{
function<bool(int,int)> dfs = [&](int x,int p) {
if(x==Y) {
onPath[x]=true;
return true;
}
for(auto[v,c]:adj[x])if(v!=p) {
if(dfs(v,x)) {
onPath[x]=true;
return true;
}
}
return false;
};
assert(dfs(X,-1));
}
vector DP(N+1,vector(2*N+1,INF));
DP[0][0]=0;
for(int i=1;i<=N;i++) {
for(int j=0;j<=2*N;j++) {
if(!onPath[i])DP[i][j]=min(DP[i][j],DP[i-1][j]);
if(j>0)DP[i][j]=min(DP[i][j],DP[i-1][j-1]+cost1[i]);
if(j>1)DP[i][j]=min(DP[i][j],DP[i-1][j-2]+cost2[i]);
}
}
for(int j=2*N;j;j--)if(DP[N][j]<=K)return max(j,simple_ans);
return simple_ans;
}
# | 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... |