Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Print all cycle if there multiple cycle exist
- #include<bits/stdc++.h>
- using namespace std;
- vector<int>adj[111], cycle;
- int vis[111], pathvis[111];
- int dfs(int node){
- vis[node] = 1;
- pathvis[node] = 1;
- cycle.push_back(node);
- for(auto it: adj[node]){
- if(pathvis[it] == 1){
- int startIndex = 0;
- while(cycle[startIndex] != it){
- startIndex++;
- }
- for(int i = startIndex; i < cycle.size(); i++){
- cout << cycle[i] << " ";
- }
- cout << endl;
- }
- else if(vis[it] == 0){
- if(dfs(it) == true){
- return true;
- }
- }
- }
- pathvis[node] = 0;
- cycle.pop_back();
- return false;
- }
- int main(){
- int v, e;
- cin >> v >> e;
- for(int i = 0; i < e; i++){
- int x, y;
- cin >> x >> y;
- adj[x].push_back(y);
- }
- memset(vis, 0, sizeof(vis));
- memset(pathvis, 0, sizeof(pathvis));
- for(int i = 0; i < v; i++){
- if(vis[i] == 0){
- dfs(i);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement