結果
| 問題 | 
                            No.439 チワワのなる木
                             | 
                    
| コンテスト | |
| ユーザー | 
                             古寺いろは
                         | 
                    
| 提出日時 | 2016-10-28 22:52:49 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 462 ms / 5,000 ms | 
| コード長 | 2,646 bytes | 
| コンパイル時間 | 837 ms | 
| コンパイル使用メモリ | 113,228 KB | 
| 実行使用メモリ | 81,348 KB | 
| 最終ジャッジ日時 | 2024-11-24 06:08:18 | 
| 合計ジャッジ時間 | 4,286 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 28 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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<int> Cs = new List<int>();
        List<int> Ws = new List<int>();
        foreach (var to in es[p])
        {
            if (to == pre) continue;
            var tmp = dfs(to, p);
            Cs.Add(tmp.Item1);
            Ws.Add(tmp.Item2);
        }
        int sumC = 0;
        int sumW = 0;
        foreach (var item in Cs)
        {
            sumC += item;
        }
        foreach (var item in Ws)
        {
            sumW += item;
        }
        if (S[p] == 'c') sumC++;
        else sumW++;
        if(p != 0)
        {
            Cs.Add(AllC - sumC);
            Ws.Add(AllW - sumW);
        }
        long addAns = 0;
        if (S[p] == 'w')
        {
            for (int i = 0; i < Cs.Count; i++)
            {
                addAns += (AllC - (long)Cs[i]) * Ws[i];
            }
        }
        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());
    }
}
            
            
            
        
            
古寺いろは