Tap here to switch tabs
Problem
Submissions

Problem: Incremental Even-Weighted Cycle Queries

Try to solve the Incremental Even-Weighted Cycle Queries problem.

Statement

You are given an integer n, representing the number of vertices in an undirected graph numbered from 00 to n −1- 1. You are also given a 22D integer array edges, where edges[i] =[ui,vi,wi]= [u_i, v_i, w_i] denotes a prospective undirected edge between vertex uiu_i and vertex viv_i with weight wiw_i.

Initially, the graph contains no edges. You process the edges in edges one by one in the given order. An edge is added to the graph if and only if adding it does not form any cycle with an odd total weight.

Return the total number of edges successfully added to the graph.

Constraints:

  • 3≤3 \leq n ≤5imes104\leq 5 imes 10^4

  • 1≤1 \leq edges.length ≤5imes104\leq 5 imes 10^4

  • edges[i] =[ui,vi,wi]= [u_i, v_i, w_i]

  • 0≤ui<vi<0 \leq u_i < v_i < n

  • All elements in edges are distinct.

  • wi=0w_i = 0 or wi=1w_i = 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Incremental Even-Weighted Cycle Queries

Try to solve the Incremental Even-Weighted Cycle Queries problem.

Statement

You are given an integer n, representing the number of vertices in an undirected graph numbered from 00 to n −1- 1. You are also given a 22D integer array edges, where edges[i] =[ui,vi,wi]= [u_i, v_i, w_i] denotes a prospective undirected edge between vertex uiu_i and vertex viv_i with weight wiw_i.

Initially, the graph contains no edges. You process the edges in edges one by one in the given order. An edge is added to the graph if and only if adding it does not form any cycle with an odd total weight.

Return the total number of edges successfully added to the graph.

Constraints:

  • 3≤3 \leq n ≤5imes104\leq 5 imes 10^4

  • 1≤1 \leq edges.length ≤5imes104\leq 5 imes 10^4

  • edges[i] =[ui,vi,wi]= [u_i, v_i, w_i]

  • 0≤ui<vi<0 \leq u_i < v_i < n

  • All elements in edges are distinct.

  • wi=0w_i = 0 or wi=1w_i = 1