#include "closing.h"
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN=200010;
vector<pair<int,int>> g[MAXN];
long long distX[MAXN], distY[MAXN];
int dd[MAXN];
vector<int> path;
void dfsx(int at, int p) {
for(auto [to,w]:g[at]) {
if(to==p)continue;
distX[to]=distX[at]+w;
dfsx(to, at);
}
}
void dfsy(int at, int p) {
for(auto [to,w]:g[at]) {
if(to==p)continue;
distY[to]=distY[at]+w;
dfsy(to, at);
dd[at]|=dd[to];
}
if(dd[at])path.push_back(at);
}
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for(int i = 0;i<N-1;i++) {
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
}
dd[X]=1;
dfsx(X, -1);
dfsy(Y, -1);
int ans=0;
// case 1
{
int cnt=0;
priority_queue<long long> pq;
for(int i = 0;i<N;i++) {
pq.push(-distX[i]);
pq.push(-distY[i]);
}
long long sum=0;
while(sum<=K and pq.size()) {
sum-=pq.top();
cnt++;
pq.pop();
if(sum<=K)ans=max(ans,cnt);
}
}
// case 2
{
for(int i = 0;i<N;i++) {
if(distX[i]>distY[i])swap(distX[i], distY[i]);
}
int upd[N];
memset(upd, 0, sizeof upd);
for(int i:path) {
upd[i]++;
}
long long dp[N+1][2*N+1];
for(int i = 0;i<=N;i++)
for(int j = 0;j<=2*N;j++)
dp[i][j]=1e18;
dp[0][0]=0;
for(int i = 1;i<=N;i++) {
if(upd[i]==0) {
for(int j = 0;j<=2*N;j++){
dp[i][j]=dp[i-1][j];
}
}
for(int j = 1;j<=2*N;j++) {
dp[i][j]=min(dp[i][j], dp[i-1][j-1]+distX[i-1]);
}
for(int j = 2;j<=2*N;j++) {
dp[i][j]=min(dp[i][j], dp[i-1][j-2]+distY[i-1]);
}
}
for(int j = 0;j<=2*N;j++) {
if(dp[N][j]<=K){
ans=max(ans,j);
}
}
}
for(int i = 0;i<=N;i++) {
dd[i]=0;
distX[i]=distY[i]=0;
g[i].clear();
}
path.clear();
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:42:5: error: 'priority_queue' was not declared in this scope
42 | priority_queue<long long> pq;
| ^~~~~~~~~~~~~~
closing.cpp:6:1: note: 'std::priority_queue' is defined in header '<queue>'; did you forget to '#include <queue>'?
5 | #include<cstring>
+++ |+#include <queue>
6 | using namespace std;
closing.cpp:42:20: error: expected primary-expression before 'long'
42 | priority_queue<long long> pq;
| ^~~~
closing.cpp:44:7: error: 'pq' was not declared in this scope
44 | pq.push(-distX[i]);
| ^~
closing.cpp:48:22: error: 'pq' was not declared in this scope
48 | while(sum<=K and pq.size()) {
| ^~