結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー 👑 kakel-sankakel-san
提出日時 2023-02-03 22:12:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,405 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 68,024 KB
実行使用メモリ 91,000 KB
最終ジャッジ日時 2023-09-15 18:08:35
合計ジャッジ時間 16,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
21,684 KB
testcase_01 AC 65 ms
23,632 KB
testcase_02 AC 64 ms
21,648 KB
testcase_03 AC 64 ms
23,648 KB
testcase_04 AC 64 ms
21,584 KB
testcase_05 AC 64 ms
21,720 KB
testcase_06 AC 64 ms
23,644 KB
testcase_07 AC 65 ms
21,724 KB
testcase_08 AC 64 ms
21,620 KB
testcase_09 AC 64 ms
23,704 KB
testcase_10 AC 64 ms
21,740 KB
testcase_11 AC 403 ms
67,288 KB
testcase_12 AC 399 ms
67,356 KB
testcase_13 AC 401 ms
67,236 KB
testcase_14 AC 383 ms
67,204 KB
testcase_15 AC 419 ms
91,000 KB
testcase_16 AC 282 ms
53,936 KB
testcase_17 AC 288 ms
53,972 KB
testcase_18 AC 65 ms
25,800 KB
testcase_19 AC 404 ms
65,892 KB
testcase_20 AC 405 ms
68,728 KB
testcase_21 AC 405 ms
68,568 KB
testcase_22 AC 394 ms
67,756 KB
testcase_23 AC 404 ms
68,532 KB
testcase_24 AC 404 ms
67,248 KB
testcase_25 AC 407 ms
66,604 KB
testcase_26 AC 415 ms
66,596 KB
testcase_27 AC 410 ms
70,176 KB
testcase_28 AC 395 ms
65,692 KB
testcase_29 AC 402 ms
70,108 KB
testcase_30 AC 416 ms
68,556 KB
testcase_31 AC 410 ms
69,852 KB
testcase_32 AC 410 ms
67,064 KB
testcase_33 AC 405 ms
69,100 KB
testcase_34 AC 402 ms
66,520 KB
testcase_35 AC 417 ms
68,640 KB
testcase_36 AC 421 ms
64,720 KB
testcase_37 AC 406 ms
68,564 KB
testcase_38 AC 427 ms
68,588 KB
testcase_39 AC 410 ms
67,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = int.Parse(ReadLine());
        var map = NMap(n - 1);
        var c = NList;
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0]].Add(edge[1]);
            tree[edge[1]].Add(edge[0]);
        }
        var colors = c.Select(ci => ci == 0).ToArray();
        var ans = 0;
        DFS(-1, 0, tree, colors, ref ans);
        WriteLine(colors[0] ? -1 : ans);
    }
    static void DFS(int prev, int cur, List<int>[] tree, bool[] colors, ref int ans)
    {
        foreach (var next in tree[cur])
        {
            if (prev == next) continue;
            DFS(cur, next, tree, colors, ref ans);
            if (colors[next])
            {
                ++ans;
                colors[next] = false;
                colors[cur] = !colors[cur];
            }
        }
    }
}
0