This page is still under construction.

Parts of this page are still being built. What you see may change.

Red-Blue Spanning Tree

Time limit3sMemory limit256 MB

Summary
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 kk blue edges.

Input

The input consists of several test cases.

The first line of each test case contains three integers nn, mm, and kk. Here nn is the number of vertices (2≤n≤1,0002 \le n \le 1{,}000), mm is the number of edges, and kk is the required number of blue edges in the spanning tree (0≤k<n0 \le k < n).

Each of the next mm lines describes one edge with three values cc, ff, and tt. The color cc is R for a red edge or B for a blue edge. ff and tt are the two vertices joined by the edge (1≤f,t≤n1 \le f, t \le n, f≠tf \ne t). 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 kk blue edges can be formed, or 0 otherwise.

Examples1

  1. Example 1

    Input
    3 3 2
    B 1 2
    B 2 3
    R 3 1
    2 1 1
    R 1 2
    0 0 0
    
    Expected output
    1
    0