Tap here to switch tabs
Problem
Submissions

Problem: Remove Maximum Edges to Keep Graph Fully Traversable

Try to solve the Remove Maximum Edges to Keep Graph Fully Traversable problem.

Statement

Alice and Bob have an undirected graph containing n nodes labeled from 11 to n. You are given an integer n and a 2extD2 ext{D} array edges, where edges[i] =[typei,ui,vi]= [type_i, u_i, v_i] represents a bidirectional edge between nodes uiu_i and viv_i.

The parameter typeitype_i defines the accessibility of the edge:

  • Type 11: Traversable only by Alice.

  • Type 22: Traversable only by Bob.

  • Type 33: Traversable by both Alice and Bob.

A graph is fully traversable by an individual if that person can reach every node from every other node using only their accessible edges.

Return the maximum number of edges you can remove such that the graph remains fully traversable by both Alice and Bob. If it is impossible for both Alice and Bob to fully traverse the graph, return −1-1.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq edges.length ≤min⁡(105,3⋅\leq \min(10^5, 3 \cdot n ⋅(\cdot ( n −1)/2)- 1) / 2)

  • edges[i].length =3= 3

  • 1≤typei≤31 \leq type_i \leq 3

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

  • All tuples (typei,ui,vi)(type_i, u_i, v_i) are distinct.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Remove Maximum Edges to Keep Graph Fully Traversable

Try to solve the Remove Maximum Edges to Keep Graph Fully Traversable problem.

Statement

Alice and Bob have an undirected graph containing n nodes labeled from 11 to n. You are given an integer n and a 2extD2 ext{D} array edges, where edges[i] =[typei,ui,vi]= [type_i, u_i, v_i] represents a bidirectional edge between nodes uiu_i and viv_i.

The parameter typeitype_i defines the accessibility of the edge:

  • Type 11: Traversable only by Alice.

  • Type 22: Traversable only by Bob.

  • Type 33: Traversable by both Alice and Bob.

A graph is fully traversable by an individual if that person can reach every node from every other node using only their accessible edges.

Return the maximum number of edges you can remove such that the graph remains fully traversable by both Alice and Bob. If it is impossible for both Alice and Bob to fully traverse the graph, return −1-1.

Constraints:

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq edges.length ≤min⁡(105,3⋅\leq \min(10^5, 3 \cdot n ⋅(\cdot ( n −1)/2)- 1) / 2)

  • edges[i].length =3= 3

  • 1≤typei≤31 \leq type_i \leq 3

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

  • All tuples (typei,ui,vi)(type_i, u_i, v_i) are distinct.