P15366 [GeeksforGeeks] Detect cycle using dsu
普及
题目描述

Given an undirected graph with V nodes and E edges. The task is to check if there is any cycle in undirected graph.
Note: Solve the problem using disjoint set union(dsu).

Example 1:
Input:
Output: 1
Explanation: There is a cycle between 0->2->4->0
Example 2:
Input:
Output: 0
Explanation: The graph doesn't contain any cycle

Your Task:
You don't need to read or print anyhting. Your task is to complete the function detectCycle() which takes number of vertices in the graph denoting as V and adjacency list denoting as adj and returns 1 if graph contains any cycle otherwise returns 0.
Expected Time Complexity: O(V + E)
Expected Space Complexity: O(V)

Constraints:
1 ≤ V, E ≤ 10^{4}

输入格式

见题面

输出格式

见题面

样例输出
1