#include "closing.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<tuple>
using namespace std;
const int MAX_N=2e5+5;
const long long INF=(1LL<<60);
int n,x,y;
long long k;
vector<pair<int,int>>g[MAX_N];
long long ans;
bool usedline[MAX_N];
long long bfsdist[MAX_N];
void bfs()
{
for(int i=0;i<n;i++)bfsdist[i]=INF;
long long rem=k;
long long curans=0;
bfsdist[x]=0;
bfsdist[y]=0;
set<pair<long long,int>>q;
q.insert({0,x});
q.insert({0,y});
while(q.size())
{
int u,curdist;
tie(curdist,u)=(*(q.begin()));
if(rem>=curdist)
{
rem-=curdist;
curans++;
}
else break;
q.erase({curdist,u});
for(auto [v,edge]:g[u])
{
long long len=bfsdist[u]+edge;
if(len<bfsdist[v])
{
q.erase({bfsdist[v],v});
bfsdist[v]=len;
q.insert({bfsdist[v],v});
}
}
}
ans=max(ans,curans);
}
void line()
{
for(int i=0;i<n;i++)usedline[i]=0;
usedline[x]=1;
usedline[y]=1;
long long distxy=0;
int u;
for(int i=0;i<n;i++)
{
if(g[i].size()==1)u=i;
}
int cntxy=0;
int prev=-1;
while(1)
{
if(u==x or u==y)
{
cntxy++;
}
int nu=u;
for(auto [v,edge]:g[u])
{
if(v==prev)continue;
if(cntxy==1){usedline[v]=1;distxy+=edge;}
nu=v;
}
if(nu==u)break;
prev=u;
u=nu;
}
///calculate dist between x and y
multiset<long long>s;
long long rem=k;
cntxy=0;
prev=-1;
long long curdist=0;
while(1)
{
if(u==x or u==y)
{
cntxy++;
if(cntxy==1)
{
s.insert(distxy);
}
}
int nu=u;
for(auto [v,edge]:g[u])
{
if(v==prev)continue;
if(cntxy==1)
{
curdist+=edge;
if(curdist<=distxy-curdist)
{
rem-=curdist;
s.insert(-curdist+(distxy-curdist));
}
else
{
rem-=(distxy-curdist);
s.insert(-(distxy-curdist)+curdist);
}
}
nu=v;
}
if(nu==u)break;
prev=u;
u=nu;
}
if(rem>=0)
{
long long curans=s.size();
long long dist;
dist=0;
u=x;
while(g[u].size()>1)
{
for(auto [v,edge]:g[u])
{
if(usedline[v])continue;
usedline[v]=1;
dist+=edge;
s.insert(dist);
u=v;
break;
}
}
dist=0;
u=y;
while(g[u].size()>1)
{
for(auto [v,edge]:g[u])
{
if(usedline[v])continue;
usedline[v]=1;
dist+=edge;
s.insert(dist);
u=v;
break;
}
}
while(s.size())
{
long long top=(*(s.begin()));
if(rem>=top)
{
rem-=top;
curans++;
}
else break;
s.erase(s.find(top));
}
ans=max(ans,curans);
}
///take at least one from both
}
int max_score(int N, int X, int Y, long long K,std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
ans=2;
n=N;
x=X;
y=Y;
k=K;
int maxdeg=0;
for(int i=0;i<n;i++)g[i].clear();
for(int i=0;i<n-1;i++)
{
int u,v,w;
u=U[i];
v=V[i];
w=W[i];
g[u].push_back({v,w});
g[v].push_back({u,w});
}
for(int i=0;i<n;i++)
{
maxdeg=max(maxdeg,(int)g[i].size());
}
bfs();
if(maxdeg<=2)line();
else
{
}
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... |