#K816. Direct Edge Checker in an Undirected Graph
Direct Edge Checker in an Undirected Graph
Direct Edge Checker in an Undirected Graph
You are given an undirected graph represented by its adjacency matrix \(A\). The graph has \(N\) vertices (numbered from 1 to \(N\)) and the matrix is defined as:
\(A[i][j] = 1\) if there is an edge between vertices \(i\) and \(j\), and \(A[i][j] = 0\) otherwise.
Your task is to answer \(Q\) queries. Each query consists of two integers \(u\) and \(v\) (1-indexed). For each query, output YES
if there is a direct edge between vertices \(u\) and \(v\), otherwise output NO
.
Note: Since the graph is undirected, the edge between vertices \(u\) and \(v\) is the same as the edge between \(v\) and \(u\>.
inputFormat
The first line of input contains an integer (T), the number of test cases. Each test case is described as follows:
- The first line contains two integers (N) and (Q), where (N) is the number of vertices and (Q) is the number of queries.
- The next (N) lines each contain (N) space-separated integers, representing the adjacency matrix of the graph.
- The following (Q) lines each contain two integers (u) and (v) representing a query to check if a direct edge exists between vertex (u) and vertex (v).
All vertices are 1-indexed.
outputFormat
For each query in each test case, output a single line containing YES
if there is a direct edge between the queried vertices, or NO
otherwise.## sample
1
4 3
0 1 0 0
1 0 1 0
0 1 0 1
0 0 1 0
1 2
1 3
4 2
YES
NO
NO
</p>