結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー kakel-san
提出日時 2023-02-03 22:12:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 1,405 bytes
コンパイル時間 3,712 ms
コンパイル使用メモリ 106,240 KB
実行使用メモリ 88,812 KB
最終ジャッジ日時 2024-07-02 20:12:52
合計ジャッジ時間 15,905 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

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