import
java.util.*;
public
class
Main {
static
List<Pair<Integer, Integer>> GenerateBinaryTree(
int
n) {
Random rand =
new
Random();
List<Integer> options = Arrays.asList(
0
,
1
,
2
);
Map<Integer, Integer> mp =
new
HashMap<>();
Queue<Integer> q =
new
LinkedList<>();
List<Pair<Integer, Integer>> v =
new
ArrayList<>();
int
count =
1
;
q.offer(
1
);
mp.put(
1
,
1
);
while
(!q.isEmpty() || count < n) {
int
front =
0
;
if
(!q.isEmpty()) {
front = q.poll();
}
int
numberOfChilds = options.get(rand.nextInt(options.size()));
if
(count >= n)
continue
;
while
(numberOfChilds-- >
0
) {
int
child = rand.nextInt(n) +
1
;
while
(mp.containsKey(child)) {
child++;
if
(child > n) {
child =
1
;
}
}
count++;
mp.put(child,
1
);
v.add(
new
Pair<>(front, child));
q.offer(child);
if
(count == n)
break
;
}
}
Collections.shuffle(v);
return
v;
}
static
void
PrintTree(
int
n, List<Pair<Integer, Integer>> v) {
System.out.println(n);
for
(Pair<Integer, Integer> edge : v) {
System.out.println(edge.getKey() +
" "
+ edge.getValue());
}
}
public
static
void
main(String[] args) {
Random rand =
new
Random();
int
n = rand.nextInt(
7
) +
3
;
List<Pair<Integer, Integer>> v = GenerateBinaryTree(n);
PrintTree(n, v);
}
static
class
Pair<K, V> {
private
K key;
private
V value;
public
Pair(K key, V value) {
this
.key = key;
this
.value = value;
}
public
K getKey() {
return
key;
}
public
V getValue() {
return
value;
}
}
public
static
class
Extensions {
public
static
<T>
void
shuffle(List<T> list) {
Random rand =
new
Random();
for
(
int
i = list.size() -
1
; i >
0
; i--) {
int
j = rand.nextInt(i +
1
);
T temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
}