#DSU0000042. Đồ thị tốt (Good Graph)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đồ thị tốt (Good Graph)
Good Graph
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected graph with vertices and edges. Multiple edges and self-loops are allowed. There are also forbidden pairs : the two vertices of each such pair must never belong to the same connected component. The initial graph is guaranteed to satisfy every restriction.
You must answer queries. In query , consider adding one undirected edge joining and to the original graph. Determine whether the resulting graph still satisfies all forbidden pairs. If even one forbidden pair becomes connected, the answer is No.
Queries are independent: the tentative edge from one query is not retained when processing another query.
Input
Read and edges; then and distinct unordered forbidden pairs with ; then and query pairs with .
Output
For each query print Yes if the graph remains good, otherwise No.
Subtasks
- Subtask 1 (20 points): , , ; ; , ; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.
- Subtask 2 (30 points): , , ; ; , ; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.
- Subtask 3 (50 points): , , ; ; , ; forbidden pairs are distinct; the initial graph respects all forbidden pairs. Self-loops and multiple edges are allowed in the initial graph.
Examples
Input
6 6
1 2
2 3
2 3
3 1
5 4
5 5
3
1 5
2 6
4 3
4
2 5
2 6
5 6
5 4
Output
No
No
Yes
Yes
Explanation
The initial connected components are , , and . Adding brings the forbidden pair into one component; adding violates the forbidden pair . Neither nor violates a restriction. Each query is evaluated independently on the initial graph.