結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー 👑 chro_96chro_96
提出日時 2023-02-03 22:08:55
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,120 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 28,400 KB
実行使用メモリ 23,788 KB
最終ジャッジ日時 2023-09-15 18:02:45
合計ジャッジ時間 4,030 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,192 KB
testcase_01 AC 5 ms
11,140 KB
testcase_02 AC 4 ms
11,216 KB
testcase_03 AC 4 ms
11,248 KB
testcase_04 AC 4 ms
11,216 KB
testcase_05 AC 4 ms
11,144 KB
testcase_06 AC 3 ms
11,160 KB
testcase_07 AC 3 ms
11,184 KB
testcase_08 AC 4 ms
11,140 KB
testcase_09 AC 3 ms
11,172 KB
testcase_10 AC 4 ms
11,196 KB
testcase_11 AC 65 ms
11,216 KB
testcase_12 AC 64 ms
11,160 KB
testcase_13 AC 63 ms
11,148 KB
testcase_14 AC 58 ms
11,288 KB
testcase_15 AC 74 ms
23,788 KB
testcase_16 AC 44 ms
11,288 KB
testcase_17 AC 43 ms
11,220 KB
testcase_18 AC 4 ms
11,204 KB
testcase_19 AC 65 ms
11,204 KB
testcase_20 AC 69 ms
11,068 KB
testcase_21 AC 68 ms
11,200 KB
testcase_22 AC 63 ms
11,192 KB
testcase_23 AC 67 ms
11,248 KB
testcase_24 AC 65 ms
11,244 KB
testcase_25 AC 71 ms
11,204 KB
testcase_26 AC 79 ms
11,264 KB
testcase_27 AC 66 ms
11,104 KB
testcase_28 AC 61 ms
11,192 KB
testcase_29 AC 63 ms
11,288 KB
testcase_30 AC 77 ms
11,240 KB
testcase_31 AC 66 ms
11,156 KB
testcase_32 AC 64 ms
11,224 KB
testcase_33 AC 66 ms
11,100 KB
testcase_34 AC 64 ms
11,168 KB
testcase_35 AC 74 ms
11,288 KB
testcase_36 AC 74 ms
11,096 KB
testcase_37 AC 66 ms
11,160 KB
testcase_38 AC 78 ms
11,044 KB
testcase_39 AC 64 ms
11,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct list {
  int v;
  struct list *n;
} list;

int func (int v, list **e, int *c, int *visited) {
  list *l = e[v];
  int ans = 0;
  
  visited[v] = 1;
  while (l != NULL) {
    if (visited[l->v] <= 0) {
      ans += func(l->v, e, c, visited);
      if (c[l->v] <= 0) {
        ans++;
        c[l->v] = 1;
        c[v] = 1-c[v];
      }
    }
    l = l->n;
  }
  
  return ans;
}

int main () {
  int n = 0;
  int a = 0;
  int b = 0;
  int c[200000] = {};
  
  int res = 0;
  
  int ans = 0;
  int visited[200000] = {};
  
  list pool[399998] = {};
  int used = 0;
  
  list *e[200000] = {};
  
  res = scanf("%d", &n);
  for (int i = 0; i < n-1; i++) {
    res = scanf("%d", &a);
    res = scanf("%d", &b);
    a--;
    b--;
    pool[used].v = b;
    pool[used].n = e[a];
    e[a] = pool+used;
    used++;
    pool[used].v = a;
    pool[used].n = e[b];
    e[b] = pool+used;
    used++;
  }
  for (int i = 0; i < n; i++) {
    res = scanf("%d", c+i);
  }
  
  ans = func(0, e, c, visited);
  
  if (c[0] > 0) {
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
  
  return 0;
}
0