#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< bool > onpath;
bool DFS(int node,int prnt){
onpath[node] = true;
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;
}
onpath[node] = false;
return false;
}
long long seg[4 * N];
int seg2[4 * N];
void update(int s,int e,int idx,int Idx,long long val,int val2){
if(s > Idx || e < Idx)
return;
if(s == e){
seg[idx] = val;
seg2[idx] = val2;
return;
}
update(s, ((s+e) >> 1) , (idx << 1) , Idx , val , val2);
update(((s+e) >> 1) + 1,e,(idx << 1) + 1 , Idx , val , val2);
seg[idx] = seg[(idx << 1)] + seg[(idx << 1) + 1];
seg2[idx] = seg2[(idx << 1)] + seg2[(idx << 1) + 1];
}
int get(int s,int e,int idx,long long can){
if(can >= seg[idx])
return seg2[idx];
if(s == e)
return 0;
if(can >= seg[(idx << 1)])
return get(((s+e) >> 1) + 1,e,(idx << 1) + 1, can - seg[(idx << 1)]) + seg2[(idx << 1)];
return get(s, ((s+e) >> 1),(idx << 1), can);
}
int max_score(int N, int X, int Y, long long K,std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
n = N, x = X , y = Y , k = K;
for(int i = 0;i < n;i++) g[i].clear();
onpath = vector< bool > (n , 0);
for(int i = 0 ;i < 4 * n;i++) seg[i] = 0;
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);
vector< long long > v;
for(int i = 0 ;i < n;i++){
if(a[i] > b[i]) swap(a[i] , b[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
int ans = 0;
long long cur = 0;
while(ans < (int)v.size() && v[ans] + cur <= k) cur += v[ans++];
cur = 0;
DFS(X , -1);
vector< pair< long long , long long > > v2;
vector< pair< long long , pair< long long , int > > > v3;
priority_queue < long long > q1;
int has = 0;
for(int i = 0 ;i < n;i++){
if(onpath[i]){
cur += a[i];
q1.push(a[i] - b[i]);
has++;
}
else{
//cout << i << " " << a[i] << " " << b[i] << " ready for two with " << b[i] << endl;
v2.push_back(make_pair(b[i] , a[i]));
}
}
if(cur > k) return ans;
sort(v2.begin(),v2.end());
for(int i = 0 ;i < (int)v2.size();i++){
v3.push_back(make_pair(v2[i].second , make_pair(v2[i].first , i)));
}
sort(v3.begin(),v3.end());
reverse(v3.begin(),v3.end());
for(int i = 0 ;i < (int)v3.size();i++){
update(0 , n - 1 , 1, v3[i].second.second , v3[i].second.first , 1);
}
ans = max(ans , has + get(0 , n - 1 , 1 ,k - cur) * 2);
//cout << has << " " << get(0 , n - 1 , 1 ,k - cur) << " " << cur << endl;
while((int)v3.size() > 0 || (int)q1.size() > 0){
if(cur > k) break;
if((int)v3.size() == 0 || (((int)q1.size() > 0) && (-q1.top() < v3.back().first))){
cur -= q1.top();
q1.pop();
has++;
}
else{
cur += v3.back().first;
has++;
q1.push(v3.back().first - v3.back().second.first);
update(0 , n - 1 , 1 , v3.back().second.second , 0 , 0);
v3.pop_back();
}
if(cur > k) break;
// cout << has << " " << get(0 , n - 1 , 1 ,k - cur) << endl;
ans = max(ans , has + get(0 , n - 1 , 1 ,k - cur) * 2);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
39608 KB |
Output is correct |
2 |
Correct |
144 ms |
43408 KB |
Output is correct |
3 |
Correct |
62 ms |
7648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4964 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4964 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Incorrect |
2 ms |
4948 KB |
3rd lines differ - on the 1st token, expected: '32', found: '40' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4964 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Incorrect |
2 ms |
4948 KB |
3rd lines differ - on the 1st token, expected: '32', found: '40' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4968 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4996 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
4948 KB |
Output is correct |
25 |
Correct |
2 ms |
4992 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
2 ms |
4948 KB |
Output is correct |
28 |
Correct |
2 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4968 KB |
Output is correct |
30 |
Correct |
2 ms |
4948 KB |
Output is correct |
31 |
Correct |
2 ms |
4948 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
2 ms |
4996 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
2 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Correct |
2 ms |
4948 KB |
Output is correct |
40 |
Correct |
2 ms |
4948 KB |
Output is correct |
41 |
Correct |
2 ms |
4948 KB |
Output is correct |
42 |
Correct |
2 ms |
4948 KB |
Output is correct |
43 |
Correct |
2 ms |
4948 KB |
Output is correct |
44 |
Correct |
2 ms |
4948 KB |
Output is correct |
45 |
Correct |
2 ms |
4948 KB |
Output is correct |
46 |
Correct |
2 ms |
4948 KB |
Output is correct |
47 |
Correct |
2 ms |
4948 KB |
Output is correct |
48 |
Correct |
2 ms |
4948 KB |
Output is correct |
49 |
Correct |
2 ms |
4948 KB |
Output is correct |
50 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Incorrect |
2 ms |
4948 KB |
3rd lines differ - on the 1st token, expected: '32', found: '40' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Incorrect |
2 ms |
4948 KB |
3rd lines differ - on the 1st token, expected: '32', found: '40' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4964 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Incorrect |
2 ms |
4948 KB |
3rd lines differ - on the 1st token, expected: '32', found: '40' |
20 |
Halted |
0 ms |
0 KB |
- |