#C5275. Cycle with Even Weight Sum
Cycle with Even Weight Sum
Cycle with Even Weight Sum
You are given an undirected graph with n vertices and m edges. Each edge is described by three integers u, v, and w denoting an edge between vertices u and v with weight w.
Your task is to determine whether there exists a cycle in the graph such that the sum of the weights of the edges in the cycle is even. In other words, if a cycle is composed of edges with weights \(w_1, w_2, \ldots, w_k\), you need to check whether:
\(w_1 + w_2 + \cdots + w_k \equiv 0 \pmod{2}\)
It is guaranteed that self-loops and multiple edges will not appear in the input. If such a cycle exists, print YES
; otherwise, print NO
.
Note: The cycle must consist of at least three vertices and no edge or vertex (except the starting/ending vertex) is repeated.
inputFormat
The input is read from stdin and has the following format:
- The first line contains two integers n and m — the number of vertices and the number of edges, respectively.
- Each of the next m lines contains three integers u, v, and w, representing an edge between vertices u and v with weight w.
outputFormat
Output a single line to stdout containing either YES
if there exists a cycle with an even total weight, or NO
otherwise.
5 5
1 2 3
2 3 4
3 4 5
4 5 6
5 1 7
NO