#include "closing.h"
#include <bits/stdc++.h>
// #include <vector>
using namespace std;
using ll = long long;
// Subtask Checker
class CheckSub1
{
private:
vector<vector<pair<int, ll>>> adj;
vector<bool> vis;
vector<ll> dist;
void DFS(int u, ll dd)
{
vis[u] = 1;
dist[u] = dd;
for (auto [v, w] : adj[u])
{
if (vis[v])
continue;
DFS(v, dd + w);
}
}
public:
bool isSub1(int N, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W)
{
adj.resize(N + 3);
dist.resize(N + 3);
vis.assign(N + 3, 0);
for (int i = 0; i < N - 1; i++)
{
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
DFS(X, 0);
return (dist[Y] > 2 * K);
}
};
/*
Subtask 1 :
*/
class Subtask1
{
private:
vector<vector<pair<int, ll>>> adj;
vector<bool> vis;
void DFS(int u, ll dd, vector<ll> &dist)
{
vis[u] = 1;
dist[u] = dd;
for (auto [v, w] : adj[u])
{
if (vis[v])
continue;
DFS(v, dd + w, dist);
}
}
public:
int getsol(int N, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W)
{
adj.resize(N + 3);
for (int i = 0; i < N - 1; i++)
{
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
vector<ll> dist1(N + 3);
vis.assign(N + 3, 0);
DFS(X, 0, dist1);
vector<ll> dist2(N + 3);
vis.assign(N + 3, 0);
DFS(Y, 0, dist2);
vector<ll> adist;
for (int i = 0; i < N; i++)
{
adist.push_back(dist1[i]);
adist.push_back(dist2[i]);
}
sort(adist.begin(), adist.end());
int pt = 0, ans = 0;
ll cost = 0;
while (pt < adist.size() && cost + adist[pt] <= K)
{
cost += adist[pt];
pt++, ans++;
}
return ans;
}
};
// Subtask2-8
class Subtask2
{
private:
vector<vector<pair<int, ll>>> adj;
vector<bool> vis;
void DFS(int u, ll dd, vector<ll> &dist, vector<int> &trace)
{
vis[u] = 1;
dist[u] = dd;
for (auto [v, w] : adj[u])
{
if (vis[v])
continue;
trace[v] = u;
DFS(v, dd + w, dist, trace);
}
}
public:
int getsol(int N, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W)
{
adj.resize(N + 3);
for (int i = 0; i < N - 1; i++)
{
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
if (X > Y)
swap(X, Y);
vector<ll> dist1(N + 3);
vector<int> trace1(N + 3);
vis.assign(N + 3, 0);
DFS(X, 0, dist1, trace1);
vector<ll> dist2(N + 3);
vector<int> trace2(N + 3);
vis.assign(N + 3, 0);
DFS(Y, 0, dist2, trace2);
Subtask1 solver;
ll ans = solver.getsol(N, X, Y, K, U, V, W);
vector<bool> pth(N + 3, 0);
int gg = Y;
while (gg != X)
{
pth[gg] = 1;
gg = trace1[gg];
}
vector<vector<ll>> dp(4000, vector<ll>(8000, 1e18));
dp[0][0] = 0;
for (int i = 0; i < N; i++)
if (pth[i])
for (int j = 0; j <= i * 2; j++)
{
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + min(dist1[i], dist2[i]));
dp[i + 1][j + 2] = min(dp[i + 1][j + 2], dp[i][j] + max(dist1[i], dist2[i]));
}
else
for (int j = 0; j <= i * 2; j++)
{
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + min(dist1[i], dist2[i]));
dp[i + 1][j + 2] = min(dp[i + 1][j + 2], dp[i][j] + max(dist1[i], dist2[i]));
}
for (int i = 0; i <= 2 * N; i++)
{
if (dp[N][i] <= K)
ans = max(ans, (ll)(i));
}
return ans;
}
};
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
CheckSub1 check;
bool isSub1 = check.isSub1(N, X, Y, K, U, V, W);
if (isSub1)
{
Subtask1 sub1;
return sub1.getsol(N, X, Y, K, U, V, W);
}
else if (N <= 3000 && !isSub1)
{
Subtask2 sub2;
return sub2.getsol(N, X, Y, K, U, V, W);
}
}
Compilation message
closing.cpp: In member function 'int Subtask1::getsol(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:91:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while (pt < adist.size() && cost + adist[pt] <= K)
| ~~~^~~~~~~~~~~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:182:15: warning: control reaches end of non-void function [-Wreturn-type]
182 | CheckSub1 check;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
160 ms |
50460 KB |
Output is correct |
2 |
Correct |
195 ms |
53156 KB |
Output is correct |
3 |
Correct |
93 ms |
3044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
251296 KB |
Output is correct |
2 |
Correct |
81 ms |
250960 KB |
Output is correct |
3 |
Correct |
82 ms |
250964 KB |
Output is correct |
4 |
Correct |
89 ms |
250960 KB |
Output is correct |
5 |
Correct |
84 ms |
250880 KB |
Output is correct |
6 |
Correct |
84 ms |
251052 KB |
Output is correct |
7 |
Correct |
85 ms |
251224 KB |
Output is correct |
8 |
Correct |
81 ms |
250956 KB |
Output is correct |
9 |
Correct |
82 ms |
250960 KB |
Output is correct |
10 |
Correct |
82 ms |
251024 KB |
Output is correct |
11 |
Correct |
83 ms |
251048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
251296 KB |
Output is correct |
2 |
Correct |
81 ms |
250960 KB |
Output is correct |
3 |
Correct |
82 ms |
250964 KB |
Output is correct |
4 |
Correct |
89 ms |
250960 KB |
Output is correct |
5 |
Correct |
84 ms |
250880 KB |
Output is correct |
6 |
Correct |
84 ms |
251052 KB |
Output is correct |
7 |
Correct |
85 ms |
251224 KB |
Output is correct |
8 |
Correct |
81 ms |
250956 KB |
Output is correct |
9 |
Correct |
82 ms |
250960 KB |
Output is correct |
10 |
Correct |
82 ms |
251024 KB |
Output is correct |
11 |
Correct |
83 ms |
251048 KB |
Output is correct |
12 |
Correct |
82 ms |
250992 KB |
Output is correct |
13 |
Correct |
83 ms |
250964 KB |
Output is correct |
14 |
Correct |
83 ms |
250960 KB |
Output is correct |
15 |
Correct |
89 ms |
251284 KB |
Output is correct |
16 |
Correct |
84 ms |
250960 KB |
Output is correct |
17 |
Correct |
82 ms |
250960 KB |
Output is correct |
18 |
Execution timed out |
1052 ms |
251256 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
251296 KB |
Output is correct |
2 |
Correct |
81 ms |
250960 KB |
Output is correct |
3 |
Correct |
82 ms |
250964 KB |
Output is correct |
4 |
Correct |
89 ms |
250960 KB |
Output is correct |
5 |
Correct |
84 ms |
250880 KB |
Output is correct |
6 |
Correct |
84 ms |
251052 KB |
Output is correct |
7 |
Correct |
85 ms |
251224 KB |
Output is correct |
8 |
Correct |
81 ms |
250956 KB |
Output is correct |
9 |
Correct |
82 ms |
250960 KB |
Output is correct |
10 |
Correct |
82 ms |
251024 KB |
Output is correct |
11 |
Correct |
83 ms |
251048 KB |
Output is correct |
12 |
Correct |
82 ms |
250992 KB |
Output is correct |
13 |
Correct |
83 ms |
250964 KB |
Output is correct |
14 |
Correct |
83 ms |
250960 KB |
Output is correct |
15 |
Correct |
89 ms |
251284 KB |
Output is correct |
16 |
Correct |
84 ms |
250960 KB |
Output is correct |
17 |
Correct |
82 ms |
250960 KB |
Output is correct |
18 |
Execution timed out |
1052 ms |
251256 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
2 |
Correct |
83 ms |
251296 KB |
Output is correct |
3 |
Correct |
81 ms |
250960 KB |
Output is correct |
4 |
Correct |
82 ms |
250964 KB |
Output is correct |
5 |
Correct |
89 ms |
250960 KB |
Output is correct |
6 |
Correct |
84 ms |
250880 KB |
Output is correct |
7 |
Correct |
82 ms |
250928 KB |
Output is correct |
8 |
Correct |
82 ms |
251096 KB |
Output is correct |
9 |
Correct |
83 ms |
251220 KB |
Output is correct |
10 |
Correct |
89 ms |
250976 KB |
Output is correct |
11 |
Correct |
83 ms |
250960 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
82 ms |
250960 KB |
Output is correct |
14 |
Correct |
82 ms |
250960 KB |
Output is correct |
15 |
Correct |
82 ms |
250960 KB |
Output is correct |
16 |
Correct |
85 ms |
250908 KB |
Output is correct |
17 |
Correct |
84 ms |
250960 KB |
Output is correct |
18 |
Correct |
82 ms |
250960 KB |
Output is correct |
19 |
Correct |
84 ms |
250960 KB |
Output is correct |
20 |
Correct |
88 ms |
251216 KB |
Output is correct |
21 |
Correct |
83 ms |
250960 KB |
Output is correct |
22 |
Correct |
83 ms |
250960 KB |
Output is correct |
23 |
Correct |
83 ms |
250964 KB |
Output is correct |
24 |
Correct |
90 ms |
250960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
2 |
Correct |
83 ms |
251296 KB |
Output is correct |
3 |
Correct |
81 ms |
250960 KB |
Output is correct |
4 |
Correct |
82 ms |
250964 KB |
Output is correct |
5 |
Correct |
89 ms |
250960 KB |
Output is correct |
6 |
Correct |
84 ms |
250880 KB |
Output is correct |
7 |
Correct |
84 ms |
251052 KB |
Output is correct |
8 |
Correct |
85 ms |
251224 KB |
Output is correct |
9 |
Correct |
81 ms |
250956 KB |
Output is correct |
10 |
Correct |
82 ms |
250960 KB |
Output is correct |
11 |
Correct |
82 ms |
251024 KB |
Output is correct |
12 |
Correct |
83 ms |
251048 KB |
Output is correct |
13 |
Correct |
82 ms |
250992 KB |
Output is correct |
14 |
Correct |
83 ms |
250964 KB |
Output is correct |
15 |
Correct |
83 ms |
250960 KB |
Output is correct |
16 |
Correct |
89 ms |
251284 KB |
Output is correct |
17 |
Correct |
84 ms |
250960 KB |
Output is correct |
18 |
Correct |
82 ms |
250960 KB |
Output is correct |
19 |
Correct |
82 ms |
250928 KB |
Output is correct |
20 |
Correct |
82 ms |
251096 KB |
Output is correct |
21 |
Correct |
83 ms |
251220 KB |
Output is correct |
22 |
Correct |
89 ms |
250976 KB |
Output is correct |
23 |
Correct |
83 ms |
250960 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
82 ms |
250960 KB |
Output is correct |
26 |
Correct |
82 ms |
250960 KB |
Output is correct |
27 |
Correct |
82 ms |
250960 KB |
Output is correct |
28 |
Correct |
85 ms |
250908 KB |
Output is correct |
29 |
Correct |
84 ms |
250960 KB |
Output is correct |
30 |
Correct |
82 ms |
250960 KB |
Output is correct |
31 |
Correct |
84 ms |
250960 KB |
Output is correct |
32 |
Correct |
88 ms |
251216 KB |
Output is correct |
33 |
Correct |
83 ms |
250960 KB |
Output is correct |
34 |
Correct |
83 ms |
250960 KB |
Output is correct |
35 |
Correct |
83 ms |
250964 KB |
Output is correct |
36 |
Correct |
90 ms |
250960 KB |
Output is correct |
37 |
Correct |
423 ms |
251304 KB |
Output is correct |
38 |
Correct |
165 ms |
251000 KB |
Output is correct |
39 |
Correct |
82 ms |
250900 KB |
Output is correct |
40 |
Correct |
83 ms |
250960 KB |
Output is correct |
41 |
Correct |
82 ms |
250960 KB |
Output is correct |
42 |
Correct |
84 ms |
251028 KB |
Output is correct |
43 |
Correct |
84 ms |
250964 KB |
Output is correct |
44 |
Correct |
83 ms |
250960 KB |
Output is correct |
45 |
Correct |
83 ms |
250956 KB |
Output is correct |
46 |
Correct |
84 ms |
250960 KB |
Output is correct |
47 |
Correct |
83 ms |
250960 KB |
Output is correct |
48 |
Correct |
83 ms |
251124 KB |
Output is correct |
49 |
Correct |
165 ms |
251004 KB |
Output is correct |
50 |
Correct |
414 ms |
251260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
2 |
Correct |
83 ms |
251296 KB |
Output is correct |
3 |
Correct |
81 ms |
250960 KB |
Output is correct |
4 |
Correct |
82 ms |
250964 KB |
Output is correct |
5 |
Correct |
89 ms |
250960 KB |
Output is correct |
6 |
Correct |
84 ms |
250880 KB |
Output is correct |
7 |
Correct |
84 ms |
251052 KB |
Output is correct |
8 |
Correct |
85 ms |
251224 KB |
Output is correct |
9 |
Correct |
81 ms |
250956 KB |
Output is correct |
10 |
Correct |
82 ms |
250960 KB |
Output is correct |
11 |
Correct |
82 ms |
251024 KB |
Output is correct |
12 |
Correct |
83 ms |
251048 KB |
Output is correct |
13 |
Correct |
82 ms |
250992 KB |
Output is correct |
14 |
Correct |
83 ms |
250964 KB |
Output is correct |
15 |
Correct |
83 ms |
250960 KB |
Output is correct |
16 |
Correct |
89 ms |
251284 KB |
Output is correct |
17 |
Correct |
84 ms |
250960 KB |
Output is correct |
18 |
Correct |
82 ms |
250960 KB |
Output is correct |
19 |
Execution timed out |
1052 ms |
251256 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
2 |
Correct |
83 ms |
251296 KB |
Output is correct |
3 |
Correct |
81 ms |
250960 KB |
Output is correct |
4 |
Correct |
82 ms |
250964 KB |
Output is correct |
5 |
Correct |
89 ms |
250960 KB |
Output is correct |
6 |
Correct |
84 ms |
250880 KB |
Output is correct |
7 |
Correct |
84 ms |
251052 KB |
Output is correct |
8 |
Correct |
85 ms |
251224 KB |
Output is correct |
9 |
Correct |
81 ms |
250956 KB |
Output is correct |
10 |
Correct |
82 ms |
250960 KB |
Output is correct |
11 |
Correct |
82 ms |
251024 KB |
Output is correct |
12 |
Correct |
83 ms |
251048 KB |
Output is correct |
13 |
Correct |
82 ms |
250992 KB |
Output is correct |
14 |
Correct |
83 ms |
250964 KB |
Output is correct |
15 |
Correct |
83 ms |
250960 KB |
Output is correct |
16 |
Correct |
89 ms |
251284 KB |
Output is correct |
17 |
Correct |
84 ms |
250960 KB |
Output is correct |
18 |
Correct |
82 ms |
250960 KB |
Output is correct |
19 |
Execution timed out |
1052 ms |
251256 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
251284 KB |
Output is correct |
2 |
Correct |
83 ms |
251296 KB |
Output is correct |
3 |
Correct |
81 ms |
250960 KB |
Output is correct |
4 |
Correct |
82 ms |
250964 KB |
Output is correct |
5 |
Correct |
89 ms |
250960 KB |
Output is correct |
6 |
Correct |
84 ms |
250880 KB |
Output is correct |
7 |
Correct |
84 ms |
251052 KB |
Output is correct |
8 |
Correct |
85 ms |
251224 KB |
Output is correct |
9 |
Correct |
81 ms |
250956 KB |
Output is correct |
10 |
Correct |
82 ms |
250960 KB |
Output is correct |
11 |
Correct |
82 ms |
251024 KB |
Output is correct |
12 |
Correct |
83 ms |
251048 KB |
Output is correct |
13 |
Correct |
82 ms |
250992 KB |
Output is correct |
14 |
Correct |
83 ms |
250964 KB |
Output is correct |
15 |
Correct |
83 ms |
250960 KB |
Output is correct |
16 |
Correct |
89 ms |
251284 KB |
Output is correct |
17 |
Correct |
84 ms |
250960 KB |
Output is correct |
18 |
Correct |
82 ms |
250960 KB |
Output is correct |
19 |
Execution timed out |
1052 ms |
251256 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |