#include <bits/stdc++.h>
#include "closing.h"
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<ll,ll>;
using tii = tuple<int,int,int>;
const int nmax = 3e3 + 5;
vector<pii> g[nmax];
vector<ll> dX, dY;
vector<int> occX;
vector<int> chain;
void dfs(int node, int f, vector<ll>& d, ll accum) {
d[node] = accum;
for(auto [x, c] : g[node])
if(x != f)
dfs(x, node, d, accum + c);
return;
}
void findchain(int node, int f, int Y) {
if(sz(chain) && chain.back() == Y) return;
chain.emplace_back(node);
for(auto [x, e] : g[node]) {
if(x == f) continue;
findchain(x, node, Y);
}
if(chain.back() == Y) return;
chain.pop_back();
}
int disjointed(int n, ll K, int X, int Y) {
priority_queue<pii, vector<pii>, greater<pii>> heap;
vector<int> occY(n, 0);
heap.emplace(0, Y);
heap.emplace(0, X);
int scorefromY = 0;
while(!heap.empty()) {
auto [C, node] = heap.top();
K -= C;
if(K < 0) break;
heap.pop();
occY[node] = 1;
scorefromY++;
for(auto [x, e] : g[node]) {
if(occY[x]) continue;
heap.emplace(C + e, x);
}
}
return scorefromY;
}
const ll inf = ((ll)1e18) + 5;
int X, Y;
bitset<nmax> onchain;
ll A[nmax], B[nmax];
namespace DP {
int foutu[nmax];
vector<ll> dp[nmax][2];
int dim[nmax][2];
void clear(int n) {
for(int i = 0; i < n; i++) {
dim[i][0] = dim[i][1] = 0;
dp[i][0].clear();
dp[i][1].clear();
foutu[i] = A[i] = B[i] = 0;
}
}
void convolute(vector<ll> a, vector<ll> b, vector<ll>& c) {
c.resize(max(sz(c), sz(a) + sz(b) - 1), inf);
//cerr << sz(a) << ' ' << sz(b) << ' ' << sz(c) << '\n';
for(int i = 0; i < sz(a); i++)
for(int j = 0; j < sz(b); j++)
c[i + j] = min(c[i + j], a[i] + b[j]);
return;
}
void getdp(int node, int f) { // vectori???
dp[node][0].resize(2, A[node]);
dp[node][1].resize(3, B[node]);
//cerr << node << '\n';
for(auto [x, e] : g[node]) {
if(x == f) continue;
getdp(x, node);
}
for(auto [x, e] : g[node]) {
if(x == f) continue;
vector<ll> tmp;
tmp = dp[node][0];
convolute(dp[node][0], dp[x][0], tmp);
dp[node][0] = move(tmp);
tmp = dp[node][1];
convolute(dp[node][1], dp[x][1], tmp);
convolute(dp[node][1], dp[x][0], tmp);
dp[node][1] = move(tmp);
}
vector<ll> idk;
for(int i = 1; i < sz(dp[node][1]); i++)
if(max({dp[node][1][i], dp[node][1][i - 1]}) < inf)
idk.emplace_back(dp[node][1][i] - dp[node][1][i - 1]);
//for(auto x : idk) cerr << x << ' '; cerr << '\n';
cerr << node << ": " << A[node] << ' ' << B[node] << '\n';
for(int i = 1; i < sz(idk); i++) {
//assert(onchain[i] || idk[i] >= idk[i - 1]);
//if(!(idk[i] >= idk[i - 1])) cerr << "HERE!: ";
cerr << idk[i] << ' ';
}
cerr << '\n';
for(auto x : dp[node][1])cerr << x << ' '; cerr << '\n';
return;
}
int disjointed2(int n, int root, ll K) {
priority_queue<pii, vector<pii>, greater<pii>> heap;
vector<int> occ(n, 0);
occ[root] = 1;
heap.emplace(B[root], root);
int scorefromY = 1;
while(!heap.empty()) {
auto [C, node] = heap.top();
cerr << C << ' ';
K -= C;
if(K < 0) break;
heap.pop();
occ[node]++;
scorefromY++;
if(occ[node] == 1) heap.emplace(B[node], node);
for(auto [x, e] : g[node]) {
if(occ[x]) continue;
heap.emplace(A[x], x);
}
}
cerr << '\n';
return scorefromY;
}
int getRestraint(int n, int root, ll K) {
clear(n);
for(auto x : chain)
K -= min(dX[x], dY[x]);
for(int i = 0; i < n; i++)
A[i] = min(dX[i], dY[i]),
B[i] = max(dX[i], dY[i]);
for(auto x : chain)
A[x] = 0,
B[x] = max(dX[x], dY[x]) - min(dX[x], dY[x]);
if(K < 0) return 0;
//getdp(root, root);
//cerr << "! " << sz(dp[root][1]) << '\n';
//for(int i = sz(dp[root][1]) - 1; i >= 0; i--)
//if(dp[root][1][i] <= K) return i;
return disjointed2(n, root, K) + sz(chain);
}
}
#undef int
int max_score(int n, int _X, int _Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
X = _X;
Y = _Y;
#define int ll
for(int i = 0; i < n; i++) g[i].clear();
chain.clear();
dX.clear();
dY.clear();
occX.clear();
onchain.reset();
for(int i = 0; i < n - 1; i++)
g[U[i]].emplace_back(V[i], W[i]),
g[V[i]].emplace_back(U[i], W[i]);
dX.resize(n);
dY.resize(n);
dfs(X, X, dX, 0);
dfs(Y, Y, dY, 0);
findchain(X, X, Y);
int maxscore = disjointed(n, K, X, Y);
int prv = X;
for(auto x : chain)
onchain[x] = 1;
for(auto x : chain) {
if(dX[x] > dY[x]) { maxscore = max(maxscore, DP::getRestraint(n, prv, K)); break; }
prv = x;
}
prv = Y;
reverse(all(chain));
for(auto x : chain) {
if(dY[x] > dX[x]) { maxscore = max(maxscore, DP::getRestraint(n, prv, K)); break; }
prv = x;
}
return maxscore;
}
#undef int
Compilation message
closing.cpp: In function 'void DP::getdp(ll, ll)':
closing.cpp:145:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
145 | for(auto x : dp[node][1])cerr << x << ' '; cerr << '\n';
| ^~~
closing.cpp:145:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
145 | for(auto x : dp[node][1])cerr << x << ' '; cerr << '\n';
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
49 ms |
13904 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '3', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '3', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '3', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
1st lines differ - on the 1st token, expected: '6', found: '9' |
2 |
Halted |
0 ms |
0 KB |
- |