#include <iostream>
#include <vector>
using namespace std;
class Graph {
int V; // Number of vertices
vector<vector<int>> adjList; // Adjacency list
// Helper function for DFS to detect cycles
bool dfs(int node, vector<bool>& visited, vector<bool>& recStack) {
// Mark the current node as visited and add it to the recursion stack
visited[node] = true;
recStack[node] = true;
// Iterate over all the neighbors of the current node
for (int neighbor : adjList[node]) {
// If the neighbor is not visited, recurse
if (!visited[neighbor]) {
if (dfs(neighbor, visited, recStack)) {
return true;
}
}
// If the neighbor is already in the recursion stack, a cycle is detected
else if (recStack[neighbor]) {
return true;
}
}
// Remove the current node from the recursion stack
recStack[node] = false;
return false;
}
public:
// Constructor
Graph(int V) {
this->V = V;
adjList.resize(V);
}
// Function to add an edge to the graph
void addEdge(int u, int v) {
adjList[u].push_back(v);
}
// Function to check if the graph contains a cycle
bool hasCycle() {
vector<bool> visited(V, false); // To keep track of visited nodes
vector<bool> recStack(V, false); // To keep track of nodes in the recursion stack
// Perform DFS for each node to detect cycles
for (int i = 0; i < V; i++) {
if (!visited[i]) {
if (dfs(i, visited, recStack)) {
return true;
}
}
}
return false;
}
};
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0); // This creates a cycle
g.addEdge(2, 3);
if (g.hasCycle()) {
cout << "Graph contains a cycle" << endl;
} else {
cout << "Graph does not contain a cycle" << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIEdyYXBoIHsKICAgIGludCBWOyAvLyBOdW1iZXIgb2YgdmVydGljZXMKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gYWRqTGlzdDsgLy8gQWRqYWNlbmN5IGxpc3QKICAgIAogICAgLy8gSGVscGVyIGZ1bmN0aW9uIGZvciBERlMgdG8gZGV0ZWN0IGN5Y2xlcwogICAgYm9vbCBkZnMoaW50IG5vZGUsIHZlY3Rvcjxib29sPiYgdmlzaXRlZCwgdmVjdG9yPGJvb2w+JiByZWNTdGFjaykgewogICAgICAgIC8vIE1hcmsgdGhlIGN1cnJlbnQgbm9kZSBhcyB2aXNpdGVkIGFuZCBhZGQgaXQgdG8gdGhlIHJlY3Vyc2lvbiBzdGFjawogICAgICAgIHZpc2l0ZWRbbm9kZV0gPSB0cnVlOwogICAgICAgIHJlY1N0YWNrW25vZGVdID0gdHJ1ZTsKICAgICAgICAKICAgICAgICAvLyBJdGVyYXRlIG92ZXIgYWxsIHRoZSBuZWlnaGJvcnMgb2YgdGhlIGN1cnJlbnQgbm9kZQogICAgICAgIGZvciAoaW50IG5laWdoYm9yIDogYWRqTGlzdFtub2RlXSkgewogICAgICAgICAgICAvLyBJZiB0aGUgbmVpZ2hib3IgaXMgbm90IHZpc2l0ZWQsIHJlY3Vyc2UKICAgICAgICAgICAgaWYgKCF2aXNpdGVkW25laWdoYm9yXSkgewogICAgICAgICAgICAgICAgaWYgKGRmcyhuZWlnaGJvciwgdmlzaXRlZCwgcmVjU3RhY2spKSB7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgLy8gSWYgdGhlIG5laWdoYm9yIGlzIGFscmVhZHkgaW4gdGhlIHJlY3Vyc2lvbiBzdGFjaywgYSBjeWNsZSBpcyBkZXRlY3RlZAogICAgICAgICAgICBlbHNlIGlmIChyZWNTdGFja1tuZWlnaGJvcl0pIHsKICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIC8vIFJlbW92ZSB0aGUgY3VycmVudCBub2RlIGZyb20gdGhlIHJlY3Vyc2lvbiBzdGFjawogICAgICAgIHJlY1N0YWNrW25vZGVdID0gZmFsc2U7CiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQogICAgCnB1YmxpYzoKICAgIC8vIENvbnN0cnVjdG9yCiAgICBHc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