File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
bona1122/[week6]Set/Set_repr Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ const readline = require ( "readline" )
2+ const rl = readline . createInterface ( {
3+ input : process . stdin ,
4+ output : process . stdout ,
5+ } )
6+
7+ const input = [ ]
8+ rl . on ( "line" , ( line ) => {
9+ input . push ( line )
10+ } ) . on ( "close" , ( ) => {
11+ const [ n , m ] = input [ 0 ] . split ( " " ) . map ( Number )
12+ const parent = Array . from ( { length : n + 1 } , ( _ , idx ) => idx )
13+
14+ const findParent = ( x , parent ) => {
15+ if ( parent [ x ] !== x ) {
16+ parent [ x ] = findParent ( parent [ x ] , parent )
17+ }
18+ return parent [ x ]
19+ }
20+ const union = ( a , b ) => {
21+ a = findParent ( a , parent )
22+ b = findParent ( b , parent )
23+ if ( a < b ) parent [ b ] = a
24+ else parent [ a ] = b
25+ }
26+
27+ let result = ""
28+ for ( let i = 1 ; i <= m ; i ++ ) {
29+ let [ op , a , b ] = input [ i ] . split ( " " ) . map ( Number )
30+ if ( op === 0 ) {
31+ union ( a , b )
32+ } else {
33+ if ( findParent ( a , parent ) === findParent ( b , parent ) ) result += "YES\n"
34+ else result += "NO\n"
35+ }
36+ }
37+
38+ console . log ( result . trim ( ) )
39+ process . exit ( )
40+ } )
Original file line number Diff line number Diff line change 1+ 7 8
2+ 0 1 3
3+ 1 1 7
4+ 0 7 6
5+ 1 7 1
6+ 0 3 7
7+ 0 4 2
8+ 0 1 1
9+ 1 1 1
You can’t perform that action at this time.
0 commit comments