結果

問題 No.778 クリスマスツリー
ユーザー silversmithsilversmith
提出日時 2019-05-03 15:36:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,634 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 78,244 KB
最終ジャッジ日時 2023-08-30 05:46:50
合計ジャッジ時間 4,350 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
13,520 KB
testcase_01 AC 23 ms
9,068 KB
testcase_02 AC 22 ms
9,180 KB
testcase_03 AC 23 ms
9,132 KB
testcase_04 AC 23 ms
9,216 KB
testcase_05 AC 23 ms
9,068 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#import sys
#sys.setrecursionlimit(1000)
import queue

class sorted_list:
    def __init__(self):
        self.list = []
    
    def _idx(self,val):
        if len(self.list) == 0:
            return 0
        else:
            left = 0
            right = len(self.list) -1
            mid = int((left + right)/2)
            if val < self.list[left]:
                return 0
            elif val > self.list[right]:
                return len(self.list)
            else:
                while right -left > 1:
                    mid = int((left + right)/2)
                    if val < self.list[mid]:
                        right = mid
                    else:
                        left = mid
                return mid if val < self.list[mid] else mid +1

    def insert(self,val):
        idx = self._idx(val)
        if idx < len(self.list):
            self.list.insert(self._idx(val), val)
        else:
            self.list.append(val)
    
    def remove(self, val):
        self.list.remove(val)

    def order_of_key(self, val):
        return self._idx(val)




class Node:
    def __init__(self):
        #self.parent_Node = None
        self.child_Node = []
        self.visited_cnt = 0
        self.dns = 0
        self.child_Node_visited_idx = -1
         

class tree:
    def __init__(self, Nodes):
        self.Nodes = Nodes
        self.sorted_list=sorted_list()
    def push_edge(self,low, upper):
        self.Nodes[upper].child_Node.append(low)
    def dns(self, val):
        stack = []
        stack.append(val)
        #self.sorted_list.insert(val)
        while len(stack) > 0:
            v = stack.pop()
            self.Nodes[v].visited_cnt += 1
            if len(self.Nodes[v].child_Node) == 0:
                self.Nodes[v].dns = self.sorted_list.order_of_key(v)
            elif self.Nodes[v].visited_cnt == len(self.Nodes[v].child_Node) + 1:
                self.sorted_list.remove(v)
                self.Nodes[v].dns = self.sorted_list.order_of_key(v)
                self.Nodes[v].dns += sum([self.Nodes[node].dns for node in self.Nodes[v].child_Node])
            else:
                stack.append(v)
                stack.append(self.Nodes[v].child_Node[self.Nodes[v].child_Node_visited_idx +1])
                if self.Nodes[v].child_Node_visited_idx == -1:
                    self.sorted_list.insert(v)
                self.Nodes[v].child_Node_visited_idx += 1

        return self.Nodes[val].dns
    

N = int(input())
A = map(int, input().split())


t = tree([Node() for i in range(N)])
for idx, a in enumerate(A):
    t.push_edge(idx + 1, a)
A = None
N = None
print(t.dns(0))
0