#include <bits/stdc++.h>
using namespace std;
typedef unsigned short us;
typedef vector<int> vl;
typedef vector<vl> vvl;
#define all(x) x.begin(), x.end()
#define pb push_back
const char nd = '\n';
struct edge {
us v;
int w;
};
vector<vector<edge>> graph;
bitset<50001> visto;
int res = 0;
us dfs(us u, us p) {
us cdown = 0, temp = 0;
for (auto v : graph[u]) {
if (v.v == p) continue;
temp = dfs(v.v, u);
if (temp && temp != 5) res += v.w;
cdown += temp;
}
if (visto[u]) cdown++;
return cdown;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//
int n;
cin >> n;
graph.assign(n + 1, vector<edge>());
us u, v;
int w;
for (int i = 1; i < n; i++) {
cin >> u >> v >> w;
graph[u].pb({v, w});
graph[v].pb({u, w});
}
int q;
cin >> q;
vector<us> a(5);
while (q--) {
for (auto &v : a) cin >> v, visto[v] = true;
res = 0;
dfs(0, -1);
for (auto &v : a) visto[v] = false;
cout << res << nd;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1056 ms |
5256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
3404 KB |
Output is correct |
2 |
Correct |
195 ms |
6888 KB |
Output is correct |
3 |
Correct |
194 ms |
6932 KB |
Output is correct |
4 |
Correct |
196 ms |
6996 KB |
Output is correct |
5 |
Correct |
203 ms |
6860 KB |
Output is correct |
6 |
Correct |
195 ms |
6964 KB |
Output is correct |
7 |
Correct |
198 ms |
6868 KB |
Output is correct |
8 |
Correct |
204 ms |
6868 KB |
Output is correct |
9 |
Correct |
197 ms |
6972 KB |
Output is correct |
10 |
Correct |
198 ms |
6876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1056 ms |
5256 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |