<style>
html, body, .ui-content {
background: #222222;
color: #00BFFF;
}
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: linear-gradient(180deg, #2BE8CF60 0%, #2B83E860 100%);
border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
background: linear-gradient(180deg, #2BE8CF95 0%, #2B83E895 100%);
}
/* 設定 code 模板 */
.markdown-body code,
.markdown-body tt {
background-color: #ffffff36;
}
.markdown-body .highlight pre,
.markdown-body pre {
color: #ddd;
background-color: #00000036;
}
.hljs-tag {
color: #ddd;
}
.token.operator {
background-color: transparent;
}
</style>
###### tags: `Leetcode`
# 2477. Minimum Fuel Cost to Report to the Capital
###### Link : https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/description/
## 題目
總共n個城市 roads會有n - 1條路
各個城市代表到首都(節點 0)總共需要多少燃料
## 程式碼
```cpp=
class Solution {
public:
vector<vector<int>> adj;//adj list
long long ans;//total fuel
long long minimumFuelCost(vector<vector<int>>& roads, int seats){
adj.resize(roads.size() + 1);
for(auto &it : roads){
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
ans = 0;
DFS(0, -1, seats);
return ans;
}
private:
int DFS(int node, int parent, int &seats){
int people = 1;
for(auto &child : adj[node]){
if(child != parent){//不往回走
//加上總共多少子節點
people += DFS(child, node, seats);
}
}
if(node)//如果不是節點0的話
//燃料增加ceil(這個節點人數除以一台車的座位數量)
ans += ceil((double)people / seats);
return people;
}
};
```