結果

問題 No.899 γatheree
ユーザー lumc_lumc_
提出日時 2019-10-04 23:27:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,448 bytes
コンパイル時間 1,549 ms
コンパイル使用メモリ 127,208 KB
実行使用メモリ 24,816 KB
最終ジャッジ日時 2024-10-03 09:04:12
合計ジャッジ時間 5,833 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,440 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
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<std::vector<int>> g;
std::vector<std::vector<pair<int, int>>> smg;
int n;
set<int> 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<int> 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;
}
0