結果

問題 No.439 チワワのなる木
ユーザー 古寺いろは古寺いろは
提出日時 2016-10-28 22:48:17
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 386 ms / 5,000 ms
コード長 2,559 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 114,584 KB
実行使用メモリ 70,856 KB
最終ジャッジ日時 2024-05-03 07:57:14
合計ジャッジ時間 4,790 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,224 KB
testcase_01 AC 28 ms
26,208 KB
testcase_02 AC 27 ms
24,040 KB
testcase_03 AC 28 ms
24,352 KB
testcase_04 AC 28 ms
26,212 KB
testcase_05 AC 28 ms
26,272 KB
testcase_06 AC 28 ms
23,904 KB
testcase_07 AC 28 ms
24,100 KB
testcase_08 AC 28 ms
26,080 KB
testcase_09 AC 28 ms
24,100 KB
testcase_10 AC 27 ms
24,100 KB
testcase_11 AC 28 ms
23,968 KB
testcase_12 AC 28 ms
25,960 KB
testcase_13 AC 28 ms
23,988 KB
testcase_14 AC 29 ms
23,840 KB
testcase_15 AC 30 ms
24,232 KB
testcase_16 AC 31 ms
24,124 KB
testcase_17 AC 30 ms
24,456 KB
testcase_18 AC 170 ms
35,332 KB
testcase_19 AC 164 ms
35,304 KB
testcase_20 AC 222 ms
39,820 KB
testcase_21 AC 80 ms
33,184 KB
testcase_22 AC 70 ms
32,632 KB
testcase_23 AC 307 ms
43,736 KB
testcase_24 AC 386 ms
70,856 KB
testcase_25 AC 231 ms
43,748 KB
testcase_26 AC 226 ms
45,284 KB
testcase_27 AC 306 ms
68,480 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Iroha
{
    public Iroha() { }
    public static int Main()
    {
        new Iroha().calc();
        return 0;
    }

    Scanner cin;

    int N;
    string S;
    List<int>[] es;
    long ans;
    int AllC, AllW;

    void calc()
    {
        cin = new Scanner();
        N = cin.nextInt();
        S = cin.next();
        AllC = AllW = 0;
        foreach (var c in S)
        {
            if (c == 'c') AllC++;
            else AllW++;
        }

        es = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            es[i] = new List<int>();
        }
        for (int i = 0; i < N - 1; i++)
        {
            int a = cin.nextInt() - 1;
            int b = cin.nextInt() - 1;
            es[a].Add(b);
            es[b].Add(a);
        }
        ans = 0;
        dfs(0, -1);
        Console.WriteLine(ans);
    }

    Tuple<int, int> dfs(int p, int pre)
    {
        List<Tuple<int, int>> lt = new List<Tuple<int, int>>();
        foreach (var to in es[p])
        {
            if (to == pre) continue;
            lt.Add(dfs(to, p));
        }

        int sumC = 0;
        int sumW = 0;
        foreach (var item in lt)
        {
            sumC += item.Item1;
            sumW += item.Item2;
        }
        if (S[p] == 'c') sumC++;
        else sumW++;

        if(p != 0)
        {
            lt.Add(Tuple.Create(AllC - sumC, AllW - sumW));
        }

        long addAns = 0;
        if (S[p] == 'w')
        {
            AllW--;

            foreach (var item in lt)
            {
                addAns += (AllC - (long)item.Item1) * item.Item2;
            }

            AllW++;
        }
        ans += addAns;
        //Console.WriteLine(p + " " + addAns + " " + sumC + " " + sumW);

        return Tuple.Create(sumC, sumW);
    }
}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        i = 0;
        return next();
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}
0