- //#include <iostream>
- //#include <vector>
- //#include <algorithm>
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 105;
- vector<pair<int, int>> graph[MAXN]; // 存圖:{鄰接點, 權重}
- bool visited[MAXN];
- int countResult = 0;
- int Q;
- void dfs(int current, int minWeight) {
- visited[current] = true;
- if (minWeight >= Q) countResult++;
- for (auto &neighbor : graph[current]) {
- int next = neighbor.first;
- int weight = neighbor.second;
- if (!visited[next]) {
- dfs(next, min(minWeight, weight));
- }
- }
- }
- int main() {
- int N, k;
- cin >> N >> k >> Q;
- // 建圖
- for (int i = 0; i < N - 1; ++i) {
- int u, v, r;
- cin >> u >> v >> r;
- graph[u].push_back({v, r});
- graph[v].push_back({u, r});
- }
- // 初始化 visited 陣列
- fill(visited, visited + MAXN, false);
- // DFS 遍歷,從節點 k 開始,初始最小值設為無窮大
- visited[k] = true;
- for (auto &neighbor : graph[k]) {
- int next = neighbor.first;
- int weight = neighbor.second;
- if (!visited[next]) {
- dfs(next, weight);
- }
- }
- // 輸出結果(不包含自己)
- cout << countResult << endl;
- return 0;
- }
複製代碼 回復 1# may |