import java.util.
*;
public class Main {
// Function to print adjacency list
static void printAdjacencyList(Map<Integer, List<Integer>> adjList) {
for (Map.Entry<Integer, List<Integer>> entry : adjList.entrySet()) {
int vertex = entry.getKey();
List<Integer> neighbors = entry.getValue();
System.out.print("vertex " + vertex + ": ");
for (int neighbor : neighbors) {
System.out.print(neighbor + " ");
System.out.println();
// Function to create adjacency list
static Map<Integer, List<Integer>> createAdjacencyList(List<int[]> edges) {
Map<Integer, List<Integer>> adjList = new HashMap<>();
for (int[] edge : edges) {
int u = edge[0];
int v = edge[1];
// Add u to v's list and v to u's list
adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
adjList.computeIfAbsent(v, k -> new ArrayList<>()).add(u);
return adjList;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: Number of vertices
int numVertices = scanner.nextInt();
// Input: Edges (pairs of vertices)
List<int[]> edges = new ArrayList<>();
for (int i = 0; i < numVertices; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
edges.add(new int[]{u, v});
// Create adjacency list
Map<Integer, List<Integer>> adjacencyList = createAdjacencyList(edges);
// Output: Display the adjacency list
printAdjacencyList(adjacencyList);
scanner.close();
GRAPH ON MATRIX
import java.util.Scanner;
public class Main {
// Function to print adjacency matrix
static void printAdjacencyMatrix(int[][] adjMatrix, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(adjMatrix[i][j] + " ");
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: Number of vertices
int n = scanner.nextInt();
// Input: Adjacency matrix
int[][] adjMatrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
adjMatrix[i][j] = scanner.nextInt();
// Output: Display the adjacency matrix
printAdjacencyMatrix(adjMatrix, n);
scanner.close();