#K81192. Graph Query Operations
Graph Query Operations
Graph Query Operations
Problem Statement:
You are given an undirected graph with n nodes (numbered from 0 to n-1) and initially no edges. You need to process a series of queries that update the graph or ask for information about it. The queries can be of the following three types:
- 0 u v: Add an edge between node u and node v.
- 1 u v: Remove the edge between node u and node v.
- 2 u: Query the neighbors of node u. You must output the neighbors in ascending order, separated by a single space. If node u has no neighbors, output an empty line.
Note: Every query of type 2 should produce an output line. All operations are performed on an undirected graph so an edge added between u and v exists in both u's and v's neighbor lists.
The graph operations follow typical dynamic graph updates and queries. Handle the queries sequentially as they appear in the input.
The formula for output of a query of type 2 can be represented in LaTeX as:
\[ Output = \{ x \mid x \in Neighbors(u) \}\]inputFormat
The input is given via standard input (stdin). The first line contains two integers n and q, where n is the number of nodes and q is the number of queries. The following q lines each contain a query in one of the following formats:
0 u v 1 u v 2 u
outputFormat
For each query of type 2, output a line listing the neighbors of the specified node in ascending order, separated by a single space. If the node has no neighbors, output an empty line.## sample
5 3
0 0 1
0 0 2
2 0
1 2
</p>