// competitive-verifier: LOCALCASE test/cpv_local/_data/bzoj/2286
#include "../../../src/graph/ds/alist/lib.hpp"
#include "../../../src/io/fastin/lib.hpp"
#include "../../../src/io/fastout/lib.hpp"
#include "../../../src/tree/virtual_tree/lib.hpp"
using namespace tifa_libs;
int main() {
u32 n;
fin_uint >> n;
vecu sign(n), min_(n);
using Tw = u32;
using tree_t = tree<alist<Tw>>;
tree_t trw(n);
for (u32 i = 1, u, v, w; i < n; ++i) fin_uint >> u >> v >> w, --u, --v, trw.add_arc(u, v, w), trw.add_arc(v, u, w);
auto dfs = [&](auto&& dfs, u32 u, u32 fa, u32 min__) -> void {
min_[u] = min__;
for (auto [to, w] : trw[u])
if (to != fa) dfs(dfs, to, u, std::min(min__, w));
};
dfs(dfs, 0, 0, INT32_MAX);
virtual_tree vt(trw);
u32 m, k;
fin_uint >> m;
flt_ (u32, i, 0, m) {
fin_uint >> k;
vecu a(k);
for (auto& x : a) fin_uint >> x, --x, sign[x] = 1;
vt.build(a);
auto dp = [&](auto&& dp, u32 u) -> u64 {
if (sign[u]) return min_[u];
u64 ret = 0;
for (auto to : vt.vt[u]) ret += dp(dp, to);
retif_((u), std::min(u64(min_[u]), ret), ret);
};
fout << dp(dp, 0) << '\n';
flt_ (u32, i, 0, k) sign[a[i]] = 0;
}
}
#line 1 "test/cpv_local/tree/virtual_tree.bzoj2286.cpp"
// competitive-verifier: LOCALCASE test/cpv_local/_data/bzoj/2286
#line 2 "src/graph/ds/alist/lib.hpp"
#line 2 "src/graph/ds/graph_c/lib.hpp"
#line 2 "src/util/traits/others/lib.hpp"
// clang-format off
#line 2 "src/util/alias/others/lib.hpp"
#line 2 "src/util/consts/lib.hpp"
#line 2 "src/util/alias/num/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();
inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/others/lib.hpp"
namespace tifa_libs {
template <class T>
struct chash {
CEXP static u64 C = u64(pi_v<f128> * 2e18) | 71;
CEXP u64 operator()(T x) CNE { return __builtin_bswap64(((u64)x ^ TIME) * C); }
};
// clang-format off
#define mk_(w, t) using w = t; using c##w = const t;
mk_(strn, std::string) mk_(strnv, std::string_view)
#undef mk_
template <class T> struct edge_t { T w; u32 u, v; CEXP auto operator<=>(edge_t CR) const = default; }; template <class T> using cedge_t = const edge_t<T>;
template <class T> struct pt3 { T _0, _1, _2; CEXP auto operator<=>(pt3 CR) const = default; }; template <class T> using cpt3 = const pt3<T>;
template <class T> struct pt4 { T _0, _1, _2, _3; CEXP auto operator<=>(pt4 CR) const = default; }; template <class T> using cpt4 = const pt4<T>;
#define mkT_(w, t, ...) template <class T> using w = t __VA_OPT__(, ) __VA_ARGS__; template <class T> using c##w = const t __VA_OPT__(, ) __VA_ARGS__;
mkT_(ptt, std::pair<T, T>) mkT_(alc, std::pmr::polymorphic_allocator<T>) mkT_(vec, std::vector<T>) mkT_(vvec, vec<vec<T>>) mkT_(v3ec, vvec<vec<T>>) mkT_(vecpt, vec<ptt<T>>) mkT_(vvecpt, vvec<ptt<T>>) mkT_(ptvec, ptt<vec<T>>) mkT_(ptvvec, ptt<vvec<T>>)
#undef mkT_
template <class T> using itl = std ::initializer_list<T>;
template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>; template <class T, usz N> using carr = std::array<const T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>; template <class U, class T> using vvecp = vvec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>; template <class U, class T> using vvvecp = vvec<vvec<std::pair<U, T>>>;
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class T, class C = std::less<T>> using set = __gnu_pbds::tree<T, __gnu_pbds::null_type, C>;
template <class K, class V, class C = std::less<K>> using map = __gnu_pbds::tree<K, V, C>;
// hset<u64> s({}, {}, {}, {}, {1<<16});
template <class T, class HF = chash<T>> using hset = __gnu_pbds::gp_hash_table<T, __gnu_pbds::null_type, HF>;
// hmap<u64, int> s({}, {}, {}, {}, {1<<16});
template <class K, class V, class HF = chash<K>> using hmap = __gnu_pbds::gp_hash_table<K, V, HF>;
#else
using std::set, std::map;
template <class T, class HF = chash<T>> using hset = std::unordered_set<T, HF>;
template <class K, class V, class HF = chash<K>> using hmap = std::unordered_map<K, V, HF>;
#endif
#ifdef PB_DS_PRIORITY_QUEUE_HPP
template <class T, class C = std::less<T>> using pq = __gnu_pbds::priority_queue<T, C>;
#else
template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
#endif
template <class T> using pqg = pq<T, std::greater<T>>;
// clang-format on
#define mk1_(V, A, T) using V##A = V<T>;
#define mk_(V, A, T) mk1_(V, A, T) mk1_(c##V, A, T)
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk1_(spn, A, T) mk1_(itl, A, T)
mk(b, bool) mk(c, chr) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64) mk(t, isz) mk(z, usz) mk(f, f32) mk(d, f64) mk(s, strn);
#undef mk
#undef mk_
#undef mk1_
} // namespace tifa_libs
#line 4 "src/util/traits/others/lib.hpp"
namespace tifa_libs {
//! only for template without non-type argument
template <class, template <class...> class> CEXP bool specialized_from_v = false;
template <template <class...> class T, class... Args> CEXP bool specialized_from_v<T<Args...>, T> = true;
static_assert(specialized_from_v<vecu, std::vector>);
template <class T> concept container_c = common_range<T> && !std::is_array_v<std::remove_cvref_t<T>> && !std::same_as<std::remove_cvref_t<T>, strn> && !std::same_as<std::remove_cvref_t<T>, strnv>;
template <class T> concept istream_c = std::derived_from<T, std::istream> || std::derived_from<T, std::wistream> || requires(T is) { is.peek(); };
template <class T> concept ostream_c = std::derived_from<T, std::ostream> || std::derived_from<T, std::wostream> || requires(T os) { os.flush(); };
} // namespace tifa_libs
// clang-format on
#line 4 "src/graph/ds/graph_c/lib.hpp"
namespace tifa_libs {
namespace graph_info_impl_ {
struct graph_info_tag_base {};
template <class... Info>
requires(std::derived_from<Info, graph_info_tag_base> && ...)
struct graph_tag_base : Info... {
CEXP graph_tag_base(auto&&... args) NE : Info(std::forward<decltype(args)>(args)...)... {}
protected:
CEXP void add_arc(auto&&... args) NE { (Info::add_arc(std::forward<decltype(args)>(args)...), ...); }
CEXP void del_arc(auto&&... args) NE { (Info::del_arc(std::forward<decltype(args)>(args)...), ...); }
};
template <class T, class = std::is_void<T>::type>
struct E;
template <class T>
struct E<T, std::false_type> {
u32 to;
T cost;
CEXP E() = default;
CEXP E(u32 v, cT_(T) c) NE : to(v), cost(c) {}
CEXP operator u32() CNE { return to; }
};
template <class T>
struct E<T, std::true_type> {
u32 to;
CEXP E() = default;
CEXP E(u32 v) NE : to(v) {}
CEXP operator u32() CNE { return to; }
};
} // namespace graph_info_impl_
namespace graph_impl_ {
template <class etag_t>
struct graph : etag_t {
using Et = etag_t::val_t;
CEXP graph() = default;
CEXPE graph(u32 n, auto&&... e_args) NE : etag_t(n, std::forward<decltype(e_args)>(e_args)...) {}
CEXP void add_edge(u32 u, u32 v, auto&&... args) NE { etag_t::add_arc(u, v, std::forward<decltype(args)>(args)...), etag_t::add_arc(v, u, std::forward<decltype(args)>(args)...); }
template <class F>
CEXP void foreach(u32 u, F&& f) CNE {
if CEXP (std::is_void_v<Et>)
for (auto v : (*this)[u]) f(v);
else
for (auto [v, w] : (*this)[u]) f(v, w);
}
};
} // namespace graph_impl_
// clang-format off
#define CONCEPT_GRAPH(name, base) \
template <class T> concept name##_c = specialized_from_v<T, base>; \
template <class T> concept u##name##_c = name##_c<T> && std::same_as<TPN T::Et, void>; \
template <class T> concept w##name##_c = name##_c<T> && !std::same_as<TPN T::Et, void>
// clang-format on
CONCEPT_GRAPH(graph, graph_impl_::graph);
} // namespace tifa_libs
#line 4 "src/graph/ds/alist/lib.hpp"
namespace tifa_libs {
namespace alist_impl_ {
template <class T, class... Info>
class alist_tag : public graph_info_impl_::graph_tag_base<Info...> {
using base_t = graph_info_impl_::graph_tag_base<Info...>;
vvec<graph_info_impl_::E<T>> g;
protected:
using val_t = T;
CEXPE alist_tag(u32 n) NE : base_t(n), g(n) {}
public:
CEXP void build() CNE {}
CEXP void add_arc(u32 u, auto&&... args) NE { base_t::add_arc(u, std::forward<decltype(args)>(args)...), g[u].emplace_back(std::forward<decltype(args)>(args)...); }
ND CEXP u32 vsize() CNE { return (u32)g.size(); }
ND CEXP u32 deg_out(u32 u) CNE { return (u32)g[u].size(); }
CEXP auto CR operator[](u32 u) CNE { return g[u]; }
CEXP auto& operator[](u32 u) NE { return g[u]; }
};
} // namespace alist_impl_
template <class Et = void, class... Info>
using alist = graph_impl_::graph<alist_impl_::alist_tag<Et, Info...>>;
} // namespace tifa_libs
#line 2 "src/io/fastin/lib.hpp"
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#line 4 "src/util/traits/math/lib.hpp"
namespace tifa_libs {
template <class T> concept char_c = std::same_as<T, char> || std::same_as<T, signed char> || std::same_as<T, unsigned char>;
#pragma GCC diagnostic ignored "-Wpedantic"
template <class T> concept s128_c = std::same_as<T, __int128_t> || std::same_as<T, __int128>;
template <class T> concept u128_c = std::same_as<T, __uint128_t> || std::same_as<T, unsigned __int128>;
template <class T> concept i128_c = s128_c<T> || u128_c<T>;
#pragma GCC diagnostic warning "-Wpedantic"
template <class T> concept imost64_c = std::integral<T> && sizeof(T) * __CHAR_BIT__ <= 64;
template <class T> concept smost64_c = imost64_c<T> && std::signed_integral<T>;
template <class T> concept umost64_c = imost64_c<T> && std::unsigned_integral<T>;
template <class T> concept int_c = i128_c<T> || imost64_c<T>;
template <class T> concept sint_c = s128_c<T> || smost64_c<T>;
template <class T> concept uint_c = u128_c<T> || umost64_c<T>;
template <class T> concept arithm_c = std::is_arithmetic_v<T> || int_c<T>;
template <class T> concept mint_c = requires(T x) { {x.mod()} -> uint_c; {x.val()} -> uint_c; };
template <class T> concept dft_c = requires(T x, std::vector<TPN T::data_t> v, u32 n) { {x.size()} -> std::same_as<u32>; x.bzr(n); x.dif(v, n); x.dit(v, n); };
template <class T> concept ntt_c = dft_c<T> && requires(T x) { T::max_size; T::G; };
template <class T> struct to_sint : std::make_signed<T> {};
template <> struct to_sint<u128> { using type = i128; };
template <> struct to_sint<i128> { using type = i128; };
template <class T> using to_sint_t = TPN to_sint<T>::type;
template <class T> struct to_uint : std::make_unsigned<T> {};
template <> struct to_uint<u128> { using type = u128; };
template <> struct to_uint<i128> { using type = u128; };
template <class T> using to_uint_t = TPN to_uint<T>::type;
template <arithm_c T> struct to_bigger : std::make_unsigned<T> {};
#define _(w,ww) template <> struct to_bigger<w> { using type = ww; }
#define _2(w,ww) _(i##w,i##ww); _(u##w,u##ww);
_2(8, 16); _2(16, 32); _2(32, 64); _2(64, 128); _(f32, f64); _(f64, f128);
#undef _2
#undef _
template <class T> using to_bigger_t = TPN to_bigger<T>::type;
template <arithm_c T> CEXP T inf_v = [] {
if CEXP(sint_c<T>) return T(to_uint_t<T>(-1) / 4 - 1);
else if CEXP(uint_c<T>) return T(-1) / 2 - 1;
else return std::numeric_limits<T>::max() / 2 - 1;
}();
} // namespace tifa_libs
// clang-format on
#line 5 "src/io/fastin/lib.hpp"
#ifdef __linux__
#include <sys/mman.h>
#include <sys/stat.h>
#endif
namespace tifa_libs {
struct fastin_data {
CEXP static u32 BUF = 0x200005;
FILE* f_ = nullptr;
#ifdef __linux__
chr *bg, *ed, *p;
struct stat Fl;
void rebind(FILE* f = nullptr) NE {
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
if (!f_) munmap(bg, Fl.st_size + 1);
#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
if (!f) return;
auto fd = fileno(f_ = f);
fstat(fd, &Fl);
p = (bg = (chr*)mmap(nullptr, Fl.st_size + 4, PROT_READ, MAP_PRIVATE, fd, 0));
ed = bg + Fl.st_size;
madvise(bg, Fl.st_size + 4, MADV_SEQUENTIAL);
}
ND bool iseof() CNE { return p == ed; }
~fastin_data() NE { rebind(); }
#else
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
chr buf[BUF], *ed, *p;
void rebind(FILE* f) NE { f_ = f, p = ed = buf; }
ND bool iseof() NE {
if (p == ed) [[unlikely]]
ed = (p = buf) + fread(buf, 1, BUF, f_);
return p == ed;
}
#endif
fastin_data(FILE* f = stdin) NE { assert(f), rebind(f); }
fastin_data(fastin_data CR) = delete;
fastin_data& operator=(fastin_data CR) = delete;
};
// clang-format off
enum FIN_SET : u8 { FS_NEWLINE = 1, FS_SPACE = 2, FS_NEG = 4, FS_NUM = 8, FS_ALPHA = 16, FS_OTHERS = 32 };
// clang-format on
template <u32 charset>
class fastin {
fastin_data& data;
static CEXP bool is_cntrl(chr c) NE {
if CEXP (charset & FS_OTHERS) return iscntrl(c);
else if CEXP (charset & FS_NEWLINE) return c < 32;
else return false;
}
static CEXP bool is_cntrls(chr c) NE {
if CEXP (charset & FS_OTHERS) return !isgraph(c);
else if CEXP (charset & (FS_NEWLINE | FS_SPACE)) return c <= 32;
else return false;
}
static CEXP bool is_digit(chr c) NE {
if CEXP (charset & (FS_ALPHA | FS_OTHERS)) return isdigit(c);
else if CEXP (!(charset & FS_NUM)) return false;
else if CEXP (!(charset & FS_NEG)) return c > 32;
else return c >= 48;
}
static CEXP bool is_neg_digit(chr c) NE {
if CEXP (!(charset & FS_NEG)) return is_digit(c);
else if CEXP (charset & (FS_ALPHA | FS_OTHERS)) return c == '-' || isdigit(c);
else return c > 32;
}
#ifdef __linux__
template <bool ignore_space = true>
void read_str(strn& n) NE {
chr* l;
if CEXP (ignore_space) {
skip_cntrls(), l = data.p;
while (!is_cntrls(*data.p)) ++data.p;
} else {
skip_cntrl(), l = data.p;
while (!is_cntrl(*data.p)) ++data.p;
}
n.assign(l, data.p);
}
#else
template <bool ignore_space>
void read_str(strn& n) NE {
if CEXP (n.clear(); ignore_space) {
n.push_back(skip_cntrls().get());
while (!is_cntrls(peek())) n.push_back(get());
} else {
n.push_back(skip_cntrl().get());
while (!is_cntrl(peek())) n.push_back(get());
}
}
#endif
public:
fastin(fastin_data& data) NE : data(data) {}
fastin(fastin CR) = delete;
fastin& operator=(fastin CR) = delete;
chr peek() NE {
if (data.iseof()) [[unlikely]]
return EOF;
return *data.p;
}
chr get_unchk() NE { return *data.p++; }
chr get() NE {
if (data.iseof()) [[unlikely]]
return EOF;
return get_unchk();
}
#define SKIP(name, pred) \
fastin& skip_##name() NE { \
while (pred(peek())) get_unchk(); \
return *this; \
}
SKIP(cntrl, is_cntrl)
SKIP(cntrls, is_cntrls)
SKIP(ndigit, !is_digit)
SKIP(nnegdigit, !is_neg_digit)
#undef SKIP
template <class T>
requires(imost64_c<T> && !char_c<T>)
fastin& operator>>(T& n) NE {
if CEXP (std::same_as<T, bool>) n = skip_ndigit().get() != '0';
else {
n = 0;
bool is_neg = false;
if CEXP ((charset & FS_NEG) && std::signed_integral<T>) is_neg = (skip_nnegdigit().peek() == '-' && get_unchk());
else skip_ndigit();
std::conditional_t<sizeof(T) < sizeof(u32), u32, to_uint_t<T>> n_ = 0;
#ifdef __linux__
// clang-format off
#define _ {while (~STR2U16[*(u16*)data.p]) (n_ *= 100) += STR2U16[*(u16*)data.p], data.p += 2; if (is_digit(peek())) (n_ *= 10) += get_unchk() & 15;}
if ((usz)data.p & 1) { if (is_digit(peek())) [[likely]] { (n_ *= 10) += get_unchk() & 15; _ } } else _;
#undef _
// clang-format on
#else
while (is_digit(peek())) (n_ *= 10) += get_unchk() & 15;
#endif
if CEXP (sint_c<T>)
if (is_neg) n_ = -n_;
n = (T)n_;
}
return *this;
}
fastin& operator>>(std::floating_point auto& n) NE {
static strn s;
(*this >> s), std::from_chars(begin(s).base(), end(s).base(), n);
return *this;
}
//! ignore cntrl and space
fastin& operator>>(char_c auto& n) NE {
n = skip_cntrls().get();
return *this;
}
fastin& operator>>(strn& n) NE {
read_str<true>(n);
return *this;
}
fastin& getline(strn& n) NE {
read_str<false>(n);
return *this;
}
//! NOT ignore cntrl and space
fastin& strict_read(char_c auto& n) NE {
n = get();
return *this;
}
fastin& operator>>(fastin& (*func)(fastin&)) NE { return func(*this); }
};
inline fastin_data fid_stdin;
inline fastin<FS_NEWLINE | FS_SPACE | FS_NEG | FS_NUM | FS_ALPHA | FS_OTHERS> fin(fid_stdin);
inline fastin<FS_NEWLINE | FS_SPACE | FS_NEG | FS_NUM> fin_int(fid_stdin);
inline fastin<FS_NEWLINE | FS_SPACE | FS_NUM> fin_uint(fid_stdin);
template <u32 w>
inline fastin<w>& ws(fastin<w>& f) NE { return f.skip_cntrls(); }
} // namespace tifa_libs
#line 2 "src/io/fastout/lib.hpp"
#line 5 "src/io/fastout/lib.hpp"
namespace tifa_libs {
class fastout {
CEXP static u32 BUF = 0x200005, INTBUF = 63;
FILE* f_ = nullptr;
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
chr int_buf[INTBUF];
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
chr buf[BUF], *p;
chr CPC ed = buf + BUF;
std::chars_format fmt = std::chars_format::general;
int precision = 6;
fastout& write_str(chr CP n, usz len = 0) NE {
if (!len) len = strlen(n);
usz l_;
chr CP n_ = n;
while (p + len >= ed) memcpy(p, n_, l_ = usz(ed - p)), p += l_, n_ += l_, len -= l_, flush();
return memcpy(p, n_, len), p += len, *this;
}
public:
fastout(FILE* f = stdout) NE { rebind(f); }
fastout(fastout CR) = delete;
fastout& operator=(fastout CR) = delete;
~fastout() NE { flush(); }
void rebind(FILE* f) NE { f_ = f, p = buf; }
void flush() NE { fwrite(buf, 1, usz(p - buf), f_), p = buf; }
fastout& operator<<(char_c auto n) NE {
if (p == ed) [[unlikely]]
flush();
*(p++) = n;
return *this;
}
fastout& operator<<(chr CP n) NE { return write_str(n); }
fastout& operator<<(strn CR str) NE { return write_str(str.data(), str.size()); }
fastout& operator<<(strnv str) NE { return write_str(str.data(), str.size()); }
template <class T>
requires(smost64_c<T> && !char_c<T>)
fastout& operator<<(T n) NE {
if CEXP (sizeof(T) < sizeof(i32)) return *this << (i32)n;
else {
if (n < 0) return *this << '-' << -to_uint_t<T>(n);
return *this << to_uint_t<T>(n);
}
}
template <class T>
requires(umost64_c<T> && !char_c<T>)
fastout& operator<<(T n) NE {
if CEXP (std::same_as<T, bool>) return *this << (chr(n | '0'));
else if CEXP (sizeof(T) < sizeof(u32)) return *this << (u32)n;
else if (usz(p - buf) >= BUF - INTBUF) [[unlikely]] {
auto res = std::to_chars(int_buf, int_buf + INTBUF, n);
return write_str(int_buf, usz(res.ptr - int_buf));
} else {
auto res = std::to_chars(p, buf + BUF, n);
p = res.ptr;
return *this;
}
}
fastout& operator<<(std::floating_point auto n) NE {
if (usz(p - buf) >= BUF - INTBUF) [[unlikely]] {
auto res = std::to_chars(int_buf, int_buf + INTBUF, n, fmt, precision);
return write_str(int_buf, usz(res.ptr - int_buf));
} else {
auto res = std::to_chars(p, buf + BUF, n, fmt, precision);
p = res.ptr;
return *this;
}
}
fastout& setf(std::chars_format f) NE {
fmt = f;
return *this;
}
//! only tested in libstdc++
fastout& operator<<(decltype(std::setprecision(0)) p) NE {
precision = *(int*)(&p);
return *this;
}
fastout& operator<<(fastout& (*func)(fastout&)) NE { return func(*this); }
};
inline fastout fout;
inline fastout& scientific(fastout& f) NE { return f.setf(std::chars_format::scientific); }
inline fastout& fixed(fastout& f) NE { return f.setf(std::chars_format::fixed); }
inline fastout& hexfloat(fastout& f) NE { return f.setf(std::chars_format::hex); }
inline fastout& defaultfloat(fastout& f) NE { return f.setf(std::chars_format::general); }
inline fastout& endl(fastout& f) NE {
(f << '\n').flush();
return f;
}
using std::setprecision;
} // namespace tifa_libs
#line 2 "src/tree/virtual_tree/lib.hpp"
#line 2 "src/tree/ds/lib.hpp"
#line 4 "src/tree/ds/lib.hpp"
namespace tifa_libs {
template <graph_c graph_t>
struct tree : graph_t {
u32 root;
CEXP tree(graph_t CR g, u32 root = 0) NE : graph_t(g), root(root) {}
CEXP tree(graph_t&& g, u32 root = 0) NE : graph_t(std::move(g)), root(root) {}
CEXPE tree(u32 n, u32 root = 0) NE : graph_t(n), root(root) {}
};
CONCEPT_GRAPH(tree, tree);
} // namespace tifa_libs
#line 2 "src/tree/lca_hld/lib.hpp"
#line 2 "src/tree/dfs/info/lib.hpp"
#line 5 "src/tree/dfs/info/lib.hpp"
namespace tifa_libs {
struct tdi_dfn {
vecu dfn;
protected:
u32 cnt{0};
CEXPE tdi_dfn(u32 n) NE : dfn(n) {}
void init(u32 u, u32) NE { dfn[u] = cnt++; }
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
struct tdi_sz {
vecu sz;
protected:
CEXPE tdi_sz(u32 n) NE : sz(n) {}
void init(u32 u, u32) NE { sz[u] = 1; }
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32 to, u32 u, auto) NE { sz[u] += sz[to]; }
void before_return(u32, u32) NE {}
};
struct tdi_fa {
vecu fa;
protected:
CEXPE tdi_fa(u32 n) NE : fa(n) {}
void init(u32 u, u32 f) NE { fa[u] = f; }
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
struct tdi_dep {
vecu dep;
protected:
CEXPE tdi_dep(u32 n) NE : dep(n) {}
void init(u32, u32) NE {}
void pre_dfs(u32 to, u32 u, auto) NE { dep[to] = dep[u] + 1; }
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
struct tdi_maxson : tdi_sz {
vecu maxson;
protected:
CEXPE tdi_maxson(u32 n) NE : tdi_sz(n), maxson(n, n) {}
void init(u32 u, u32 f) NE { tdi_sz::init(u, f); }
void pre_dfs(u32 to, u32 u, auto w) NE { tdi_sz::pre_dfs(to, u, w); }
void post_dfs(u32 to, u32 u, auto w) NE {
if (tdi_sz::post_dfs(to, u, w); maxson[u] == (u32)maxson.size() || sz[to] > sz[maxson[u]]) maxson[u] = to;
}
void before_return(u32 u, u32 f) NE { tdi_sz::before_return(u, f); }
};
struct tdi_maxdfn {
vecu maxdfn;
protected:
u32 cnt{0};
CEXPE tdi_maxdfn(u32 n) NE : maxdfn(n) {}
void init(u32, u32) NE { ++cnt; }
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32, u32, auto) NE {}
void before_return(u32 u, u32) NE { maxdfn[u] = cnt - 1; }
};
struct tdi_euler {
vecu euler;
protected:
u32 cnt{0};
CEXPE tdi_euler(u32 n) NE : euler(n) {}
void init(u32 u, u32) NE { euler[cnt++] = u; }
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
struct tdi_go {
vvecu go;
protected:
CEXP static u32 N = 21;
CEXPE tdi_go(u32 n) NE : go(n, vecu(N, n)) { assert(n < 1u << N); }
void init(u32 u, u32 f) NE {
go[u][0] = f;
for (u32 i = 1; i < N && go[u][i - 1] < go.size(); ++i) go[u][i] = go[go[u][i - 1]][i - 1];
}
void pre_dfs(u32, u32, auto) NE {}
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
template <tree_c G>
struct tdi_dis {
using w_t = std::conditional_t<std::is_void_v<TPN G::Et>, u32, TPN G::Et>;
vec<w_t> dis;
protected:
CEXPE tdi_dis(u32 n) NE : dis(n) {}
void init(u32, u32) NE {}
void pre_dfs(u32 to, u32 u, auto w) NE { dis[to] = dis[u] + w; }
void post_dfs(u32, u32, auto) NE {}
void before_return(u32, u32) NE {}
};
template <tree_c G, class... Ts>
struct tree_dfs_info : Ts... {
using w_t = std::conditional_t<std::is_void_v<TPN G::Et>, u32, TPN G::Et>;
CEXPE tree_dfs_info(G CR tree) : Ts(tree.vsize())... { dfs(tree, tree.root); }
private:
void dfs(G CR g, u32 u, u32 fa = -1_u32) NE {
if CEXP ((Ts::init(u, fa), ...); wtree_c<G>) {
for (auto [v, w] : g[u])
if (v != fa) (Ts::pre_dfs(v, u, w), ...), dfs(g, v, u), (Ts::post_dfs(v, u, w), ...);
} else
for (auto v : g[u])
if ((u32)v != fa) (Ts::pre_dfs((u32)v, u, 1), ...), dfs(g, (u32)v, u), (Ts::post_dfs((u32)v, u, 1), ...);
(Ts::before_return(u, fa), ...);
}
};
} // namespace tifa_libs
#line 2 "src/tree/dfs/top/lib.hpp"
#line 5 "src/tree/dfs/top/lib.hpp"
namespace tifa_libs {
template <tree_c G, bool need_dfn = false>
CEXP vecu tree_top(G CR tr, std::conditional_t<need_dfn, vecu&, spnu> dfn, spnu maxson) NE {
cu32 n = tr.vsize();
if CEXP (need_dfn) dfn = vecu(n);
vecu top(n, n);
u32 cnt = 0; // NOLINT(misc-const-correctness)
auto f = [&](auto&& f, u32 u, u32 top_) NE -> void {
if CEXP (need_dfn) dfn[u] = cnt++;
if (top[u] = top_; maxson[u] == tr.vsize()) return;
for (f(f, maxson[u], top_); auto v : tr[u])
if (top[(u32)v] == tr.vsize()) f(f, (u32)v, (u32)v);
};
f(f, tr.root, tr.root);
return top;
}
} // namespace tifa_libs
#line 5 "src/tree/lca_hld/lib.hpp"
namespace tifa_libs {
template <tree_c G>
struct lca_hld {
using tree_info_t = tree_dfs_info<G, tdi_dfn, tdi_maxson, tdi_dep, tdi_fa>;
tree_info_t info;
vecu top;
CEXP lca_hld(G CR tr) : info{tr} { top = tree_top(tr, info.dfn, info.maxson); }
CEXP u32 operator()(u32 u, u32 v) CNE {
while (top[u] != top[v]) info.dep[top[u]] < info.dep[top[v]] ? v = info.fa[top[v]] : u = info.fa[top[u]];
retif_((info.dep[u] > info.dep[v]), v, u);
}
CEXP ptt<vecptu> getchain(u32 u, u32 v) NE {
u32 lca = (*this)(u, v);
vecptu retu, retv;
while (top[u] != top[lca]) retu.emplace_back(u, top[u]), u = info.fa[top[u]];
retu.emplace_back(u, lca);
while (top[v] != top[lca]) retv.emplace_back(top[v], v), v = info.fa[top[v]];
if (v != lca) retv.emplace_back(info.maxson[lca], v);
reverse(retv);
return {retu, retv};
}
};
} // namespace tifa_libs
#line 6 "src/tree/virtual_tree/lib.hpp"
namespace tifa_libs {
template <tree_c G>
class virtual_tree {
u32 root, tim{0};
lca_hld<G> lca_;
vecu st, used, vis;
CEXP void touch(u32 x) NE {
if (vis[x] == tim) return;
vis[x] = tim, used.push_back(x);
}
CEXP void add_arc_(u32 u, u32 v) NE { vt.add_arc(u, v), touch(u), touch(v); }
CEXP void insert(u32 x) NE {
touch(x);
cu32 lca = lca_(x, st.back());
if (lca == st.back()) return st.push_back(x);
while (st.size() > 1 && lca_.info.dep[st[st.size() - 2]] >= lca_.info.dep[lca]) add_arc_(st[st.size() - 2], st.back()), st.pop_back();
if (lca_.info.dep[st.back()] > lca_.info.dep[lca]) add_arc_(lca, st.back()), st.pop_back();
if (st.back() != lca) touch(lca), st.push_back(lca);
st.push_back(x);
}
public:
using tree_info_t = lca_hld<G>::tree_info_t;
tree<alist<>> vt;
CEXPE virtual_tree(G CR tr) NE : root{tr.root}, lca_(tr), st{}, used{}, vis(tr.vsize()), vt(tr.vsize()) {}
CEXP void build(vecu& a) NE {
for (++tim; cu32 x : used) vt[x].clear();
used.clear(), st.clear();
sort(a, [&](u32 a, u32 b) NE { return lca_.info.dfn[a] < lca_.info.dfn[b]; });
for (st.push_back(root), touch(root); cu32 x : a) insert(x);
while (st.size() > 1) add_arc_(st[st.size() - 2], st.back()), st.pop_back();
st.pop_back();
}
};
} // namespace tifa_libs
#line 7 "test/cpv_local/tree/virtual_tree.bzoj2286.cpp"
using namespace tifa_libs;
int main() {
u32 n;
fin_uint >> n;
vecu sign(n), min_(n);
using Tw = u32;
using tree_t = tree<alist<Tw>>;
tree_t trw(n);
for (u32 i = 1, u, v, w; i < n; ++i) fin_uint >> u >> v >> w, --u, --v, trw.add_arc(u, v, w), trw.add_arc(v, u, w);
auto dfs = [&](auto&& dfs, u32 u, u32 fa, u32 min__) -> void {
min_[u] = min__;
for (auto [to, w] : trw[u])
if (to != fa) dfs(dfs, to, u, std::min(min__, w));
};
dfs(dfs, 0, 0, INT32_MAX);
virtual_tree vt(trw);
u32 m, k;
fin_uint >> m;
flt_ (u32, i, 0, m) {
fin_uint >> k;
vecu a(k);
for (auto& x : a) fin_uint >> x, --x, sign[x] = 1;
vt.build(a);
auto dp = [&](auto&& dp, u32 u) -> u64 {
if (sign[u]) return min_[u];
u64 ret = 0;
for (auto to : vt.vt[u]) ret += dp(dp, to);
retif_((u), std::min(u64(min_[u]), ret), ret);
};
fout << dp(dp, 0) << '\n';
flt_ (u32, i, 0, k) sign[a[i]] = 0;
}
}