結果

問題 No.2618 除霊
ユーザー Nzt3
提出日時 2024-02-02 17:01:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 1,253 bytes
コンパイル時間 2,063 ms
コンパイル使用メモリ 198,496 KB
最終ジャッジ日時 2025-02-19 01:05:00
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

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