#include "closing.h"
using namespace std;
#include <bits/stdc++.h>
#define pb push_back
using lli=long long;
int max_score(int N, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W)
{
int ans=0;
vector<vector<pair<int,lli>>> adj (N);
vector<vector<int>> mat (N, vector<int> (N, -1));
for(int i=0; i<N-1; ++i){
adj[U[i]].pb({V[i], W[i]});
adj[V[i]].pb({U[i], W[i]});
mat[U[i]][V[i]]=W[i];
mat[V[i]][U[i]]=W[i];
}
int init=0;
int last=0;
int ant=0;
while(adj[init].size()==2){
if(adj[init][0].first!=ant){
ant=init;
init=adj[init][0].first;
}
else{
ant=init;
init=adj[init][1].first;
}
}
ant=init;
last=adj[init][0].first;
while(adj[last].size()==2){
if(adj[last][0].first!=ant){
ant=last;
last=adj[last][0].first;
}
else{
ant=last;
last=adj[last][1].first;
}
}
vector<int> ord;
int rec=init;
ant=init;
while(rec!=last){
// cout<<"rec: "<<rec<<endl;
ord.pb(rec);
if(adj[rec][0].first!=ant){
ant=rec;
rec=adj[rec][0].first;
}
else{
ant=rec;
rec=adj[rec][1].first;
}
}
ord.pb(rec);
vector<int> numb(N);
for(int i=0; i<N; ++i){
numb[ord[i]]=i;
}
if(numb[X]>numb[Y]){
swap(X,Y);
}
for(int i=numb[X]; i>=0; --i){
vector<int> values (N, 0);
int cont=K;
for(int a=numb[X]-1; a>=i; --a){
values[a]=values[a+1]+mat[ord[a+1]][ord[a]];
cont-=values[a];
}
if(cont<0) break;
for(int j=numb[X]; j<N; ++j){
for(int a=numb[X]+1; a<=j; ++a){
values[a]=values[a-1]+mat[ord[a-1]][ord[a]];
cont-=values[a];
}
if(cont<0) break;
int aux=j-i+1;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
vector<bool> visited (N, false);
pq.push({0, numb[Y]});
while(!pq.empty()){
int a=pq.top().first;
int b=pq.top().second;
pq.pop();
if(visited[b]) continue;
visited[b]=true;
if(cont>=a){
cont-=a;
aux++;
if(b+1<N && !visited[b+1]){
pq.push({a+mat[ord[b]][ord[b+1]]-values[b+1], b+1});
}
if(b-1>=0 && !visited[b-1]){
pq.push({a+mat[ord[b]][ord[b-1]]-values[b-1], b-1});
}
}
else{
break;
}
}
ans=max(ans, aux);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1144 ms |
1980624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |