結果

問題 No.2464 To DAG
ユーザー mymelochanmymelochan
提出日時 2023-09-08 22:44:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 768 ms / 2,000 ms
コード長 1,539 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 143,348 KB
最終ジャッジ日時 2023-09-08 22:45:10
合計ジャッジ時間 26,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,448 KB
testcase_01 AC 94 ms
71,448 KB
testcase_02 AC 593 ms
143,348 KB
testcase_03 AC 768 ms
129,000 KB
testcase_04 AC 636 ms
119,516 KB
testcase_05 AC 652 ms
116,340 KB
testcase_06 AC 618 ms
115,024 KB
testcase_07 AC 509 ms
109,308 KB
testcase_08 AC 580 ms
104,864 KB
testcase_09 AC 546 ms
98,696 KB
testcase_10 AC 488 ms
96,092 KB
testcase_11 AC 430 ms
92,216 KB
testcase_12 AC 337 ms
90,232 KB
testcase_13 AC 408 ms
91,136 KB
testcase_14 AC 492 ms
110,800 KB
testcase_15 AC 647 ms
126,572 KB
testcase_16 AC 391 ms
104,852 KB
testcase_17 AC 169 ms
83,492 KB
testcase_18 AC 288 ms
96,172 KB
testcase_19 AC 91 ms
71,436 KB
testcase_20 AC 93 ms
71,564 KB
testcase_21 AC 91 ms
71,320 KB
testcase_22 AC 296 ms
93,220 KB
testcase_23 AC 348 ms
92,240 KB
testcase_24 AC 299 ms
93,456 KB
testcase_25 AC 324 ms
93,360 KB
testcase_26 AC 235 ms
89,116 KB
testcase_27 AC 695 ms
132,200 KB
testcase_28 AC 688 ms
131,740 KB
testcase_29 AC 681 ms
127,780 KB
testcase_30 AC 614 ms
99,628 KB
testcase_31 AC 470 ms
94,852 KB
testcase_32 AC 440 ms
93,692 KB
testcase_33 AC 693 ms
125,204 KB
testcase_34 AC 393 ms
102,688 KB
testcase_35 AC 281 ms
95,356 KB
testcase_36 AC 267 ms
95,676 KB
testcase_37 AC 232 ms
93,556 KB
testcase_38 AC 257 ms
93,300 KB
testcase_39 AC 118 ms
84,244 KB
testcase_40 AC 117 ms
84,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#############################################################

import sys
sys.setrecursionlimit(10**7)

from heapq import heappop,heappush
from collections import deque,defaultdict,Counter
from bisect import bisect_left, bisect_right
from itertools import product,combinations,permutations

ipt = sys.stdin.readline

def iin():
    return int(ipt())
def lmin():
    return list(map(int,ipt().split()))

MOD = 998244353
#############################################################

N,M = lmin()
ans = [[] for _ in range(N)]
G = [[] for _ in range(N)]
for _ in range(M):
    u,v = lmin()
    u,v = u-1,v-1
    G[u].append(v)

f = [0]*N
for i in range(N):
    if len(G[i]) == 0:
        continue
    cur = i
    E = []
    f[cur] = 1
    while True:
        #print(cur,E,G)
        #print(f)
        if not G[cur]:
            if not E:
                break
            else:
                u,v = E.pop()
                ans[u].append(v)
                f[cur] = 0
                cur = u
        else:
            nxt = G[cur].pop()
            if f[nxt]:
                tmp = cur
                f[cur] = 0
                while True:
                    u,v = E.pop()
                    tmp = u
                    if tmp == nxt:
                        break
                    f[tmp] = 0
                cur = tmp
            else:
                f[nxt] = 1
                E.append((cur,nxt))
                cur = nxt
    f[i] = 0

print(N,sum(len(g) for g in ans))
for i in range(N):
    for j in ans[i]:
        print(i+1,j+1)
0