[LeetCode] Collect Coins in a Tree

2603. Collect Coins in a Tree

There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given an integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given an array coins of size n where coins[i] can be either 0 or 1, where 1 indicates the presence of a coin in the vertex i.

Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times:

  • Collect all the coins that are at a distance of at most 2 from the current vertex, or
  • Move to any adjacent vertex in the tree.

Find the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex.

Note that if you pass an edge several times, you need to count it into the answer several times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

class Solution {
vector<int> adj[30001];
int c[30001], dp[30001], sub[30001], p[30001];
int dfs1(int u, int par) {
p[u] = par;
dp[u] += c[u];
sub[u] += c[u];
for(auto& v : adj[u]) {
sub[u] += c[v];
if(v == par) continue;
dp[u] += dfs1(v,u);
}
return dp[u];
}
int suball(int u, int par, int tot) {
if(par == p[u]) return dp[u];
return tot - (par >= 0 ? dp[par] : 0);
}
bool move(int u, int v, int tot) {
if(u == -1 or v == -1) return 0;
return suball(v,u,tot) > sub[v] - c[u];
}
int dfs0(int u, int par, int tot) {
int res = 0;
for(auto& v : adj[u]) {
if(v == par) continue;
if(move(u,v,tot)) {
res += 2 + dfs0(v,u,tot);
}
}
return res;
}
void dfs2(int u, int par, int x, int& res, int tot) {
res = min(res, x);
int req = 0;
for(auto& v : adj[u]) {
if(move(u,v,tot)) req += 2;
}
for(auto& v : adj[u]) {
if(v == par) continue;
int xx = x + req - (move(u,v,tot) ? 4 : 0) + (move(v,u,tot) ? 4 : 0);
dfs2(v,u,xx,res,tot);
}
}
public:
int collectTheCoins(vector<int>& coins, vector<vector<int>>& edges) {
for(int i = 0; i < coins.size(); i++) {
c[i] = coins[i];
sub[i] = dp[i] = 0;
}
for(auto& e : edges) {
int u = e[0], v = e[1];
adj[u].push_back(v);
adj[v].push_back(u);
}
int tot = dfs1(0,-1);
int x = dfs0(0,-1,tot);
int res = x;
dfs2(0,-1,x,res,tot);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/26/PS/LeetCode/collect-coins-in-a-tree/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.