#include "closing.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int n , x , y;
long long k;
const int N = 200010;
vector< pair< int , int > > g[N];
long long a[N] , b[N];
void DFS(int node,int prnt,long long *a,long long dist){
a[node] = dist;
for(int i = 0 ;i < (int)g[node].size();i++){
if(g[node][i].first != prnt)
DFS(g[node][i].first , node , a , dist + g[node][i].second);
}
}
vector< int > path;
bool DFS(int node,int prnt){
path.push_back(node);
if(node == y)
return true;
for(int i = 0 ;i < (int)g[node].size();i++){
if(g[node][i].first == prnt) continue;
if(DFS(g[node][i].first , node))
return true;
}
path.pop_back();
return false;
}
int max_score(int N, int X, int Y, long long K,std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for(int i = 0;i < n;i++) g[i].clear();
path.clear();
n = N, x = X , y = Y , k = K;
for(int i = 0 ;i < (int)U.size();i++){
g[U[i]].push_back(make_pair(V[i] , W[i]));
g[V[i]].push_back(make_pair(U[i] , W[i]));
}
DFS(X , -1 , a , 0);
DFS(Y, - 1, b , 0);
priority_queue < long long > q;
for(int i = 0 ;i < n;i++){
q.push(-min(a[i] , b[i]));
}
int ans = 0;
while(!q.empty()){
k += q.top();
q.pop();
if(k < 0) break;
ans++;
}
DFS(X , -1);
long long cur = 0;
priority_queue < pair< long long , pair< int , bool > > > q2;
long long val;
vector< int > done(n , 0);
int ans2 = (int)path.size();
for(int i = 0 ;i < (int)path.size();i++){
if(a[path[i]] > b[path[i]]){
swap(a[path[i]] , b[path[i]]);
}
cur += a[path[i]];
done[path[i]] = 1;
val = a[path[i]] - b[path[i]];
if(val < 0) val = -val;
q2.push(make_pair(-val * 2, make_pair(path[i] , 0)));
}
for(int i = 0 ;i < n;i++){
if(done[i]) continue;
if(a[i] > b[i]) swap(a[i] , b[i]);
if(a[i] * 2 <= b[i])
q2.push(make_pair(-a[i] * 2 , make_pair(i , 0)));
else
q2.push(make_pair(-b[i] , make_pair(i , 1)));
}
k = K;
if(cur > k)
return ans;
int idx;
while((int)q2.size() > 0){
idx = q2.top().second.first;
if(q2.top().second.second == 1){
q2.pop();
if(cur + b[idx] > k){
q2.push(make_pair(-a[idx] * 2, make_pair(idx , 0)));
continue;
}
ans2 += 2;
done[idx] = 2;
continue;
}
q2.pop();
if(done[idx] == 0){
if(cur + a[idx] <= k){
cur += a[idx];
done[idx] = 1;
q2.push(make_pair((a[idx] - b[idx]) * 2 , make_pair(idx , 0)));
ans2++;
}
}
else{
if(cur + b[idx] - a[idx] <= k){
cur += b[idx] - a[idx];
done[idx] = 2;
ans2++;
}
}
}
//cout << ans2 << " " << cur << endl;
return max(ans , ans2);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
34420 KB |
Output is correct |
2 |
Correct |
135 ms |
40300 KB |
Output is correct |
3 |
Correct |
64 ms |
7752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 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 |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 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 |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4948 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 |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
2 ms |
4948 KB |
1st lines differ - on the 1st token, expected: '30', found: '40' |
4 |
Halted |
0 ms |
0 KB |
- |