# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
960906 |
2024-04-11T08:04:11 Z |
Ghetto |
Chase (CEOI17_chase) |
C++17 |
|
4000 ms |
43580 KB |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using lint = long long;
const int MAX_N = 1e5 + 5, MAX_K = 1e2 + 5;
int n, k;
lint val[MAX_N];
vector<int> adj[MAX_N], adj_ind[MAX_N]; // adj_ind[u][i] = ind of adj list u is for adj[u][i]
void init() {
for (int i = 1; i <= n; i++) {
adj[i].push_back(0);
adj_ind[i].push_back(0);
}
}
vector<int> seen[MAX_N];
vector<pii> topo_order;
void dfs(int u, int prev) {
seen[u][prev] = 1;
for (int i = 1; i < adj[u].size(); i++) {
if (i == prev) continue;
int v = adj[u][i];
if (seen[v][adj_ind[u][i]] == 1) assert(true == false);
if (!seen[v][adj_ind[u][i]]) dfs(v, adj_ind[u][i]);
}
seen[u][prev] = 2;
topo_order.push_back({u, prev});
}
vector<lint> dp[MAX_N][2];
void precomp() {
for (int i = 1; i <= n; i++)
seen[i].resize(adj[i].size() + 2);
for (int i = 1; i <= n; i++)
dfs(i, 0);
reverse(topo_order.begin(), topo_order.end());
for (int i = 1; i <= n; i++) {
for (int c = 0; c <= 1; c++) {
dp[i][c].resize(adj[i].size() + 2);
}
}
}
int main() {
// freopen("chase.in", "r", stdin);
cin >> n >> k;
init();
for (int i = 1; i <= n; i++) cin >> val[i];
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
int u_ind = adj[u].size() - 1, v_ind = adj[v].size() - 1;
adj_ind[u].push_back(v_ind);
adj_ind[v].push_back(u_ind);
}
precomp();
for (int c = 1; c <= k; c++) {
int p = c % 2, opp_p = (c + 1) % 2;
for (int j = topo_order.size() - 1; j >= 0; j--) {
int u = topo_order[j].first, prev = topo_order[j].second;
lint leave = 0;
for (int i = 1; i < adj[u].size(); i++)
if (i != prev) leave = max(leave, dp[adj[u][i]][p][adj_ind[u][i]]);
lint take = 0;
for (int i = 1; i < adj[u].size(); i++)
if (i != prev) take = max(take, dp[adj[u][i]][opp_p][adj_ind[u][i]]);
for (int i = 1; i < adj[u].size(); i++)
if (i != prev) take += val[adj[u][i]];
dp[u][p][prev] = max(take, leave);
}
}
lint ans = 0;
for (int i = 1; i <= n; i++) ans = max(ans, dp[i][k % 2][0]);
cout << ans << '\n';
}
Compilation message
chase.cpp: In function 'void dfs(int, int)':
chase.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 1; i < adj[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i = 1; i < adj[u].size(); i++)
| ~~^~~~~~~~~~~~~~~
chase.cpp:77:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 1; i < adj[u].size(); i++)
| ~~^~~~~~~~~~~~~~~
chase.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 1; i < adj[u].size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12732 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12732 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
8 ms |
13148 KB |
Output is correct |
8 |
Correct |
4 ms |
13148 KB |
Output is correct |
9 |
Correct |
24 ms |
13148 KB |
Output is correct |
10 |
Correct |
10 ms |
13148 KB |
Output is correct |
11 |
Correct |
6 ms |
13148 KB |
Output is correct |
12 |
Correct |
5 ms |
12992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3893 ms |
43580 KB |
Output is correct |
2 |
Correct |
3740 ms |
42844 KB |
Output is correct |
3 |
Execution timed out |
4035 ms |
27696 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12732 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
8 ms |
13148 KB |
Output is correct |
8 |
Correct |
4 ms |
13148 KB |
Output is correct |
9 |
Correct |
24 ms |
13148 KB |
Output is correct |
10 |
Correct |
10 ms |
13148 KB |
Output is correct |
11 |
Correct |
6 ms |
13148 KB |
Output is correct |
12 |
Correct |
5 ms |
12992 KB |
Output is correct |
13 |
Correct |
3893 ms |
43580 KB |
Output is correct |
14 |
Correct |
3740 ms |
42844 KB |
Output is correct |
15 |
Execution timed out |
4035 ms |
27696 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |