File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import defaultdict , deque
3+
4+ read = lambda : sys .stdin .readline ().rstrip ()
5+
6+
7+ class Problem :
8+ def __init__ (self ):
9+ self .n , self .k = map (int , read ().split ())
10+ self .times = list (map (int , read ().split ()))
11+
12+ self .order , self .depth = defaultdict (list [int ]), [0 for _ in range (self .n )]
13+ for _ in range (self .k ):
14+ src , dest = map (int , read ().split ())
15+ self .order [src - 1 ].append (dest - 1 )
16+ self .depth [dest - 1 ] += 1
17+
18+ self .target = int (read ()) - 1
19+
20+ def solve (self ) -> None :
21+ dp , queue = self .times [:], deque (
22+ list (filter (lambda num : self .depth [num ] == 0 , range (self .n )))
23+ )
24+
25+ while queue :
26+ node = queue .popleft ()
27+
28+ for next_node in self .order [node ]:
29+ dp [next_node ] = max (dp [next_node ], dp [node ] + self .times [next_node ])
30+ self .depth [next_node ] -= 1
31+
32+ if self .depth [next_node ] == 0 :
33+ queue .append (next_node )
34+
35+ print (dp [self .target ])
36+
37+
38+ if __name__ == "__main__" :
39+ for _ in range (int (read ())):
40+ Problem ().solve ()
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "input" : [
4+ " 2" ,
5+ " 4 4" ,
6+ " 10 1 100 10" ,
7+ " 1 2" ,
8+ " 1 3" ,
9+ " 2 4" ,
10+ " 3 4" ,
11+ " 4" ,
12+ " 8 8" ,
13+ " 10 20 1 5 8 7 1 43" ,
14+ " 1 2" ,
15+ " 1 3" ,
16+ " 2 4" ,
17+ " 2 5" ,
18+ " 3 6" ,
19+ " 5 7" ,
20+ " 6 7" ,
21+ " 7 8" ,
22+ " 7"
23+ ],
24+ "expected" : [
25+ " 120" ,
26+ " 39"
27+ ]
28+ },
29+ {
30+ "input" : [
31+ " 5" ,
32+ " 3 2" ,
33+ " 1 2 3" ,
34+ " 3 2" ,
35+ " 2 1" ,
36+ " 1" ,
37+ " 4 3" ,
38+ " 5 5 5 5" ,
39+ " 1 2" ,
40+ " 1 3" ,
41+ " 2 3" ,
42+ " 4" ,
43+ " 5 10" ,
44+ " 100000 99999 99997 99994 99990" ,
45+ " 4 5" ,
46+ " 3 5" ,
47+ " 3 4" ,
48+ " 2 5" ,
49+ " 2 4" ,
50+ " 2 3" ,
51+ " 1 5" ,
52+ " 1 4" ,
53+ " 1 3" ,
54+ " 1 2" ,
55+ " 4" ,
56+ " 4 3" ,
57+ " 1 1 1 1" ,
58+ " 1 2" ,
59+ " 3 2" ,
60+ " 1 4" ,
61+ " 4" ,
62+ " 7 8" ,
63+ " 0 0 0 0 0 0 0" ,
64+ " 1 2" ,
65+ " 1 3" ,
66+ " 2 4" ,
67+ " 3 4" ,
68+ " 4 5" ,
69+ " 4 6" ,
70+ " 5 7" ,
71+ " 6 7" ,
72+ " 7"
73+ ],
74+ "expected" : [
75+ " 6" ,
76+ " 5" ,
77+ " 399990" ,
78+ " 2" ,
79+ " 0"
80+ ]
81+ }
82+ ]
Original file line number Diff line number Diff line change 1+ import json
2+ import os .path
3+ import sys
4+ import unittest
5+ from io import StringIO
6+ from unittest .mock import patch
7+
8+ from parameterized import parameterized
9+
10+ from main import Problem
11+
12+
13+ def load_sample (filename : str ):
14+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), filename )
15+
16+ with open (path , "r" ) as file :
17+ return [(case ["input" ], case ["expected" ]) for case in json .load (file )]
18+
19+
20+ class TestCase (unittest .TestCase ):
21+ @parameterized .expand (load_sample ("sample.json" ))
22+ def test_case (self , case : str , expected : list [str ]):
23+ # When
24+ with (
25+ patch ("sys.stdin.readline" , side_effect = case ),
26+ patch ("sys.stdout" , new_callable = StringIO ) as output ,
27+ ):
28+ for _ in range (int (sys .stdin .readline ().rstrip ())):
29+ Problem ().solve ()
30+
31+ result = output .getvalue ().rstrip ()
32+
33+ # Then
34+ self .assertEqual ("\n " .join (expected ), result )
35+
36+
37+ if __name__ == "__main__" :
38+ unittest .main ()
You can’t perform that action at this time.
0 commit comments