#C5136. Decorated Tree Formation
Decorated Tree Formation
Decorated Tree Formation
In this problem, you are given a weighted undirected graph representing a network of intersections connected by roads. Your task is to determine if it is possible to form a decorated tree from the graph by removing some roads. A decorated tree is defined as a spanning tree in which all the road (edge) weights are distinct.
Formally, let (G = (V, E)) be an undirected graph with (|V| = n) intersections and (|E| = m) roads. A spanning tree (T) of (G) is a tree that connects all the intersections. The tree is considered decorated if all the (n-1) edges in (T) have unique weights.
You are given multiple test cases. For each test case, determine whether there exists a spanning tree with all unique weights. Output "YES" if such a tree exists, or "NO" otherwise.
The approach to solve this problem typically involves computing a Minimum Spanning Tree (MST) using algorithms such as Kruskal's algorithm. After constructing an MST, check if all the weights in the MST are distinct. If not, then the decorated tree cannot be formed.
inputFormat
The first line of input contains an integer (T) denoting the number of test cases. Each test case starts with a line containing two integers (n) and (m), the number of intersections and roads respectively. The following (m) lines each contain three integers (u), (v), and (w) describing a road connecting intersections (u) and (v) with weight (w). Note that the intersections are numbered from 1 to (n).
outputFormat
For each test case, output a single line containing either "YES" if it is possible to form a decorated tree (i.e., a spanning tree with all edges having distinct weights), or "NO" otherwise.## sample
3
4 5
1 2 3
1 3 5
2 3 7
2 4 4
3 4 6
6 8
1 2 3
1 3 2
1 4 1
2 5 4
2 6 6
3 5 2
4 6 5
5 6 3
3 2
1 2 5
2 3 7
YES
NO
YES
</p>