`{"id":"356b2fc6-d5ab-4f4f-8a4c-c347066db123","name":"Remove Max Number Of Edges To Keep Graph Fully Traversable","description":"Alice and Bob have an undirected graph of n nodes and 3 types of edges:\r\n\r\nType 1: Can be traversed by Alice only.\r\nType 2: Can be traversed by Bob only.\r\nType 3: Can by traversed by both Alice and Bob.\r\n\r\nGiven an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.\r\nReturn the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.\r\n","inputFormat":"First line contains two integers n and m.\r\nEach of next n lines contain 3 numbers denoting type of edges and the node between which node is placed.","outputFormat":"Print the maximum number of edges you can remove.","constraints":"1&lt;= n &lt;= 1000\r\n1 &lt;= edges.length &lt;= n * (n-1) / 2\r\n1 &lt;= edges[i][0] &lt;= 3\r\n1 &lt;= edges[i][1] &lt; edges[i][2] &lt;= n\r\nAll tuples (typei, ui, vi) are distinct.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n // write your code here\n} \n\nint main() { \n {\n int n, m;\n cin >> n >> m;\n vector<vector<int>>edges(m, vector<int>(3));\n for (int i = 0; i < m; i++) {\n cin >> edges[i][0];\n cin >> edges[i][1];\n cin >> edges[i][2];\n }\n cout<<maxNumEdgesToRemove(n, edges);\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n String[] st = br.readLine().split(\" \");\r\n int n = Integer.parseInt(st[0]);\r\n int m = Integer.parseInt(st[1]);\r\n\r\n int[][] edges = new int[m][3];\r\n for (int i = 0; i < m; i++) {\r\n st = br.readLine().split(\" \");\r\n edges[i][0] = Integer.parseInt(st[0]);\r\n edges[i][1] = Integer.parseInt(st[1]);\r\n edges[i][2] = Integer.parseInt(st[2]);\r\n }\r\n Main obj = new Main();\r\n System.out.println(obj.maxNumEdgesToRemove(n, edges));\r\n }\r\n\r\n public int maxNumEdgesToRemove(int n, int[][] edges) {\r\n\r\n }\r\n\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 6\r\n3 1 2\r\n3 2 3\r\n1 1 3\r\n1 2 4\r\n1 1 2\r\n2 3 4\r\n","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"348c0286-081a-4e91-93f8-6f6684e4e603","name":"Remove Max Number Of Edges To Keep Graph Fully Traversable","slug":"remove-max-number-of-edges-to-keep-graph-fully-traversable","type":1}],"next":{"id":"6c5422fc-4e2a-4ecd-8928-e61aca3dcc78","name":"Remove Max Number of Edges to keep graph fully Traversable MCQ","type":0,"slug":"remove-max-number-of-edges-to-keep-graph-fully-traversable-mcq"},"prev":{"id":"5e76c20e-ab59-47fa-93e8-d38de3db3641","name":"Kosaraju Algorithm","type":3,"slug":"kosaraju-algorithm"}}}`

# Remove Max Number Of Edges To Keep Graph Fully Traversable

Alice and Bob have an undirected graph of n nodes and 3 types of edges: Type 1: Can be traversed by Alice only. Type 2: Can be traversed by Bob only. Type 3: Can by traversed by both Alice and Bob. Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes. Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.

`{"id":"356b2fc6-d5ab-4f4f-8a4c-c347066db123","name":"Remove Max Number Of Edges To Keep Graph Fully Traversable","description":"Alice and Bob have an undirected graph of n nodes and 3 types of edges:\r\n\r\nType 1: Can be traversed by Alice only.\r\nType 2: Can be traversed by Bob only.\r\nType 3: Can by traversed by both Alice and Bob.\r\n\r\nGiven an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.\r\nReturn the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.\r\n","inputFormat":"First line contains two integers n and m.\r\nEach of next n lines contain 3 numbers denoting type of edges and the node between which node is placed.","outputFormat":"Print the maximum number of edges you can remove.","constraints":"1&lt;= n &lt;= 1000\r\n1 &lt;= edges.length &lt;= n * (n-1) / 2\r\n1 &lt;= edges[i][0] &lt;= 3\r\n1 &lt;= edges[i][1] &lt; edges[i][2] &lt;= n\r\nAll tuples (typei, ui, vi) are distinct.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n // write your code here\n} \n\nint main() { \n {\n int n, m;\n cin >> n >> m;\n vector<vector<int>>edges(m, vector<int>(3));\n for (int i = 0; i < m; i++) {\n cin >> edges[i][0];\n cin >> edges[i][1];\n cin >> edges[i][2];\n }\n cout<<maxNumEdgesToRemove(n, edges);\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n String[] st = br.readLine().split(\" \");\r\n int n = Integer.parseInt(st[0]);\r\n int m = Integer.parseInt(st[1]);\r\n\r\n int[][] edges = new int[m][3];\r\n for (int i = 0; i < m; i++) {\r\n st = br.readLine().split(\" \");\r\n edges[i][0] = Integer.parseInt(st[0]);\r\n edges[i][1] = Integer.parseInt(st[1]);\r\n edges[i][2] = Integer.parseInt(st[2]);\r\n }\r\n Main obj = new Main();\r\n System.out.println(obj.maxNumEdgesToRemove(n, edges));\r\n }\r\n\r\n public int maxNumEdgesToRemove(int n, int[][] edges) {\r\n\r\n }\r\n\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 6\r\n3 1 2\r\n3 2 3\r\n1 1 3\r\n1 2 4\r\n1 1 2\r\n2 3 4\r\n","sampleOutput":"2\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"348c0286-081a-4e91-93f8-6f6684e4e603","name":"Remove Max Number Of Edges To Keep Graph Fully Traversable","slug":"remove-max-number-of-edges-to-keep-graph-fully-traversable","type":1}],"next":{"id":"6c5422fc-4e2a-4ecd-8928-e61aca3dcc78","name":"Remove Max Number of Edges to keep graph fully Traversable MCQ","type":0,"slug":"remove-max-number-of-edges-to-keep-graph-fully-traversable-mcq"},"prev":{"id":"5e76c20e-ab59-47fa-93e8-d38de3db3641","name":"Kosaraju Algorithm","type":3,"slug":"kosaraju-algorithm"}}}`

Editor

# Remove Max Number Of Edges To Keep Graph Fully Traversable

easy

Alice and Bob have an undirected graph of n nodes and 3 types of edges: Type 1: Can be traversed by Alice only. Type 2: Can be traversed by Bob only. Type 3: Can by traversed by both Alice and Bob. Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes. Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.

## Constraints

1<= n <= 1000 1 <= edges.length <= n * (n-1) / 2 1 <= edges[i][0] <= 3 1 <= edges[i][1] < edges[i][2] <= n All tuples (typei, ui, vi) are distinct.

## Format

### Input

First line contains two integers n and m. Each of next n lines contain 3 numbers denoting type of edges and the node between which node is placed.

### Output

Print the maximum number of edges you can remove.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 6 3 1 2 3 2 3 1 1 3 1 2 4 1 1 2 2 3 4 ```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2 ```

Discussions

Show Discussion

Related Resources