結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tnakao0123tnakao0123
提出日時 2023-04-22 23:13:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,014 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 53,208 KB
実行使用メモリ 18,176 KB
最終ジャッジ日時 2024-04-25 00:54:04
合計ジャッジ時間 5,838 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,320 KB
testcase_01 AC 5 ms
8,448 KB
testcase_02 AC 5 ms
8,448 KB
testcase_03 AC 5 ms
8,448 KB
testcase_04 AC 5 ms
8,320 KB
testcase_05 AC 5 ms
8,448 KB
testcase_06 AC 5 ms
8,320 KB
testcase_07 AC 5 ms
8,448 KB
testcase_08 AC 5 ms
8,448 KB
testcase_09 AC 5 ms
8,576 KB
testcase_10 AC 6 ms
8,448 KB
testcase_11 AC 77 ms
17,440 KB
testcase_12 AC 77 ms
17,696 KB
testcase_13 AC 76 ms
17,392 KB
testcase_14 AC 72 ms
17,568 KB
testcase_15 AC 81 ms
17,792 KB
testcase_16 AC 58 ms
14,336 KB
testcase_17 AC 58 ms
14,208 KB
testcase_18 AC 5 ms
8,448 KB
testcase_19 AC 87 ms
17,792 KB
testcase_20 AC 92 ms
17,664 KB
testcase_21 AC 90 ms
17,664 KB
testcase_22 AC 77 ms
18,048 KB
testcase_23 AC 87 ms
17,408 KB
testcase_24 AC 88 ms
17,664 KB
testcase_25 AC 92 ms
17,664 KB
testcase_26 AC 101 ms
17,920 KB
testcase_27 AC 85 ms
17,792 KB
testcase_28 AC 75 ms
17,920 KB
testcase_29 AC 79 ms
18,176 KB
testcase_30 AC 99 ms
17,792 KB
testcase_31 AC 83 ms
18,048 KB
testcase_32 AC 92 ms
17,664 KB
testcase_33 AC 89 ms
17,792 KB
testcase_34 AC 83 ms
17,280 KB
testcase_35 AC 98 ms
17,792 KB
testcase_36 AC 99 ms
17,792 KB
testcase_37 AC 87 ms
17,152 KB
testcase_38 AC 101 ms
17,792 KB
testcase_39 AC 83 ms
18,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2205.cc:  No.2205 Lights Out on Christmas Tree - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_N];
int cs[MAX_N], ps[MAX_N], ss[MAX_N], cis[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
    nbrs[v].push_back(u);
  }

  for (int i = 0; i < n; i++) scanf("%d", cs + i);

  ps[0] = -1;
  for (int u = 0; u >= 0;) {
    vi &nbru = nbrs[u];
    int up = ps[u];

    if (cis[u] < nbru.size()) {
      int v = nbru[cis[u]++];
      if (v != up) {
	ps[v] = u;
	u = v;
      }
    }
    else {
      if (up >= 0) {
	ss[up] += ss[u];
	if (! cs[u]) {
	  ss[up]++;
	  cs[u] ^= 1, cs[up] ^= 1;
	}
      }
      u = up;
    }
  }

  printf("%d\n", cs[0] ? ss[0] : -1);
  return 0;
}
0