classSolution: defmaximalPathQuality(self, values: List[int], edges: List[List[int]], maxTime: int) -> int: n = len(values) g = [[] for _ inrange(n)] for u, v, t in edges: g[u].append((v, t)) g[v].append((u, t))
ans = 0 visited = [False] * n
defdfs(u: int, time: int, quality: int) -> None: if u == 0: # 回到起點,但還可以繼續往下走,不用 return nonlocal ans ans = max(ans, quality) for v, t in g[u]: if time + t > maxTime: continue ifnot visited[v]: # 沒有走過,可以獲得 value visited[v] = True dfs(v, time + t, quality + values[v]) visited[v] = False else: # 走過了,還是可以繼續往下走,但是不會獲得 value dfs(v, time + t, quality)
classSolution { public: intmaximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime){ int n = values.size(); vector<vector<pair<int, int>>> g(n); for (auto& e : edges) { g[e[0]].emplace_back(e[1], e[2]); g[e[1]].emplace_back(e[0], e[2]); }
int ans = 0; vector<bool> visited(n, false); function<void(int, int, int)> dfs = [&](int u, int time, int quality) { if (u == 0) ans = max(ans, quality); for (auto& [v, t] : g[u]) { if (time + t > maxTime) continue; if (!visited[v]) { visited[v] = true; dfs(v, time + t, quality + values[v]); visited[v] = false; } else { dfs(v, time + t, quality); } } }; visited[0] = true; dfs(0, 0, values[0]); return ans; } };
寫在最後
masterpiece, best quality, high quality, extremely detailed CG unity 8k wallpaper, extremely detailed, High Detail,
1 girl, solo, long hair, black hair, blue eyes, skirt, shirt, school uniform, standing, white shirt, short sleeves, pleated skirt, outdoors, sky, serafuku, (pink serafuku, pink school uniform), day, sailor collar, blurry, arm up, neckerchief, long skirt, girl