import
java.util.*;
public
class
UnweightedTreeTestCasesGenerator {
static
final
int
RUN =
5
;
static
final
int
MAXNODE =
20
;
static
class
Tree {
int
V;
List<Integer>[] adj;
boolean
isCyclicUtil(
int
v,
boolean
[] visited,
boolean
[] recStack) {
if
(!visited[v]) {
visited[v] =
true
;
recStack[v] =
true
;
for
(
int
i : adj[v]) {
if
(!visited[i] && isCyclicUtil(i, visited, recStack))
return
true
;
else
if
(recStack[i])
return
true
;
}
}
recStack[v] =
false
;
return
false
;
}
public
Tree(
int
V) {
this
.V = V;
adj =
new
ArrayList[V];
for
(
int
i =
0
; i < V; i++)
adj[i] =
new
ArrayList<>();
}
public
void
addEdge(
int
v,
int
w) {
adj[v].add(w);
}
public
void
removeEdge(
int
v,
int
w) {
adj[v].remove(Integer.valueOf(w));
}
public
boolean
isCyclic() {
boolean
[] visited =
new
boolean
[V];
boolean
[] recStack =
new
boolean
[V];
Arrays.fill(visited,
false
);
Arrays.fill(recStack,
false
);
for
(
int
i =
0
; i < V; i++)
if
(isCyclicUtil(i, visited, recStack))
return
true
;
return
false
;
}
}
public
static
void
main(String[] args) {
HashSet<Pair<Integer, Integer>> container =
new
HashSet<>();
Random rand =
new
Random();
for
(
int
i =
1
; i <= RUN; i++) {
int
NUM = rand.nextInt(MAXNODE) +
1
;
System.out.println(NUM);
Tree t =
new
Tree(NUM);
for
(
int
j =
1
; j <= NUM -
1
; j++) {
int
a = rand.nextInt(NUM);
int
b = rand.nextInt(NUM);
Pair<Integer, Integer> p =
new
Pair<>(a, b);
t.addEdge(a, b);
while
(container.contains(p) || t.isCyclic()) {
t.removeEdge(a, b);
a = rand.nextInt(NUM);
b = rand.nextInt(NUM);
p =
new
Pair<>(a, b);
t.addEdge(a, b);
}
container.add(p);
}
for
(Pair<Integer, Integer> p : container)
System.out.println(p.getFirst() +
" "
+ p.getSecond());
container.clear();
System.out.println();
}
}
static
class
Pair<T, U> {
private
T first;
private
U second;
public
Pair(T first, U second) {
this
.first = first;
this
.second = second;
}
public
T getFirst() {
return
first;
}
public
U getSecond() {
return
second;
}
}
}