#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define all(x) x.begin(), x.end()
#define pb push_back
const char nd = '\n';
struct edge {
ll v, w;
};
vector<vector<edge>> graph;
bitset<50001> visto;
ll res = 0;
ll dfs(ll u, ll p) {
ll cdown = 0, temp;
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);
//
ll n;
cin >> n;
graph.assign(n + 1, vector<edge>());
ll u, v, w;
for (ll i = 1; i < n; i++) {
cin >> u >> v >> w;
graph[u].pb({v, w});
graph[v].pb({u, w});
}
ll q;
cin >> q;
vl 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;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1078 ms |
5144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
3284 KB |
Output is correct |
2 |
Correct |
190 ms |
7108 KB |
Output is correct |
3 |
Correct |
199 ms |
6996 KB |
Output is correct |
4 |
Correct |
191 ms |
7044 KB |
Output is correct |
5 |
Correct |
192 ms |
6996 KB |
Output is correct |
6 |
Correct |
190 ms |
6996 KB |
Output is correct |
7 |
Correct |
189 ms |
7000 KB |
Output is correct |
8 |
Correct |
190 ms |
6996 KB |
Output is correct |
9 |
Correct |
193 ms |
6996 KB |
Output is correct |
10 |
Correct |
200 ms |
7088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
5144 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |