You are given an infinite graph. Determine whether it has a perfect matching$$$^{\text{∗}}$$$.
The infinite graph is given in the following format:
We consider a family of graphs $$$G_1, \cdots, G_k$$$. Each edge $$$(u,v)$$$ has a label l, $$$0 \le l \le k$$$.
We start with a copy of the graph $$$G_1$$$. Every edge $$$(u,v)$$$ with label $$$l \ne 0$$$ is replaced by a copy of the graph $$$G_l$$$, with $$$G_l$$$'s vertex $$$1$$$ merged with $$$u$$$ and $$$G_l$$$'s vertex $$$2$$$ merged with $$$v$$$. This replacing is repeated to generate a potentially infinite graph.
Formally, let $$$H_0 = G_1$$$. $$$H_{i+1}$$$ is produced from $$$H_i$$$ by the replacement procedure described above. $$$H_i'$$$ is produced from $$$H_i$$$ by removing all edges with with labels $$$l \ne 0$$$. The resulting graph H is the infinite union of all $$$H_i'$$$.
All $$$G_i$$$ contain no self-loops or multiedges.
$$$^{\text{∗}}$$$A perfect matching is a subset of the edges such that every vertex is incident to exactly one of them
The first line contains a single integer $$$k$$$ ($$$1 \le k \le 10$$$), the number of graphs. The descriptions of the graphs follows.
The first line of each graph contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 100$$$, $$$0 \le m \le 100$$$), the number of vertices and edges.
Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$l$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$, $$$0 \le l \le k$$$), indicating an edge $$$(u,v)$$$ with label $$$l$$$.
Output "YES" if the infinite graph has a perfect matching, "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
16 61 3 01 4 03 5 14 6 12 5 02 6 0
YES
12 11 2 1
NO
13 21 3 12 3 0
NO
13 23 1 12 3 0
YES
23 31 2 01 3 02 3 23 23 1 22 3 0
YES
The first sample describes a pair of infinite binary trees. At first, it looks like this:

Then, each dashed edge is replaced with another copy of graph 1:

Continuing like this, each side becomes an infinite binary tree. Note that the sides form two different connected components, since no non-dashed edge ever connects them.
The second sample is a finite graph with two vertices and no edges. Each replacement of the edge with another copy of the graph does not change the graph.
The third sample is a path which is infinite in one direction, and an isolated vertex. The path grows by one vertex with each replacement step, the vertex stays isolated.
The fourth sample is a pair of paths which are infinite in one direction. At each step, one of the paths grows by one vertex, while the other one stays the same length; the paths take turns growing. It matters in which direction a replacement edge is given, since this is the only difference from the third sample.
The fifth sample is a path which is infinite in both directions. It is the forth sample with the two paths glued together with another two edges.