結果

問題 No.330 Eigenvalue Decomposition
ユーザー SI1000-githubSI1000-github
提出日時 2022-07-07 15:06:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,156 ms / 5,000 ms
コード長 2,345 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 86,284 KB
実行使用メモリ 230,420 KB
最終ジャッジ日時 2023-08-26 00:35:35
合計ジャッジ時間 11,595 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 284 ms
132,832 KB
testcase_01 AC 71 ms
71,264 KB
testcase_02 AC 113 ms
78,344 KB
testcase_03 AC 285 ms
133,120 KB
testcase_04 AC 72 ms
70,976 KB
testcase_05 AC 111 ms
78,292 KB
testcase_06 AC 536 ms
220,112 KB
testcase_07 AC 90 ms
71,204 KB
testcase_08 AC 332 ms
155,036 KB
testcase_09 AC 343 ms
129,220 KB
testcase_10 AC 71 ms
71,372 KB
testcase_11 AC 182 ms
91,280 KB
testcase_12 AC 411 ms
153,320 KB
testcase_13 AC 73 ms
71,488 KB
testcase_14 AC 190 ms
93,704 KB
testcase_15 AC 184 ms
93,156 KB
testcase_16 AC 71 ms
71,156 KB
testcase_17 AC 137 ms
80,284 KB
testcase_18 AC 276 ms
102,868 KB
testcase_19 AC 73 ms
71,080 KB
testcase_20 AC 147 ms
81,448 KB
testcase_21 AC 1,156 ms
230,420 KB
testcase_22 AC 142 ms
78,196 KB
testcase_23 AC 178 ms
86,324 KB
testcase_24 AC 335 ms
129,040 KB
testcase_25 AC 73 ms
71,088 KB
testcase_26 AC 109 ms
78,276 KB
testcase_27 AC 124 ms
88,652 KB
testcase_28 AC 71 ms
71,136 KB
testcase_29 AC 107 ms
84,080 KB
testcase_30 AC 73 ms
71,412 KB
testcase_31 AC 71 ms
71,216 KB
testcase_32 AC 71 ms
71,336 KB
testcase_33 AC 72 ms
71,256 KB
testcase_34 AC 71 ms
71,348 KB
testcase_35 AC 72 ms
71,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
# -*- coding: utf-8 -*-

# import
from sys import stdin, setrecursionlimit
setrecursionlimit(10**8)

def int_map():
    return map(int,input().split())

def int_list():
    return list(map(int,input().split()))

def int_mtx(N):
    x = [list(map(int, stdin.readline().split())) for _ in range(N)]
    return x

def str_mtx(N):
    x = [list(stdin.readline()[:-1]) for _ in range(N)]
    return x

def print_space(l):
    return print(" ".join([str(x) for x in l]))



"""    main code    """
N,M = int_map()


def scc(N, G, RG):

    # 一回目のDFS (順方向のグラフ G を用いる)
    # DFSにおいてスタックから除かれる頂点の順番 order を記録することが目的
    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)

    # 二回目のDFS (逆方向のグラフ RG を用いる)
    # 上で得た order を逆順にたどって,行き詰まるまでが一つのグループ扱いになる
    def rdfs(s, label):
        group[s] = label
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, label)



    order = []
    used = [0]*N

    # 一回目のDFS    
    for i in range(N):
        if not used[i]:
            dfs(i)
    
    used = [0]*N
    label = 0
    group = [None]*N

    # 二回目のDFS    
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    
    return label, group


# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    for i in range(label):
        G0[i] = list(G0[i])

    return G0, GP




UV = []

for _ in range(M):
    a,b,c = int_map()
    UV.append([a-1,b-1])
    UV.append([b-1,a-1])


G = [[] for _ in range(N)]
RG = [[] for _ in range(N)]
for uvi in UV:
    G[uvi[0]].append(uvi[1])
    RG[uvi[1]].append(uvi[0])

####  必ず有向化!!!!  #### 
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

label_num, group = scc(N, G, RG)


print(label_num)
0