結果

問題 No.2618 除霊
ユーザー Nzt3Nzt3
提出日時 2024-02-02 17:01:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 1,253 bytes
コンパイル時間 2,879 ms
コンパイル使用メモリ 207,664 KB
実行使用メモリ 17,336 KB
最終ジャッジ日時 2024-02-02 17:01:36
合計ジャッジ時間 13,912 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 130 ms
16,300 KB
testcase_07 AC 108 ms
16,268 KB
testcase_08 AC 125 ms
16,516 KB
testcase_09 AC 128 ms
16,272 KB
testcase_10 AC 130 ms
16,496 KB
testcase_11 AC 97 ms
16,400 KB
testcase_12 AC 125 ms
16,428 KB
testcase_13 AC 88 ms
17,148 KB
testcase_14 AC 103 ms
16,980 KB
testcase_15 AC 78 ms
16,888 KB
testcase_16 AC 81 ms
16,836 KB
testcase_17 AC 129 ms
17,336 KB
testcase_18 AC 106 ms
16,660 KB
testcase_19 AC 84 ms
17,220 KB
testcase_20 AC 78 ms
16,952 KB
testcase_21 AC 113 ms
16,600 KB
testcase_22 AC 105 ms
17,312 KB
testcase_23 AC 95 ms
17,204 KB
testcase_24 AC 92 ms
17,012 KB
testcase_25 AC 108 ms
17,244 KB
testcase_26 AC 127 ms
16,572 KB
testcase_27 AC 92 ms
16,924 KB
testcase_28 AC 98 ms
17,268 KB
testcase_29 AC 109 ms
17,216 KB
testcase_30 AC 120 ms
17,208 KB
testcase_31 AC 129 ms
16,580 KB
testcase_32 AC 94 ms
16,708 KB
testcase_33 AC 102 ms
16,708 KB
testcase_34 AC 106 ms
16,124 KB
testcase_35 AC 116 ms
16,460 KB
testcase_36 AC 136 ms
16,708 KB
testcase_37 AC 135 ms
16,316 KB
testcase_38 AC 131 ms
16,708 KB
testcase_39 AC 131 ms
16,708 KB
testcase_40 AC 164 ms
16,488 KB
testcase_41 AC 147 ms
16,708 KB
testcase_42 AC 133 ms
16,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin>>N;
  vector G(N,vector(0,0));
  for(int i=0;i<N-1;i++){
    int A,B;
    cin>>A>>B;
    --A,--B;
    G[A].push_back(B);
    G[B].push_back(A);
  }
  vector ghost(N,0);
  int M;
  cin>>M;
  for(int i=0;i<M;i++){
    int t;
    cin>>t;
    ghost[t-1]=1;
  }
  vector cnt(N,0);
  for(int i=0;i<N;i++){
    if(ghost[i]){
      cnt[i]+=1;
      for(int j:G[i]){
        cnt[j]+=1;
      }
    }
  }
  vector g_erase(N,0);
  for(int i=0;i<N;i++){
    if(ghost[i]){
      for(int j:G[i]){
        if(cnt[j]==1){
          g_erase[i]+=1;
        }
      }
    }
  }
  int base=0;
  for(int i=0;i<N;i++){
    if(cnt[i]!=0)base+=1;
  }
  
  for(int i=0;i<N;i++){
    if(cnt[i]==0){
      cout<<base<<'\n';
      continue;
    }
    int ans=base-1;
    if(ghost[i]){
      ans-=g_erase[i];
      for(int j:G[i]){
        if(ghost[j]){
          ans-=g_erase[j];
          if(cnt[j]==2)ans-=1;
        }
      }
    }else{
      int t=0;
      if(cnt[i]==1)t=1;
      for(int j:G[i]){
        if(ghost[j]){
          ans-=g_erase[j]-t;
          if(cnt[j]==1)ans-=1;
        }
      }
    }
    cout<<ans<<'\n';
  }
  
}
0