結果

問題 No.439 チワワのなる木
ユーザー 古寺いろは古寺いろは
提出日時 2016-10-28 23:04:41
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 274 ms / 5,000 ms
コード長 2,426 bytes
コンパイル時間 4,083 ms
コンパイル使用メモリ 109,232 KB
実行使用メモリ 51,004 KB
最終ジャッジ日時 2023-08-15 21:31:28
合計ジャッジ時間 8,755 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
20,776 KB
testcase_01 AC 61 ms
22,864 KB
testcase_02 AC 59 ms
18,588 KB
testcase_03 AC 59 ms
20,692 KB
testcase_04 AC 60 ms
20,628 KB
testcase_05 AC 61 ms
20,776 KB
testcase_06 AC 61 ms
22,672 KB
testcase_07 AC 60 ms
20,812 KB
testcase_08 AC 62 ms
22,900 KB
testcase_09 AC 62 ms
22,684 KB
testcase_10 AC 61 ms
18,844 KB
testcase_11 AC 61 ms
20,800 KB
testcase_12 AC 60 ms
20,688 KB
testcase_13 AC 61 ms
18,676 KB
testcase_14 AC 64 ms
20,872 KB
testcase_15 AC 62 ms
20,752 KB
testcase_16 AC 63 ms
22,800 KB
testcase_17 AC 64 ms
22,856 KB
testcase_18 AC 192 ms
31,980 KB
testcase_19 AC 190 ms
32,096 KB
testcase_20 AC 247 ms
36,496 KB
testcase_21 AC 108 ms
28,072 KB
testcase_22 AC 99 ms
24,904 KB
testcase_23 AC 262 ms
38,200 KB
testcase_24 AC 274 ms
48,620 KB
testcase_25 AC 234 ms
36,216 KB
testcase_26 AC 233 ms
36,260 KB
testcase_27 AC 188 ms
51,004 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)
    {
        long sumC = 0;
        long sumW = 0;
        long addAns = 0;
        foreach (var to in es[p])
        {
            if (to == pre) continue;
            var tmp = dfs(to, p);
            if(S[p] == 'w')
            {
                addAns += sumC * tmp.Item2;
                addAns += sumW * tmp.Item1;
            }
            sumC += tmp.Item1;
            sumW += tmp.Item2;
        }
        

        if (p != 0)
        {
            if (S[p] == 'w')
            {
                addAns += sumW * (AllC - sumC);
                addAns += sumC * (AllW - (sumW + 1));
            }
        }

        if (S[p] == 'c') sumC++;
        else sumW++;
        

        ans += addAns;

        return Tuple.Create((int)sumC, (int)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