結果

問題 No.2780 The Bottle Imp
ユーザー katonyonkokatonyonko
提出日時 2024-06-07 22:21:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 3,531 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 126,720 KB
最終ジャッジ日時 2024-06-08 10:32:58
合計ジャッジ時間 8,313 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,296 KB
testcase_01 AC 46 ms
55,424 KB
testcase_02 AC 44 ms
54,912 KB
testcase_03 AC 45 ms
55,040 KB
testcase_04 AC 44 ms
54,784 KB
testcase_05 AC 46 ms
55,296 KB
testcase_06 AC 44 ms
55,116 KB
testcase_07 AC 237 ms
92,544 KB
testcase_08 AC 244 ms
92,032 KB
testcase_09 AC 241 ms
91,520 KB
testcase_10 AC 241 ms
92,704 KB
testcase_11 AC 235 ms
92,928 KB
testcase_12 AC 343 ms
107,888 KB
testcase_13 AC 351 ms
110,448 KB
testcase_14 AC 139 ms
81,536 KB
testcase_15 AC 132 ms
81,656 KB
testcase_16 AC 137 ms
81,280 KB
testcase_17 AC 139 ms
81,792 KB
testcase_18 AC 136 ms
81,664 KB
testcase_19 AC 136 ms
81,408 KB
testcase_20 AC 129 ms
81,408 KB
testcase_21 AC 133 ms
81,732 KB
testcase_22 AC 161 ms
81,560 KB
testcase_23 AC 137 ms
81,724 KB
testcase_24 AC 221 ms
89,600 KB
testcase_25 AC 303 ms
103,168 KB
testcase_26 AC 198 ms
86,144 KB
testcase_27 AC 137 ms
91,136 KB
testcase_28 AC 135 ms
91,196 KB
testcase_29 AC 149 ms
91,008 KB
testcase_30 AC 130 ms
85,632 KB
testcase_31 AC 187 ms
103,780 KB
testcase_32 AC 89 ms
78,944 KB
testcase_33 AC 200 ms
126,720 KB
testcase_34 AC 221 ms
121,344 KB
testcase_35 AC 93 ms
78,200 KB
testcase_36 AC 46 ms
54,912 KB
testcase_37 AC 47 ms
55,424 KB
testcase_38 AC 96 ms
78,608 KB
testcase_39 AC 173 ms
103,268 KB
testcase_40 AC 171 ms
103,424 KB
testcase_41 AC 171 ms
103,416 KB
testcase_42 AC 47 ms
55,296 KB
testcase_43 AC 45 ms
55,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import io
import sys
from collections import defaultdict, deque, Counter
from itertools import permutations, combinations, accumulate
from heapq import heappush, heappop
from bisect import bisect_right, bisect_left
from math import gcd
import math

_INPUT = """\
6
5
2 2 3
3 1 3 5
2 1 2
1 5
2 2 4
3
1 2
1 1
0
4
1 3
0
1 4
1 2
10
3 5 3 2
1 8
3 7 8 4
0 
3 3 10 4
6 2 9 10 5 7 8
3 1 10 3
3 10 6 5
5 6 10 3 7 5
3 8 3 7
"""

class SCC:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.rev_graph = [[] for _ in range(n)]
        self.labels = [-1] * n
        self.lb_cnt = 0

    def add_edge(self, v, nxt_v):
        self.graph[v].append(nxt_v)
        self.rev_graph[nxt_v].append(v)

    def build(self):
        self.post_order = []
        self.used = [False] * self.n
        for v in range(self.n):
            if not self.used[v]:
                self._dfs(v)
        for v in reversed(self.post_order):
            if self.labels[v] == -1:
                self._rev_dfs(v)
                self.lb_cnt += 1

    def _dfs(self, v):
        stack = [v, 0]
        while stack:
            v, idx = stack[-2:]
            if not idx and self.used[v]:
                stack.pop()
                stack.pop()
            else:
                self.used[v] = True
                if idx < len(self.graph[v]):
                    stack[-1] += 1
                    stack.append(self.graph[v][idx])
                    stack.append(0)
                else:
                    stack.pop()
                    self.post_order.append(stack.pop())

    def _rev_dfs(self, v):
        stack = [v]
        self.labels[v] = self.lb_cnt
        while stack:
            v = stack.pop()
            for nxt_v in self.rev_graph[v]:
                if self.labels[nxt_v] != -1:
                    continue
                stack.append(nxt_v)
                self.labels[nxt_v] = self.lb_cnt

    def construct(self):
        self.dag = [[] for i in range(self.lb_cnt)]
        self.groups = [[] for i in range(self.lb_cnt)]
        for v, lb in enumerate(self.labels):
            for nxt_v in self.graph[v]:
                nxt_lb = self.labels[nxt_v]
                if lb == nxt_lb:
                    continue
                self.dag[lb].append(nxt_lb)
            self.groups[lb].append(v)
        return self.dag, self.groups

def input():
  return sys.stdin.readline()[:-1]

def solve(test):
  N=int(input())
  G=SCC(N)
  for i in range(N):
    A=list(map(int, input().split()))[1:]
    for j in range(len(A)):
      G.add_edge(i,A[j]-1)
  G.build()
  dag,groups=G.construct()
  if 0 not in groups[0]:
    print("No")
  else:
    ans='Yes'
    for i in range(len(dag)-1):
      if i+1 not in dag[i]:
        ans='No'
        break
    print(ans)

def random_input():
  from random import randint,shuffle
  N=randint(1,10)
  M=randint(1,N)
  A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)]
  shuffle(A)
  return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3

def simple_solve():
  return []

def main(test):
  if test==0:
    solve(0)
  elif test==1:
    sys.stdin = io.StringIO(_INPUT)
    case_no=int(input())
    for _ in range(case_no):
      solve(0)
  else:
    for i in range(1000):
      sys.stdin = io.StringIO(random_input())
      x=solve(1)
      y=simple_solve()
      if x!=y:
        print(i,x,y)
        print(*[line for line in sys.stdin],sep='')
        break

#0:提出用、1:与えられたテスト用、2:ストレステスト用
main(0)
0