結果

問題 No.2618 除霊
ユーザー sasa8uyauyasasa8uyauya
提出日時 2024-01-26 22:50:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 883 ms / 2,000 ms
コード長 1,206 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 140,760 KB
最終ジャッジ日時 2024-01-26 22:50:50
合計ジャッジ時間 27,933 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 776 ms
113,044 KB
testcase_07 AC 756 ms
112,908 KB
testcase_08 AC 789 ms
115,760 KB
testcase_09 AC 718 ms
121,996 KB
testcase_10 AC 642 ms
132,680 KB
testcase_11 AC 693 ms
110,820 KB
testcase_12 AC 844 ms
130,808 KB
testcase_13 AC 423 ms
110,728 KB
testcase_14 AC 445 ms
138,448 KB
testcase_15 AC 452 ms
110,804 KB
testcase_16 AC 503 ms
109,188 KB
testcase_17 AC 566 ms
125,136 KB
testcase_18 AC 585 ms
137,776 KB
testcase_19 AC 526 ms
112,360 KB
testcase_20 AC 508 ms
111,048 KB
testcase_21 AC 556 ms
122,084 KB
testcase_22 AC 571 ms
140,760 KB
testcase_23 AC 523 ms
110,968 KB
testcase_24 AC 530 ms
110,696 KB
testcase_25 AC 570 ms
123,404 KB
testcase_26 AC 521 ms
138,404 KB
testcase_27 AC 522 ms
110,020 KB
testcase_28 AC 493 ms
111,956 KB
testcase_29 AC 559 ms
123,024 KB
testcase_30 AC 561 ms
132,484 KB
testcase_31 AC 883 ms
134,564 KB
testcase_32 AC 565 ms
109,508 KB
testcase_33 AC 615 ms
110,840 KB
testcase_34 AC 670 ms
111,236 KB
testcase_35 AC 676 ms
115,360 KB
testcase_36 AC 803 ms
112,836 KB
testcase_37 AC 684 ms
121,500 KB
testcase_38 AC 850 ms
123,124 KB
testcase_39 AC 701 ms
125,872 KB
testcase_40 AC 697 ms
125,224 KB
testcase_41 AC 722 ms
134,040 KB
testcase_42 AC 670 ms
134,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
e=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  e[a]+=[b]
  e[b]+=[a]
m=int(input())
x=[0]*n
y=[0]*n
for s in list(map(int,input().split())):
  x[s-1]+=1
  for t in e[s-1]:
    x[t]+=1
  y[s-1]=1
X=0
v=[0]*n
u=[0]*n
p=[0]*n
g=[0]*n
q=[0]
while len(q)>0:
  s=q[-1]
  if v[s]==0:
    v[s]=1
    X+=x[s]>=1
  while g[s]<len(e[s]):
    t=e[s][g[s]]
    if v[t]==0:
      break
    g[s]+=1
  if g[s]<len(e[s]):
    q+=[t]
    p[t]=s
  else:
    for t in e[s]:
      if t!=p[s]:
        u[s]+=x[t]==1
    q.pop()
for i in range(n):
  a=0
  s=i
  for t in e[s]:
    if t!=p[s]:
      if (y[s],y[t])==(0,0):
        pass
      if (y[s],y[t])==(0,1):
        a+=u[t]+(x[t]==1)
      if (y[s],y[t])==(1,0):
        a+=(x[t]==1)
      if (y[s],y[t])==(1,1):
        a+=u[t]+(x[t]==2)
    elif s!=0:
      if (y[s],y[p[s]])==(0,0):
        pass
      if (y[s],y[p[s]])==(0,1):
        a+=u[t]+(x[p[s]]==1)-(x[s]==1)
        if p[s]!=0:
          a+=(x[p[p[s]]]==1)
      if (y[s],y[p[s]])==(1,0):
        a+=(x[p[s]]==1)
      if (y[s],y[p[s]])==(1,1):
        a+=u[p[s]]+(x[p[s]]==2)
        if p[s]!=0:
          a+=(x[p[p[s]]]==1)
  a+=x[s]>0
  print(X-a)
0