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>
typedef long long ll;
using namespace std;
vector<vector<pair<int,ll> > >g;
vector<vector<ll> >d;
vector<int>p;
void dfs(int v,int pr,int j){
p[v]=pr;
for (auto [to,w]:g[v]){
if (to==pr) continue;
d[j][to]=d[j][v]+w;
dfs(to,v,j);
}
}
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
g.clear();
d.clear();
p.clear();
g.resize(N+1);
d.resize(2,vector<ll>(N+1,0));
p.resize(N+1,-1);
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]});
}
{
d[0][X]=0;
dfs(X,-1,0);
}
{
d[1][Y]=0;
dfs(Y,-1,1);
}
set<pair<ll,int> >st;
vector<pair<ll,int> >v;
for (int i=0;i<N;i++){
st.insert({min(d[0][i],d[1][i]),i});
v.push_back({max(d[0][i],d[1][i]),i});
}
sort(v.begin(),v.end());
ll sum=0ll;
int ans=2;
int cnt1=0;
for (auto [dist,i]:v){
int cnt2=0;
ll sum2=sum;
for (auto cur:st){
sum2+=cur.first;
if (sum2>K) break;
cnt2++;
}
ans=max(ans,cnt1+cnt2);
sum+=dist;
st.erase({min(d[0][i],d[1][i]),i});
cnt1++;
}
if (sum<=K){
ans=2*N;
}
return 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... |