#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<int>path;
{
int cur=X;
while (cur!=Y){
path.insert(cur);
cur=p[cur];
}
path.insert(Y);
}
set<pair<ll,int> >st;
vector<pair<ll,int> >v;
ll sum_path=0ll;
ll sum_st=0ll;
vector<ll>u;
for (int i=0;i<N;i++){
u.push_back(min(d[0][i],d[1][i]));
if (!path.count(i)){
st.insert({min(d[0][i],d[1][i]),i});
sum_st+=min(d[0][i],d[1][i]);
} else {
sum_path+=min(d[0][i],d[1][i]);
}
v.push_back({max(d[0][i],d[1][i]),i});
}
sort(u.begin(),u.end());
int ans=0;
{
ll sum=0ll;
for (int i=0;i<N;i++){
sum+=u[i];
if (sum>K) break;
ans++;
}
}
sort(v.begin(),v.end());
int cnt1=0;
ll sum=0ll;
bool bad=false;
for (auto [dist,i]:v){
while (sum_path+sum_st+sum>K && !st.empty()){
auto it=--st.end();
sum_st-=it->first;
st.erase(it);
}
if (sum_path+sum_st+sum>K) {
bad=true;
break;
}
ans=max(ans,cnt1*2+(int)path.size()+(int)st.size());
sum+=dist;
auto it=st.find({min(d[0][i],d[1][i]),i});
if (it!=st.end()){
sum_st-=it->first;
st.erase(it);
} else {
auto it1=path.find(i);
if (it1!=path.end()){
sum_path-=*it1;
path.erase(it1);
}
}
cnt1++;
}
if (!bad && sum<=K){
ans=2*N;
exit(1);
}
if (ans==4) {
if (N<10) exit(1);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
46540 KB |
Output is correct |
2 |
Correct |
203 ms |
53048 KB |
Output is correct |
3 |
Correct |
106 ms |
3068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution failed because the return code was nonzero |
3 |
Halted |
0 ms |
0 KB |
- |