//#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ii pair<int,long long>
#define ll long long
const long long inf=1e18+7;
using Graph = vector<vector<pair<int, long long>>> ;
template<typename T>
bool maximize(T &a,T b)
{
if(a<b)
{
a=b;
return true;
}
return false;
}
void dfs(int x,int par,Graph &edge,vector<ll>&dist)
{
for(ii need:edge[x])
{
int node=need.x;
ll value=need.y;
if(node==par)continue;
dist[node]=dist[x]+value;
dfs(node,x,edge,dist);
}
}
int Sub1(int N,long long K,vector<ll>distX,vector<ll>distY)
{
vector<ll>que;
for(int i=1; i<=N; i++)
{
que.push_back(distX[i]);
que.push_back(distY[i]);
}
sort(que.begin(),que.end());
ll res=K;
int ans=0;
for(int value:que)
{
if(res<value)break;
res-=value;
ans++;
}
return ans;
}
bool checkLinear(int N,Graph &edge,vector<int>°)
{
int maxv=0;
for(int i=1;i<=N;i++)
{
for(ii need:edge[i])
{
int node=need.x;
deg[node]++;
maxv=max(maxv,deg[node]);
}
}
return maxv<=2;
}
int Sub2(int N,int X,int Y,long long K,Graph &edge,vector<int>deg,vector<long long>distX,vector<long long>distY)
{
int need_node;
if(deg[X]>deg[Y])swap(X,Y);
for(int i=1;i<=N;i++)if(deg[i]==1)
{
need_node=i;
break;
}
vector<int>beg(N+1,0);
vector<int>pos(N+1);
beg[need_node]=1;
queue<int>q;
q.push(need_node);
while(!q.empty())
{
int x=q.front();
pos[beg[x]]=x;
q.pop();
for(ii need:edge[x])
{
int node=need.x;
int value=need.y;
if(!beg[node])
{
beg[node]=beg[x]+1;
q.push(node);
}
}
}
vector<long long>sumX(N+1);
vector<long long>sumY(N+1);
for(int i=1;i<=N;i++)
{
sumX[i]=sumX[i-1]+min(distX[i],distY[i]);
sumY[i]=sumY[i-1]+max(distX[i],distY[i]);
}
vector<int>que;
for(int i=1;i<=N;i++)
{
que.push_back(distX[i]);
que.push_back(distY[i]);
}
sort(que.begin(),que.end());
int ans=0;
long long res=K;
for(long long value:que)
{
if(value>res)break;
ans++;
res-=value;
}
for(int i=1;i<=N;i++)
{
for(int j=i;j<=N;j++)
{
long long value=sumY[j]-sumY[i-1];
int total=(j-i+1)*2;
if(i-1>=beg[X])
{
total+=(i-beg[X]);
value+=sumX[i-1]-sumX[beg[X]-1];
}
if(j<=beg[Y])
{
total+=(beg[Y]-j);
value+=sumX[beg[Y]]-sumX[j];
}
if(value>K)continue;
vector<long long>block;
int posX=min(beg[X],i);
int posY=max(beg[Y],j);
for(int cnt=1;cnt<=posX-1;cnt++)block.push_back(distX[pos[cnt]]-distX[pos[posX]]);
for(int cnt=posY+1;cnt<=N;cnt++)block.push_back(distX[pos[cnt]]-distX[pos[posY]]);
sort(block.begin(),block.end());
long long used=K-value;
for(long long need:block)
{
if(used<need)break;
total+=1;
used-=need;
}
ans=max(ans,total);
}
}
return ans;
}
int max_score(int N,int X,int Y,long long K,vector<int>U,vector<int>V,vector<int>W)
{
X+=1;
Y+=1;
Graph edge(N+1);
for(int i=1; i<=N-1; i++)
{
int x=U[i-1]+1;
int y=V[i-1]+1;
int value=W[i-1];
edge[x].push_back({y,value});
edge[y].push_back({x,value});
}
vector<ll>distX(N+1);
vector<ll>distY(N+1);
dfs(X,0,edge,distX);
dfs(Y,0,edge,distY);
if(distX[Y]>2*K)return Sub1(N,K,distX,distY);
vector<int>deg(N+1);
if(checkLinear(N,edge,deg))return Sub2(N,X,Y,K,edge,deg,distX,distY);
}
/*signed main()
{
int n,x,y,k;
vector<int>U;
vector<int>V;
vector<int>W;
cin>>n>>x>>y>>k;
for(int i=1; i<=n-1; i++)
{
int x;
cin>>x;
U.push_back(x);
}
for(int i=1; i<=n-1; i++)
{
int y;
cin>>y;
V.push_back(y);
}
for(int i=1; i<=n-1; i++)
{
int value;
cin>>value;
W.push_back(value);
}
cout<<max_score(n,x,y,k,U,V,W);
}*/
Compilation message
closing.cpp: In function 'int Sub2(int, int, int, long long int, Graph&, std::vector<int>, std::vector<long long int>, std::vector<long long int>)':
closing.cpp:87:17: warning: unused variable 'value' [-Wunused-variable]
87 | int value=need.y;
| ^~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:158:19: warning: control reaches end of non-void function [-Wreturn-type]
158 | Graph edge(N+1);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
36444 KB |
Output is correct |
2 |
Correct |
142 ms |
42324 KB |
Output is correct |
3 |
Correct |
75 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '34' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '34' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '34' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |