#include "closing.h"
// #pragma optimize ("O3")
// #pragma target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define ll long long
#define ff first
#define ss second
const int N = 3e5+10;
const ll INF = 1e18;
int sz[N];
vector<pair<int, ll>> g[N];
ll val1[N], val2[N], T[N][2];
void build(int l, int r, int k){
if(l == r){
T[k][0] = 0;
T[k][1] = 0;
return;
}
int m = l+r>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
T[k][0] = 0;
T[k][1] = 0;
}
void upd(int l, int r, int p, int k, ll val, int co){
if(l == r){
T[k][0] = val;
T[k][1] = co;
return;
}
int m = l+r>>1;
if(p <= m) upd(l, m, p, k<<1, val, co);
else upd(m+1, r, p, k<<1|1, val, co);
T[k][0] = T[k<<1][0] + T[k<<1|1][0];
T[k][1] = T[k<<1][1] + T[k<<1|1][1];
}
pair<ll, ll> get(int l, int r, int k, ll R){
if(R >= T[k][0]) return {T[k][0], T[k][1]};
if(l == r){
if(R >= T[k][0]) return {T[k][0], T[k][1]};
return {0ll, 0ll};
}
int m = l+r>>1;
pair<ll, ll> ans = {0ll, 0ll};
if(T[k<<1][0] <= R){
ans = get(l, m, k<<1, R);
}else{
return get(l, m, k<<1, R);
}
R -= ans.ff;
auto x = get(m+1, r, k<<1|1, R);
return {x.ff + ans.ff, x.ss + ans.ss};
}
void dfs(int v, int p, vector<ll> &D){
for(auto U: g[v]){
int u = U.ff;
if(u != p){
D[u] = D[v] + U.ss;
dfs(u, v, D);
}
}
}
vector<int> euler;
bool pre(int v, int p, int t){
euler.pb(v);
if(v == t){
return 1;
}
for(auto U: g[v]){
int u = U.ff;
if(u != p){
bool ok = pre(u, v, t);
if(ok) return 1;
}
}
euler.pop_back();
return 0;
}
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();
euler.clear();
for(int i = 0; i < n - 1; ++i){
g[V[i]].pb({U[i], W[i]});
g[U[i]].pb({V[i], W[i]});
}
if(X > Y) swap(X,Y);
vector<ll> D1(n), D2(n);
dfs(X, X, D1);
dfs(Y, Y, D2);
vector<ll> A;
for(int i =0 ; i < n; ++i) A.pb(D1[i]);
for(int i =0 ; i < n; ++i) A.pb(D2[i]);
sort(all(A));
int anss = 0; ll tot = 0;
for(int i = 0; i < A.size(); ++i){
if(tot + A[i] <= K) tot += A[i], ++anss;
}
pre(X, X, Y);
vector<bool> s(n);
for(int v: euler) s[v] = 1;
vector<array<ll, 3>> vals;
for(int i = 0; i < n; ++i){
if(s[i]){
val1[i] = max(D1[i], D2[i]) - min(D1[i], D2[i]);
val2[i] = -1;
K -= min(D1[i], D2[i]);
vals.pb({2*val1[i], i, 4});
}else{
val1[i] = min(D1[i], D2[i]);
val2[i] = max(D1[i], D2[i]) - val1[i];
vals.pb({2*val1[i], i, 1});
if(val2[i] < val1[i]){
vals.pb({val1[i]+val2[i], i, 3});
}else{
vals.pb({2*val2[i], i, 2});
}
}
}
if(K<0) return anss;
sort(all(vals));
vector<int> used(n);
int d = vals.size();
build(0, d-1, 1);
for(int i = 0; i < vals.size(); ++i){
int idx = vals[i][1];
int co = vals[i][2];
if(co == 3){
upd(0, d-1, i, 1, vals[i][0], 2);
used[idx] = 1;
}else if(co == 4){
upd(0, d-1, i, 1, vals[i][0] / 2, 1);
}else if(co == 2 || (co == 1 && !used[idx])){
upd(0, d-1, i, 1, vals[i][0] / 2, 1);
// cout << vals[i][0] << ' ';
}else if(co == 1 && used[idx]){
used[idx] = i;
}
}
int best = get(0, d-1, 1, K).ss;
for(int i = 0; i < vals.size(); ++i){
int idx = vals[i][1];
int co = vals[i][2];
if(co == 3){
upd(0, d-1, i, 1, 0, 0);
upd(0, d-1, used[idx], 1, val1[idx], 1);
best = max(best, (int)get(0, d-1, 1, K).ss);
upd(0, d-1, used[idx], 1, 0, 0);
upd(0, d-1, i, 1, vals[i][0], 2);
}
}
return max(best+int(euler.size()), anss);
// for(int v: euler){
// p(v, v);
// calc(v, v);
// }
// for(int u: euler){
// for(int j = 2; j <= sz[u]; ++j){
// assert(dp[u][j] - dp[u][j - 1] >= dp[u][j - 1] - dp[u][j - 2]);
// }
// }
// int ans = 0;
// ll ls = 0, rs = 1e18;
// while(ls <= rs){
// ll lm = ls+rs>>1;
// int num = 0;
// ll cost = 0;
// for(int u: euler){
// for(int j = 1; j <= sz[u]; ++j){
// if(dp[u][j] - dp[u][j - 1] <= lm){
// cost += dp[u][j] - dp[u][j - 1];
// ++num;
// if(cost <= K){
// ans = max(ans, num);
// }
// }
// }
// }
// if(cost > K){
// rs = lm - 1;
// }else{
// ls = lm + 1;
// }
// }
// vector<ll> DP(2*n + 5, INF);
// DP[0] = 0;
// int S = 0;
// for(int u: euler){
// S += sz[u];
// for(int j = S; j >= 1; --j){
// for(int l = 1; l <= min(sz[u], j); ++l){
// DP[j] = min(DP[j], DP[j - l] + dp[u][l]);
// }
// }
// }
// for(int i = 2*n; i >= 0; --i){
// if(DP[i] <= K){
// }
// }
// return anss;
}
Compilation message
closing.cpp: In function 'void build(int, int, int)':
closing.cpp:23:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int m = l+r>>1;
| ~^~
closing.cpp: In function 'void upd(int, int, int, int, long long int, int)':
closing.cpp:35:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int m = l+r>>1;
| ~^~
closing.cpp: In function 'std::pair<long long int, long long int> get(int, int, int, long long int)':
closing.cpp:47:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int m = l+r>>1;
| ~^~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:103:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i = 0; i < A.size(); ++i){
| ~~^~~~~~~~~~
closing.cpp:135:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i = 0; i < vals.size(); ++i){
| ~~^~~~~~~~~~~~~
closing.cpp:152:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for(int i = 0; i < vals.size(); ++i){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
54720 KB |
Output is correct |
2 |
Correct |
127 ms |
54712 KB |
Output is correct |
3 |
Correct |
64 ms |
12816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
3 ms |
7256 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7492 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
3 ms |
7256 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7492 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
4 ms |
7492 KB |
Output is correct |
14 |
Correct |
4 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7512 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Incorrect |
4 ms |
7496 KB |
2nd lines differ - on the 1st token, expected: '25', found: '24' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7260 KB |
Output is correct |
3 |
Correct |
3 ms |
7256 KB |
Output is correct |
4 |
Correct |
3 ms |
7260 KB |
Output is correct |
5 |
Correct |
3 ms |
7492 KB |
Output is correct |
6 |
Correct |
4 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
4 ms |
7492 KB |
Output is correct |
14 |
Correct |
4 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7512 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Incorrect |
4 ms |
7496 KB |
2nd lines differ - on the 1st token, expected: '25', found: '24' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
3 ms |
7512 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7256 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7492 KB |
Output is correct |
7 |
Correct |
4 ms |
7512 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Incorrect |
3 ms |
7256 KB |
1st lines differ - on the 1st token, expected: '9', found: '8' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
3 ms |
7512 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7256 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7492 KB |
Output is correct |
7 |
Correct |
4 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
4 ms |
7492 KB |
Output is correct |
15 |
Correct |
4 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Correct |
4 ms |
7512 KB |
Output is correct |
20 |
Correct |
3 ms |
7516 KB |
Output is correct |
21 |
Incorrect |
3 ms |
7256 KB |
1st lines differ - on the 1st token, expected: '9', found: '8' |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
3 ms |
7512 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7256 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7492 KB |
Output is correct |
7 |
Correct |
4 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
4 ms |
7492 KB |
Output is correct |
15 |
Correct |
4 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Incorrect |
4 ms |
7496 KB |
2nd lines differ - on the 1st token, expected: '25', found: '24' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
3 ms |
7512 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7256 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7492 KB |
Output is correct |
7 |
Correct |
4 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
4 ms |
7492 KB |
Output is correct |
15 |
Correct |
4 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Incorrect |
4 ms |
7496 KB |
2nd lines differ - on the 1st token, expected: '25', found: '24' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7256 KB |
Output is correct |
2 |
Correct |
3 ms |
7512 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7256 KB |
Output is correct |
5 |
Correct |
3 ms |
7260 KB |
Output is correct |
6 |
Correct |
3 ms |
7492 KB |
Output is correct |
7 |
Correct |
4 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7516 KB |
Output is correct |
14 |
Correct |
4 ms |
7492 KB |
Output is correct |
15 |
Correct |
4 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7512 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7516 KB |
Output is correct |
19 |
Incorrect |
4 ms |
7496 KB |
2nd lines differ - on the 1st token, expected: '25', found: '24' |
20 |
Halted |
0 ms |
0 KB |
- |