結果

問題 No.439 チワワのなる木
ユーザー 14番14番
提出日時 2016-12-24 02:22:43
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,291 bytes
コンパイル時間 4,176 ms
コンパイル使用メモリ 112,028 KB
実行使用メモリ 27,836 KB
最終ジャッジ日時 2023-08-21 07:46:13
合計ジャッジ時間 16,661 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
19,572 KB
testcase_01 AC 67 ms
21,684 KB
testcase_02 AC 67 ms
21,596 KB
testcase_03 AC 67 ms
21,692 KB
testcase_04 AC 68 ms
19,636 KB
testcase_05 AC 67 ms
21,548 KB
testcase_06 AC 67 ms
21,824 KB
testcase_07 AC 69 ms
21,756 KB
testcase_08 AC 69 ms
21,688 KB
testcase_09 AC 68 ms
19,660 KB
testcase_10 AC 70 ms
23,700 KB
testcase_11 AC 68 ms
23,716 KB
testcase_12 AC 68 ms
21,728 KB
testcase_13 AC 79 ms
27,744 KB
testcase_14 AC 92 ms
25,780 KB
testcase_15 AC 278 ms
25,796 KB
testcase_16 AC 2,233 ms
27,836 KB
testcase_17 AC 1,664 ms
23,800 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        int nodeCount = int.Parse(Reader.ReadLine());
        this.TreeList = new TreeNode[nodeCount+1];
        string cwwText = Reader.ReadLine();
        for(int i=0; i<cwwText.Length; i++) {
            TreeNode nd = new TreeNode(i+1, cwwText[i]);
            this.TreeList[i+1] = nd;
            if(nd.Type == TreeNodeType.C) {
                this.CList.Add(nd);
            }
        }
        for(int i=0; i<cwwText.Length-1; i++) {
            int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).OrderBy(a=>a).ToArray();
            int idx1 = inpt[0];
            int idx2 = inpt[1];
            if(TreeList[idx2].Parent == null) {
                TreeList[idx1].Add(TreeList[idx2]);
            } else if(TreeList[idx1].Parent == null) {
                TreeList[idx2].Add(TreeList[idx1]);
            } else {
                TreeList[idx2].UpsideDown();
                TreeList[idx1].Add(TreeList[idx2]);
            }
        }
        long ans = 0;
        foreach(TreeNode cnode in this.CList) {
            cnode.UpsideDown();
            ans+=GetAns(cnode, 2);
        }
        Console.WriteLine(ans);
    }


    private long GetAns(TreeNode nd, int remainW) {

        long ans = 0;
        if(nd.Type == TreeNodeType.W && remainW == 1) {
            ans+=1;
        }
        if(nd.Type == TreeNodeType.W && remainW == 2) {
            nd.Items.ForEach(a=>ans+=GetAns(a, remainW-1));
        }
        nd.Items.ForEach(a=>ans+=GetAns(a, remainW));
        return ans;
    }
    private TreeNode[] TreeList;

    private List<TreeNode> CList = new List<TreeNode>();

    public class TreeNode {
        public TreeNode Parent;
        public int ID;

        public TreeNodeType Type;

        public List<TreeNode> Items = new List<TreeNode>();

        public TreeNode UpsideDown() {
            if(this.Parent == null) {
                return null;
            }
            TreeNode target = this.Parent;
            target.Items.Remove(this);
            this.Parent = null;
            target.UpsideDown();
            this.Add(target);
            return target;
        }
        public void Add(TreeNode target) {
            target.Parent = this;
            this.Items.Add(target);
        }

        public TreeNode(int id, char c) {
            this.ID = id;
            this.Type = c=='c'?TreeNodeType.C:TreeNodeType.W;
        }
    }

    public enum TreeNodeType {
        C,
        W,
    }

    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"

3
wcw
1 2
3 2


";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0