#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18 + 1;
int max_score(int n, int x, int y, ll k, vector<int> u, vector<int> v, vector<int> w)
{
x++;
y++;
vector<vector<pair<int, int>>> g(n + 1);
for (int i = 0; i < n - 1; ++i)
{
u[i]++;
v[i]++;
g[u[i]].push_back({v[i], w[i]});
g[v[i]].push_back({u[i], w[i]});
}
vector<vector<ll>> cost(n + 1, vector<ll>(2));
function<void(int, int, int)> compute_cost = [&](int node, int parent, int qui)
{
for (auto i : g[node])
{
if (i.first != parent)
{
cost[i.first][qui] = cost[node][qui] + i.second;
compute_cost(i.first, node, qui);
}
}
};
compute_cost(x, 0, 0);
compute_cost(y, 0, 1);
function<int()> greedy = [&]()
{
set<tuple<ll, int, int>> accesibile;
vector<vector<int>> viz(n + 1, vector<int>(2));
accesibile.insert({0, x, 0});
accesibile.insert({0, y, 1});
int ans = 0;
ll left = k;
while (!accesibile.empty())
{
int adam, qui, byqui;
tie(adam, qui, byqui) = *accesibile.begin();
accesibile.erase(accesibile.begin());
if (adam > left)
{
break;
}
left -= adam;
ans++;
viz[qui][byqui] = true;
for (auto i : g[qui])
{
if (!viz[i.first][byqui])
{
accesibile.insert({cost[i.first][byqui], i.first, byqui});
}
}
}
return ans;
};
function<int(int)> solve_root = [&](int root)
{
vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(n + n + 1, vector<ll>(3, inf)));
vector<bool> important(n + 1);
vector<int> sz(n + 1);
function<void(int, int)> dfs_important = [&](int node, int parent)
{
if (node == x || node == y)
{
important[node] = true;
}
for (auto i : g[node])
{
if (i.first != parent)
{
dfs_important(i.first, node);
}
}
for (auto i : g[node])
{
if (i.first != parent)
{
important[node] = important[node] || important[i.first];
}
}
};
dfs_important(root, 0);
vector<ll> merged(n + n + 1, inf);
function<void(int, int)> dfs = [&](int node, int parent)
{
dp[node][1][0] = cost[node][0];
dp[node][1][1] = cost[node][1];
dp[node][2][2] = max(cost[node][0], cost[node][1]);
sz[node] = 1;
if (node == x)
{
dp[node][1][1] = inf;
}
if (node == y)
{
dp[node][1][0] = inf;
}
for (auto i : g[node])
{
if (i.first != parent)
{
dfs(i.first, node);
int maxleft = sz[node] + sz[node];
int maxright = sz[i.first] + sz[i.first];
for (int i = 0; i <= maxleft + maxright; ++i)
{
merged[i] = inf;
}
for (int color = 0; color <= 2; ++color)
{
for (int color2 = 0; color2 <= 2; ++color2)
{
if (color != color2 && color != 2)
{
continue;
}
for (int cntleft = 1; cntleft <= maxleft; ++cntleft)
{
if (dp[node][cntleft][color] > k)
{
continue;
}
for (int cntright = 1; cntright <= maxright; ++cntright)
{
merged[cntleft + cntright] = min(merged[cntleft + cntright], dp[node][cntleft][color] + dp[i.first][cntright][color2]);
}
}
}
for (int cnt = 1; cnt <= maxleft + maxright; ++cnt)
{
if (important[i.first])
{
dp[node][cnt][color] = merged[cnt];
}
else
{
dp[node][cnt][color] = min(dp[node][cnt][color], merged[cnt]);
}
}
}
sz[node] += sz[i.first];
}
}
};
dfs(root, 0);
for (int i = n + n; i >= 1; --i)
{
if (dp[root][i][2] <= k)
{
return i;
}
}
return 0;
};
int ans = greedy();
ll mini = inf;
int qui = 0;
for (int i = 1; i <= n; ++i)
{
ll val = abs(cost[i][0] - cost[i][1]);
if (val < mini)
{
mini = val;
qui = i;
}
else
{
if (val == mini && cost[i][0] < cost[qui][0])
{
qui = i;
}
}
}
ans = max(ans, solve_root(qui));
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1119 ms |
1612716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1380 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
34 ms |
27992 KB |
Output is correct |
20 |
Correct |
24 ms |
23392 KB |
Output is correct |
21 |
Correct |
28 ms |
25608 KB |
Output is correct |
22 |
Correct |
33 ms |
27904 KB |
Output is correct |
23 |
Correct |
35 ms |
27996 KB |
Output is correct |
24 |
Correct |
30 ms |
27996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
848 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1380 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
34 ms |
27992 KB |
Output is correct |
20 |
Correct |
24 ms |
23392 KB |
Output is correct |
21 |
Correct |
28 ms |
25608 KB |
Output is correct |
22 |
Correct |
33 ms |
27904 KB |
Output is correct |
23 |
Correct |
35 ms |
27996 KB |
Output is correct |
24 |
Correct |
30 ms |
27996 KB |
Output is correct |
25 |
Correct |
13 ms |
772 KB |
Output is correct |
26 |
Execution timed out |
1128 ms |
998220 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
436 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
436 KB |
Output is correct |
17 |
Correct |
0 ms |
612 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
848 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1380 KB |
Output is correct |
19 |
Correct |
0 ms |
436 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
440 KB |
Output is correct |
26 |
Correct |
1 ms |
440 KB |
Output is correct |
27 |
Correct |
0 ms |
352 KB |
Output is correct |
28 |
Correct |
0 ms |
436 KB |
Output is correct |
29 |
Correct |
0 ms |
612 KB |
Output is correct |
30 |
Correct |
1 ms |
440 KB |
Output is correct |
31 |
Correct |
1 ms |
356 KB |
Output is correct |
32 |
Correct |
0 ms |
352 KB |
Output is correct |
33 |
Correct |
1 ms |
352 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
2 ms |
1372 KB |
Output is correct |
40 |
Correct |
2 ms |
1480 KB |
Output is correct |
41 |
Correct |
1 ms |
1372 KB |
Output is correct |
42 |
Correct |
1 ms |
1372 KB |
Output is correct |
43 |
Correct |
1 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1372 KB |
Output is correct |
46 |
Correct |
2 ms |
1368 KB |
Output is correct |
47 |
Correct |
2 ms |
1372 KB |
Output is correct |
48 |
Correct |
1 ms |
1056 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
848 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1380 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
34 ms |
27992 KB |
Output is correct |
21 |
Correct |
24 ms |
23392 KB |
Output is correct |
22 |
Correct |
28 ms |
25608 KB |
Output is correct |
23 |
Correct |
33 ms |
27904 KB |
Output is correct |
24 |
Correct |
35 ms |
27996 KB |
Output is correct |
25 |
Correct |
30 ms |
27996 KB |
Output is correct |
26 |
Correct |
0 ms |
436 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
436 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
440 KB |
Output is correct |
33 |
Correct |
1 ms |
440 KB |
Output is correct |
34 |
Correct |
0 ms |
352 KB |
Output is correct |
35 |
Correct |
0 ms |
436 KB |
Output is correct |
36 |
Correct |
0 ms |
612 KB |
Output is correct |
37 |
Correct |
1 ms |
440 KB |
Output is correct |
38 |
Correct |
1 ms |
356 KB |
Output is correct |
39 |
Correct |
0 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
2 ms |
1372 KB |
Output is correct |
47 |
Correct |
2 ms |
1480 KB |
Output is correct |
48 |
Correct |
1 ms |
1372 KB |
Output is correct |
49 |
Correct |
1 ms |
1372 KB |
Output is correct |
50 |
Correct |
1 ms |
1372 KB |
Output is correct |
51 |
Correct |
2 ms |
1372 KB |
Output is correct |
52 |
Correct |
2 ms |
1372 KB |
Output is correct |
53 |
Correct |
2 ms |
1368 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
1 ms |
1056 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
4 ms |
1372 KB |
Output is correct |
60 |
Correct |
5 ms |
1372 KB |
Output is correct |
61 |
Correct |
6 ms |
2908 KB |
Output is correct |
62 |
Correct |
6 ms |
2744 KB |
Output is correct |
63 |
Correct |
29 ms |
27996 KB |
Output is correct |
64 |
Correct |
25 ms |
24412 KB |
Output is correct |
65 |
Correct |
23 ms |
23048 KB |
Output is correct |
66 |
Correct |
29 ms |
24752 KB |
Output is correct |
67 |
Correct |
27 ms |
27484 KB |
Output is correct |
68 |
Correct |
23 ms |
23248 KB |
Output is correct |
69 |
Correct |
29 ms |
27484 KB |
Output is correct |
70 |
Correct |
26 ms |
25692 KB |
Output is correct |
71 |
Correct |
25 ms |
25180 KB |
Output is correct |
72 |
Correct |
27 ms |
26216 KB |
Output is correct |
73 |
Correct |
19 ms |
17244 KB |
Output is correct |
74 |
Correct |
19 ms |
19128 KB |
Output is correct |
75 |
Correct |
4 ms |
1460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
848 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1380 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
34 ms |
27992 KB |
Output is correct |
21 |
Correct |
24 ms |
23392 KB |
Output is correct |
22 |
Correct |
28 ms |
25608 KB |
Output is correct |
23 |
Correct |
33 ms |
27904 KB |
Output is correct |
24 |
Correct |
35 ms |
27996 KB |
Output is correct |
25 |
Correct |
30 ms |
27996 KB |
Output is correct |
26 |
Correct |
13 ms |
772 KB |
Output is correct |
27 |
Execution timed out |
1128 ms |
998220 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
848 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1368 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
1380 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
34 ms |
27992 KB |
Output is correct |
21 |
Correct |
24 ms |
23392 KB |
Output is correct |
22 |
Correct |
28 ms |
25608 KB |
Output is correct |
23 |
Correct |
33 ms |
27904 KB |
Output is correct |
24 |
Correct |
35 ms |
27996 KB |
Output is correct |
25 |
Correct |
30 ms |
27996 KB |
Output is correct |
26 |
Correct |
13 ms |
772 KB |
Output is correct |
27 |
Execution timed out |
1128 ms |
998220 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |