// #define _GLIBCXX_DEBUG
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define pb push_back
#define f first
#define s second
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = 1e9;
// const ll INF = 1e18;
const ll mod = 1000000007;
// const ll mod = 998244353;
vvi adj;
vpi dp;
vi tin, tout;
int timer;
vi sites;
void dfs(int node, int par){
tin[node] = timer++;
for(int e : adj[node]){
if(e == par) continue;
dfs(e, node);
dp[node].f += dp[e].f;
}
if(sites[node])
dp[node].f++;
tout[node] = timer++;
}
bool is_anc(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
void solve(int tc){
int n;
cin >> n;
adj.resize(n);
vector<array<int, 3>> edges;
for(int i = 0; i < n-1; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].pb(v);
adj[v].pb(u);
edges.pb({u, v, w});
}
int q;
cin >> q;
for(int j = 0; j < q; j++){
tin.resize(n), tout.resize(n);
sites.assign(n, 0);
for(int i = 0; i < 5; i++){
int x;
cin >> x;
sites[x] = 1;
}
dp.assign(n, {0, 0});
dfs(0, 0);
for(int i = 0; i < n; i++)
dp[i].s = 5 - dp[i].f;
int ans = 0;
for(auto e : edges){
if(is_anc(e[0], e[1]))
swap(e[0], e[1]);
if(dp[e[0]].f > 0 && dp[e[0]].s > 0)
ans += e[2];
}
cout << ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
#ifdef LOCAL
auto begin = high_resolution_clock::now();
#endif
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++) solve(t);
#ifdef LOCAL
auto end = high_resolution_clock::now();
cout << fixed << setprecision(4) << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1038 ms |
6820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
420 ms |
4644 KB |
Output is correct |
2 |
Correct |
698 ms |
7864 KB |
Output is correct |
3 |
Correct |
716 ms |
7716 KB |
Output is correct |
4 |
Correct |
753 ms |
7720 KB |
Output is correct |
5 |
Correct |
719 ms |
7716 KB |
Output is correct |
6 |
Correct |
682 ms |
7716 KB |
Output is correct |
7 |
Correct |
698 ms |
7716 KB |
Output is correct |
8 |
Correct |
691 ms |
7844 KB |
Output is correct |
9 |
Correct |
698 ms |
7716 KB |
Output is correct |
10 |
Correct |
683 ms |
7716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Execution timed out |
1038 ms |
6820 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |