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+
3+ read = lambda : sys .stdin .readline ().rstrip ()
4+
5+
6+ class Problem :
7+ def __init__ (self ):
8+ self .n , self .m = map (int , read ().split ())
9+ self .queries = []
10+ for line in [read ().split () for _ in range (self .m )]:
11+ self .queries .append ((line [0 ] == "?" , list (map (int , line [1 :]))))
12+
13+ self .parent , self .weight = list (range (self .n + 1 )), [0 ] * (self .n + 1 )
14+
15+ def solve (self ) -> None :
16+ for is_query , args in self .queries :
17+ if not is_query :
18+ self .union (* args )
19+ continue
20+
21+ x , y = args
22+ print ("UNKNOWN" if self .find (x ) != self .find (y ) else self .weight [y ] - self .weight [x ])
23+
24+ def find (self , num : int ) -> int :
25+ path = []
26+ while self .parent [num ] != num :
27+ path .append (num )
28+ num = self .parent [num ]
29+
30+ for node in reversed (path ):
31+ self .weight [node ] += self .weight [self .parent [node ]]
32+ self .parent [node ] = num
33+
34+ return num
35+
36+ def union (self , x : int , y : int , weight : int ) -> None :
37+ root_x , root_y = self .find (x ), self .find (y )
38+ if root_x != root_y :
39+ self .parent [root_y ] = root_x
40+ self .weight [root_y ] = self .weight [x ] - self .weight [y ] + weight
41+
42+
43+ if __name__ == "__main__" :
44+ while True :
45+ try :
46+ Problem ().solve ()
47+ except StopIteration :
48+ break
49+ except (EOFError , ValueError ):
50+ break
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "input" : [
4+ " 2 2" ,
5+ " ! 1 2 1" ,
6+ " ? 1 2" ,
7+ " 2 2" ,
8+ " ! 1 2 1" ,
9+ " ? 2 1" ,
10+ " 4 7" ,
11+ " ! 1 2 100" ,
12+ " ? 2 3" ,
13+ " ! 2 3 100" ,
14+ " ? 2 3" ,
15+ " ? 1 3" ,
16+ " ! 4 3 150" ,
17+ " ? 4 1" ,
18+ " 0 0"
19+ ],
20+ "expected" : [
21+ " 1" ,
22+ " -1" ,
23+ " UNKNOWN" ,
24+ " 100" ,
25+ " 200" ,
26+ " -50"
27+ ]
28+ }
29+ ]
Original file line number Diff line number Diff line change 1+ import json
2+ import os .path
3+ import unittest
4+ from io import StringIO
5+ from unittest .mock import patch
6+
7+ from parameterized import parameterized
8+
9+ from main import Problem
10+
11+
12+ def load_sample (filename : str ):
13+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), filename )
14+
15+ with open (path , "r" ) as file :
16+ return [(case ["input" ], case ["expected" ]) for case in json .load (file )]
17+
18+
19+ class TestCase (unittest .TestCase ):
20+ @parameterized .expand (load_sample ("sample.json" ))
21+ def test_case (self , case : str , expected : list [str ]):
22+ # When
23+ with (
24+ patch ("sys.stdin.readline" , side_effect = case ),
25+ patch ("sys.stdout" , new_callable = StringIO ) as output ,
26+ ):
27+ while True :
28+ try :
29+ Problem ().solve ()
30+ except StopIteration :
31+ break
32+
33+ result = output .getvalue ().rstrip ()
34+
35+ # Then
36+ self .assertEqual ("\n " .join (expected ), result )
37+
38+
39+ if __name__ == "__main__" :
40+ unittest .main ()
You can’t perform that action at this time.
0 commit comments