File tree Expand file tree Collapse file tree 3 files changed +135
-0
lines changed
Expand file tree Collapse file tree 3 files changed +135
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+
3+ public class Main {
4+ public static void main (String [] args ) throws IOException {
5+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
6+ String [] parts = br .readLine ().split (" " );
7+
8+ int A = Integer .parseInt (parts [0 ]);
9+ int B = Integer .parseInt (parts [1 ]);
10+
11+ int count = 1 ;
12+ while (A < B ) {
13+ if (B % 10 == 1 ) {
14+ B /= 10 ;
15+ } else if (B % 2 == 0 ) {
16+ B /= 2 ;
17+ } else {
18+ break ;
19+ }
20+ count ++;
21+
22+ //System.out.println(B);
23+ }
24+
25+ int res = (A == B ) ? count : -1 ;
26+ System .out .println (res );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+ static int N , M ;
6+ static int [] inputs ;
7+ // static Set<int[]> set;
8+ static boolean [] visited ;
9+ static int [] selected ;
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
13+
14+ String [] parts = br .readLine ().split (" " );
15+
16+ N = Integer .parseInt (parts [0 ]);
17+ M = Integer .parseInt (parts [1 ]);
18+
19+ inputs = new int [N ];
20+ // set = new HashSet<>();
21+ visited = new boolean [N + 1 ];
22+ selected = new int [M ];
23+ String [] inputParts = br .readLine ().split (" " );
24+ for (int i =0 ; i <N ; i ++) {
25+ inputs [i ] = Integer .parseInt (inputParts [i ]);
26+ }
27+
28+ Arrays .sort (inputs );
29+ dfs (0 );
30+ }
31+ public static void dfs (int depth ) {
32+ if (depth == M ) {
33+ for (int num : selected ) {
34+ System .out .print (num +" " );
35+ }
36+ System .out .println ();
37+ return ;
38+ }
39+
40+ // 같은 depth 내에서 중복 선택이 되지 않도록
41+ int prev = -1 ;
42+ for (int i =0 ; i <N ; i ++) {
43+ if (!visited [i ] && prev != inputs [i ]) {
44+ visited [i ] = true ;
45+ selected [depth ] = inputs [i ];
46+ prev = inputs [i ];
47+ dfs (depth + 1 );
48+ visited [i ] = false ;
49+ }
50+ }
51+ }
52+ }
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+ static List <List <Integer >> graph ;
6+ static boolean [] visited ;
7+ static int [] parent ;
8+
9+ public static void main (String [] args ) throws IOException {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11+
12+ int N = Integer .parseInt (br .readLine ());
13+ graph = new ArrayList <>();
14+
15+ // 1~N
16+ for (int i =0 ; i <=N ; i ++) {
17+ graph .add (new ArrayList <>());
18+ }
19+
20+ for (int i =0 ; i <N -1 ; i ++) {
21+ String [] parts = br .readLine ().split (" " );
22+ int u = Integer .parseInt (parts [0 ]);
23+ int v = Integer .parseInt (parts [1 ]);
24+
25+ graph .get (u ).add (v );
26+ graph .get (v ).add (u );
27+ }
28+
29+ parent = new int [N + 1 ];
30+ visited = new boolean [N + 1 ];
31+ bfs (1 );
32+
33+ for (int i =2 ; i <=N ; i ++) {
34+ System .out .println (parent [i ]);
35+ }
36+ }
37+
38+ static public void bfs (int start ) {
39+ Queue <Integer > queue = new LinkedList <>();
40+ queue .offer (start );
41+ visited [start ] = true ;
42+
43+ while (!queue .isEmpty ()) {
44+ int parentNode = queue .poll ();
45+
46+ for (int child : graph .get (parentNode )) {
47+ if (!visited [child ]) {
48+ visited [child ] = true ;
49+ queue .offer (child );
50+ parent [child ] = parentNode ;
51+ }
52+ }
53+ }
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments