Under construction

Still being built. Things may change

K-Divisible Graph

Time limit2sMemory limit1024 MB

Summary
Given a weighted undirected graph and K, decide whether every closed walk has total weight divisible by K.
Level

Medium6 of 10

Topics
Graph, Union-find, Math
Solved
No attempts yet

Problem

A weighted undirected graph is called a KK-divisible graph if, for every closed walk PP in the graph, the sum of the edge weights along PP is always a multiple of KK. If an edge is used several times, its weight is added to the sum once for each use.

Given a weighted undirected graph GG and a positive integer KK, determine whether GG is a KK-divisible graph.

Input

The first line contains the number of vertices NN, the number of edges MM, and the positive integer KK, separated by spaces. (1≤N≤300 0001 \le N \le 300\,000, 0≤M≤500 0000 \le M \le 500\,000, 1≤K≤1091 \le K \le 10^9)

Each of the next MM lines contains three integers viv_i, wiw_i, and xix_i, separated by spaces. This describes an undirected edge with weight xix_i between vertex viv_i and vertex wiw_i. (1≤vi,wi≤N1 \le v_i, w_i \le N, 0≤xi≤1090 \le x_i \le 10^9)

There can be several edges between the same pair of vertices, and an edge may connect a vertex to itself. The graph may not be connected.

Output

If the given graph is a KK-divisible graph, print Yes. Otherwise, print No.

Hint

A closed walk is a walk that starts and ends at the same vertex. It may visit the same vertices and edges several times.

Examples2

  1. Example 1

    Input
    3 3 6
    1 2 3
    2 3 6
    3 1 9
    
    Expected output
    Yes
    
  2. Example 2

    Input
    4 4 7
    1 2 3
    2 3 2
    3 4 3
    4 2 2
    
    Expected output
    No