Red-Blue Spanning Tree
Time limit3sMemory limit256 MB
Given a connected graph with red and blue edges, decide whether some spanning tree has exactly k blue edges.
- Level
Medium7 of 10
- Topics
- Union-find, Graph, Greedy, Minimum spanning tree
- Solved
- No attempts yet
Problem
You are given an undirected, unweighted, connected graph. Each edge is colored either red (R) or blue (B). Write a program that determines whether the graph has a spanning tree containing exactly blue edges.
Input
The input consists of several test cases.
The first line of each test case contains three integers , , and . Here is the number of vertices (), is the number of edges, and is the required number of blue edges in the spanning tree ().
Each of the next lines describes one edge with three values , , and . The color is R for a red edge or B for a blue edge. and are the two vertices joined by the edge (, ). At most one edge connects any pair of vertices.
The last line of the input is 0 0 0, which must not be processed.
Output
For each test case, print 1 on its own line if a spanning tree with exactly blue edges can be formed, or 0 otherwise.