// includes {{{ #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #include // #include // #include // #include // }}} using namespace std; using ll = long long; // #undef DEBUG // #define DEBUG // DEBUG {{{ #include #include #include #include #include #include #include #include #include template < int n, class... T > typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple( std::ostream &, std::tuple< T... > const &) {} template < int n, class... T > typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple( std::ostream &os, std::tuple< T... > const &t) { os << (n == 0 ? "" : ", ") << std::get< n >(t); __output_tuple< n + 1 >(os, t); } template < class... T > std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) { os << "("; __output_tuple< 0 >(os, t); os << ")"; return os; } template < class T, class U > std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template < class T > std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) { os << "{"; for(auto tmp = a; tmp.size(); tmp.pop()) os << (a.size() == tmp.size() ? "" : ", ") << tmp.top(); os << "}"; return os; } template < class T, class Container, class Compare > std::ostream &operator<<(std::ostream &os, std::priority_queue< T, Container, Compare > a) { os << "{ (top) "; while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } template < class T, class Container > std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) { os << "{ "; while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop(); os << " }"; return os; } #ifdef DEBUG #if !defined(DEBUG_OUT) #define DEBUG_OUT std::cerr #endif #define dump(...) \ [&]() { \ auto __debug_tap = std::make_tuple(__VA_ARGS__); \ DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \ << std::endl; \ }() template < class T > inline void dump2D(T &d, size_t sizey, size_t sizex) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for(size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << std::endl; } } template < class T > inline void dump1D(T &d, size_t sizey) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " "); } DEBUG_OUT << std::endl; } template < class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type, class = typename std::enable_if< !std::is_same< T, std::string >::value >::type > std::ostream &operator<<(std::ostream &os, const T &a) { os << "{"; for(auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : ", ") << *ite; os << "}"; return os; } #else #define dump(...) ((void) 42) #define dump2D(...) ((void) 42) #define dump1D(...) ((void) 42) template < class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type, class = typename std::enable_if< !std::is_same< T, std::string >::value >::type > std::ostream &operator<<(std::ostream &os, const T &a) { for(auto ite = begin(a); ite != end(a); ++ite) os << (ite == begin(a) ? "" : " ") << *ite; return os; } #endif // }}} const int N = 1e5; std::vector> g; std::vector>> smg; int n; set to_check[N]; int vis[N]; ll a[N]; bool ava[N]; int par[N], dep[N]; void dfsF(int i, int p, int d) { dep[i] = d; par[i] = p; for(int j : g[i]) if(j != p) { dfsF(j, i, d + 1); } } void ch(int f, int t) { if(f == t) return; a[t] += a[f]; a[f] = 0; } void to(int i, int j, int d) { if(d == 2) { if(dep[i] == dep[j]) { ch(j, i); if(par[i] != -1) ch(par[i], i); } else { int I = i; if(dep[i] < dep[j]) swap(i, j); ch(i, I); ch(par[i], I); ch(par[par[i]], I); } } else if(d == 1) { int I = i; if(dep[i] < dep[j]) swap(i, j); ch(i, I); ch(par[i], I); } } void dfs3(int t, int i, int p, int d = 3) { if(vis[i] >= d) return; vis[i] = d; if(t != i) { ch(i, t); } if(d >= 1) { for(int j : g[i]) if(j != p) { dfs3(t, j, i, d - 1); } } } void dfs0(int i, int p) { int cnt = 0; for(auto j : g[i]) if(j != p) { dfs0(j, i); cnt += ava[j]; } if(cnt >= 2) ava[i] = 1; } void dfs(int i, int p, int d, int bef) { // dump(i, p, d, bef, ava[i]); if(ava[i]) { if(bef != -1) { smg[bef].emplace_back(i, d); smg[i].emplace_back(bef, d); } bef = i; d = 0; } for(auto j : g[i]) if(j != p) { dfs(j, i, d + 1, bef); } } void dfs2(int t, int i, int p, int d) { if(d > 2) return; // dump(t, i); if(t != i) { a[t] += a[i]; a[i] = 0; to(t, i, d); } // dump(smg[i], d); for(auto to : smg[i]) if(to.first != p) { int j, w; tie(j, w) = to; dfs2(t, j, i, d + w); } } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); cin >> n; g.resize(n); for(int i = 0; i < n - 1; i++) { int a, b; std::cin >> a >> b; g[a].emplace_back(b); g[b].emplace_back(a); } for(int i = 0; i < n; i++) cin >> a[i]; dfsF(0, -1, 0); int q; cin >> q; constexpr int B = 256; // O(Q/B N) + O(QB) vector xs; for(int i = 0; i < q; i++) { int x; cin >> x; xs.push_back(x); ava[x] = 1; if(xs.size() == B || i + 1 == q) { // for(auto xx: xs) dfs00(xx, -1, 3); dump1D(ava, 10); smg.clear(); smg.resize(n); dfs(0, -1, 0, -1); dump(smg); for(auto x : xs) { dfs2(x, x, -1, 0); dfs3(x, x, -1); cout << a[x] << "\n"; } xs.clear(); for(int j = 0; j < n; j++) ava[j] = 0; } } return 0; }