Problem:
Score : 700 points

Problem Statement

Snuke's mother gave Snuke an undirected graph consisting of N vertices numbered 0 to N-1 and M edges.
This graph was connected and contained no parallel edges or self-loops.

One day, Snuke broke this graph.
Fortunately, he remembered Q clues about the graph.
The i-th clue (0 \leq i \leq Q-1) is represented as integers A_i,B_i,C_i and means the following:

If C_i=0: there was exactly one simple path (a path that never visits the same vertex twice) from Vertex A_i to B_i.

If C_i=1: there were two or more simple paths from Vertex A_i to B_i.

Snuke is not sure if his memory is correct, and worried whether there is a graph that matches these Q clues.
Determine if there exists a graph that matches Snuke's memory.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq N \times (N-1)/2

1 \leq Q \leq 10^5

0 \leq A_i,B_i \leq N-1

A_i \neq B_i

0 \leq C_i \leq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_0 B_0 C_0
A_1 B_1 C_1
\vdots
A_{Q-1} B_{Q-1} C_{Q-1}

Output

If there exists a graph that matches Snuke's memory, print Yes; otherwise, print No.

Sample Input 1

5 5 3
0 1 0
1 2 1
2 3 0

Sample Output 1

Yes

For example, consider a graph with edges (0,1),(1,2),(1,4),(2,3),(2,4). This graph matches the clues.

Sample Input 2

4 4 3
0 1 0
1 2 1
2 3 0

Sample Output 2

No

Sample Input 3

10 9 9
7 6 0
4 5 1
9 7 0
2 9 0
2 3 0
4 1 0
8 0 0
9 1 0
3 0 0

Sample Output 3

No

Solution in Python (3.4.3) (judge verdict: Runtime Error)

import sys
input = sys.stdin.readline

class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.child_num = 0
        self.is_root = True
        self.grand_parent = False

    def root(self):
        node = self
        while not node.is_root:
            # print(node.id)
            node = node.parent
        return node

def main():
    N, M, Q = map(int, input().split())
    abc = sorted([list(map(int, input().split())) for i in range(Q)], key=lambda x: x[2])

    nodes = [Node(i) for i in range(N)]
    edge_count = 0
    count_rest = True
    ok = True
    for i in range(Q):
        # print(i)
        a, b, c = abc[i]
        if c == 0:# Union-find
            root_a = nodes[a].root()
            root_b = nodes[b].root()
            if root_a != root_b:
                if root_a.child_num > root_b.child_num:
                    root_b, root_a = root_a, root_b
                root_a.child_num += root_b.child_num + 1
                root_b.parent = root_a
                root_b.is_root = False
                edge_count += 1
                if edge_count > M:
                    ok = False
                    break
        else:
            if count_rest:
                rest_node = 0
                for i in range(N):
                    if nodes[i].is_root and nodes[i].child_num == 0:
                        rest_node += 1
                count_rest = False
            if rest_node == 0:
                ok = False
                break
            root_a = nodes[a].root()
            root_b = nodes[b].root()
            if root_a == root_b:
                ok = False
                break
            elif root_a.grand_parent and root_b.grand_parent:
                pass
            elif root_a.grand_parent and not root_b.grand_parent:
                root_b.grand_parent = True
                edge_count += 1
            elif (not root_a.grand_parent) and root_b.grand_parent:
                root_a.grand_parent = True
                edge_count += 1
            else:
                root_a.grand_parent = True
                root_b.grand_parent = True
                edge_count += 2
    
    has_grand = 0
    for i in range(N):
        if nodes[i].grand_parent:
            has_grand += 1
    yoyu = (has_grand * (has_grand - 1)) // 2
    
    if edge_count > M:
        ok = False
    elif edge_count < M:
        if edge_count + rest_node - 1 <= M:
            if edge_count + rest_node - 1 + yoyu < M:
                ok = False
        else:
            ok = False

    if not ok:
        print("No")
    else:
        print("Yes")


if __name__ == "__main__":
    main()