Domain: Algorithms & Data Structures

You are given `k` simple undirected graphs `G1..Gk` on the *same* labeled vertex set `V = {1,2,...,n}`.

We say a graph `Q` is a **blow-up pattern** (called a *fun graph*) of a graph `H` if there exists a partition of `V(H)` into `|V(Q)|` nonempty parts, one part per vertex of `Q`, such that:

1. Each part induces either a **clique** or an **independent set** in `H`.
2. For two distinct vertices `x,y` of `Q` with corresponding parts `Px, Py`:
   - if `xy` is an edge of `Q`, then **every** vertex of `Px` is adjacent in `H` to **every** vertex of `Py`;
   - if `xy` is not an edge of `Q`, then **no** vertex of `Px` is adjacent in `H` to **any** vertex of `Py`.

A fun graph `Q` of `H` is called **super-minimal** (called *superb*) if among all fun graphs of `H`, it has the **fewest vertices**.

---

### The question

For each test case, decide whether there exist graphs `H1..Hk` that all share a common (unknown) vertex set `W`, such that:

- For every `i`, the given graph `Gi` is a **superb fun graph** of `Hi`.

Additionally, the following cross-graph restriction must hold for each label `v ∈ V`:

- If in some pair `(Gi,Hi)` the vertex `v` corresponds to an **independent-set part of size at least 2**, then there is **no** other pair `(Gj,Hj)` in which the same label `v` corresponds to a **clique part of size at least 2**.

Print `"Yes"` if such graphs `H1..Hk` exist, otherwise print `"No"`.

---

## Input
- The first line: integer `t` — number of test cases.
For each test case:
- One line: integers `n, k` (`1 ≤ n ≤ 300`, `1 ≤ k ≤ 10`)
- Then for each `i=1..k`:
  - One line: integer `m` — number of edges in `Gi`
  - Next `m` lines: edges `u v` (`1 ≤ u < v ≤ n`)

Constraints:
- Sum of `m` over all graphs over all test cases ≤ `2 * 10^5`
- Sum of `n` over all test cases ≤ `300`

## Output
For each test case output `Yes` or `No` (case-insensitive).

Grading rubric:
criterion: Does the solution define a Type-A twin pair as a nonedge pair (a,b) with identical open neighborhoods?
weight: 10
criterion: Does the solution define a Type-B twin pair as an edge pair (a,b) with identical closed neighborhoods?
weight: 10
criterion: Does the solution add the clause (x_a ∨ x_b) for each Type-A pair (a,b)?
weight: 10
criterion: Does the solution add the clause (¬x_a ∨ ¬x_b) for each Type-B pair (a,b)?
weight: 10
criterion: Does the solution state that satisfiability is decided using SCC computation on the implication graph?
weight: 20
criterion: Does the solution state that unsatisfiable occurs when x_v shares an SCC with ¬x_v for some v?
weight: 20
criterion: Does the solution describe detecting Type-A pairs using equality of adjacency bitsets?
weight: 10
criterion: Does the solution describe detecting Type-B pairs using equality of closed-neighborhood bitsets?
weight: 10

Answer:
## Expert Solution (DSA — Graphs / 2-SAT)

### Key structural notion: twins inside `Gi`

Fix one graph `Gi` on vertex set `{1..n}`.

- **Type-A twin pair (open twins):** vertices `a ≠ b` such that  
  `ab ∉ E(Gi)` and `N(a) = N(b)` (open neighborhoods identical).

- **Type-B twin pair (closed twins):** vertices `a ≠ b` such that  
  `ab ∈ E(Gi)` and `N[a] = N[b]` (closed neighborhoods identical).

These are exactly the pairs that can be merged in a fun-graph representation unless internal structure blocks the merge.

### Why “size ≥ 2” matters for superbness

Assume `Gi` is a superb fun graph of some `Hi` via parts `{P1..Pn}` (part `Pv` corresponds to label `v`).

- If `a,b` are **Type-A** in `Gi` and both `Pa` and `Pb` are **independent sets**, then `Pa ∪ Pb` is also independent, and it has the same external adjacency pattern (because `a` and `b` have identical neighborhoods in `Gi`).  
  This produces a valid fun graph with fewer vertices than `Gi`, contradicting superbness.

  Therefore, for every Type-A pair `(a,b)` in `Gi`, at least one of `{Pa,Pb}` must be a **clique with size ≥ 2** (a singleton clique does not prevent `Pa ∪ Pb` from being independent).

- If `a,b` are **Type-B** in `Gi` and both `Pa` and `Pb` are **cliques**, then `Pa ∪ Pb` is a clique (edges inside parts are complete, and edges across parts are complete because `ab` is an edge of `Gi`).  
  This again yields a smaller fun graph, contradicting superbness.

  Therefore, for every Type-B pair `(a,b)` in `Gi`, at least one of `{Pa,Pb}` must be an **independent set with size ≥ 2**.

### Collapsing the multi-graph constraint into one boolean per label

The extra global rule says a label `v` cannot be realized as:
- an **independent set of size ≥ 2** in some `Hi`
- and a **clique of size ≥ 2** in another `Hj`.

Introduce one boolean variable per label:
- `x_v = 1` means label `v` is allowed to be the “expanded clique label” (clique of size ≥ 2 whenever expansion is needed).
- `x_v = 0` means label `v` is allowed to be the “expanded independent label” (independent set of size ≥ 2 whenever expansion is needed).

Labels that end up size `1` in every `Hi` impose no restriction; they can be assigned either value.

Now translate each twin constraint into a 2-CNF clause on `{x_v}`:

- For every **Type-A** pair `(a,b)` in every `Gi`:  
  at least one must be an expanded clique label  
  ⇒ clause **`(x_a ∨ x_b)`**

- For every **Type-B** pair `(a,b)` in every `Gi`:  
  at least one must be an expanded independent label  
  ⇒ clause **`(¬x_a ∨ ¬x_b)`**

All constraints across all `k` graphs become a single **2-SAT instance with `n` variables**.

### Decision procedure (2-SAT)

Build the implication graph on `2n` literals.

- Clause `(p ∨ q)` adds implications `¬p → q` and `¬q → p`.

So:
- From `(x_a ∨ x_b)` add `¬x_a → x_b` and `¬x_b → x_a`.
- From `(¬x_a ∨ ¬x_b)` add `x_a → ¬x_b` and `x_b → ¬x_a`.

Run SCC (Kosaraju or Tarjan). The formula is satisfiable iff for every `v`,
`x_v` and `¬x_v` are in different SCCs.

If satisfiable print `Yes`, else print `No`.

### How to compute Type-A / Type-B pairs efficiently

For each `Gi`, store adjacency as bitsets `adj[v]` of length `n`.

For a pair `(a,b)`:
- `ab ∉ E(Gi)` and `adj[a] == adj[b]`  ⇒ Type-A
- `ab ∈ E(Gi)` and `(adj[a] ∪ {a}) == (adj[b] ∪ {b})` ⇒ Type-B

This is `O(k * n^2 * (n/word_size))`, which is fine for `n ≤ 300`.

### Complexity

Per test case:
- Building bitsets: `O(k * (n^2/word + m))`
- Checking all pairs: `O(k * n^2 * (n/word))`
- 2-SAT SCC: `O(n + number_of_clauses)`

Fits easily under the constraints.

### Output
For each test case output `Yes` iff the constructed 2-SAT instance is satisfiable.