結果

問題 No.899 γatheree
ユーザー ecto0310
提出日時 2019-10-04 21:44:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 852 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 174,768 KB
実行使用メモリ 17,732 KB
最終ジャッジ日時 2024-10-03 07:24:27
合計ジャッジ時間 6,119 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
using i64 = long long;

int main()
{
  i64 n;
  cin >> n;
  vector<i64> edge[n];
  for (i64 i = 0; i < n - 1; i++)
  {
    i64 w, v;
    cin >> w >> v;
    edge[w].push_back(v);
    edge[v].push_back(w);
  }
  vector<i64> cnt(n);
  for (i64 i = 0; i < n; i++)
    cin >> cnt[i];
  i64 q;
  cin >> q;
  for (i64 _ = 0; _ < q; _++)
  {
    i64 x;
    cin >> x;
    i64 ans = 0;
    queue<i64> que;
    vector<i64> far(n, 1e9);
    que.push(x);
    far[x] = 0;
    while (que.size())
    {
      i64 p = que.front();
      que.pop();
      ans += cnt[p];
      cnt[p] = 0;
      for (i64 i : edge[p])
        if (far[p] < 2 && far[p] + 1 < far[i])
        {
          far[i] = far[p] + 1;
          que.push(i);
        }
    }
    cnt[x] = ans;
    cout << ans << endl;
  }
  return 0;
}
0