Detect Cycle on Directed graph

#include<bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GREY 1
#define BLACK 2
vector<int>adjList[100],v;
int color[100];
int parent[100];
void PrintCycle(int node,int u){
    if(node == u){
        for(int i = v.size()-1;i>=0;i--){
            cout<<v[i]<<" ";
        }
        cout<<u;
        return;
    }
    v.push_back(node);
    PrintCycle(parent[node],u);
}
int dfs(int node){
    color[node] = GREY;
    for(int i = 0;i<adjList[node].size();i++){
        int u = adjList[node][i];
        if(color[u]==GREY){
            //cout<<
            cout<<"Found Cycle"<<endl;
            cout<<u<<" ";
            PrintCycle(node,u);
        }
        if(color[u]==WHITE){
            color[u]= GREY;
            parent[u] = node;
            dfs(u);
        }
    }
    color[node] = BLACK;
}
int main(){
    freopen("in.txt","r",stdin);
    int node,edge;
    cin>>node>>edge;
    for(int i = 0;i<edge;i++){
        int from,to;
        cin>>from>>to;
        adjList[from].push_back(to);
    }
    dfs(1);
//    for(int i = 1;i<=node;i++){
//        for(int j = 0;j<adjList[i].size();j++){
//            cout<<adjList[i][j]<<" ";
//        }
//        cout<<endl;
//    }
}

Breadth First Search (BFS)

#include<bits/stdc++.h>
using namespace std;
vector<int> graph[100];
int parent[100];
int dist[100];
bool visit[100];
int bfs(int sx,int target){
    queue<int> q;
    q.push(sx);
    parent[sx] = 0;
    dist[sx] = 0;
    visit[sx] = true;
    while(!q.empty()){
        int u = q.front();
        q.pop();
//        if(u==target)
//            do something
        for(int i = 0;i<graph[u].size();i++){
            int v = graph[u][i];
            if(!visit[v]){
                q.push(v);
                visit[v] = true;
                dist[v] = dist[u] + 1;
                parent[v] = u;
            }
         }
    }
}
int main(){
    int node,edge,src,target;
    cin>>node>>edge;
    for(int i = 0;i<edge;i++){
        int from,to;
        cin>>from>>to;
        graph[from].push_back(to);
        graph[to].push_back(from);
    }
    cout<<"Source and Target Node: ";
    cin>>src>>target;
    bfs(src,target);
//    for(int i = 0;i<node;i++){
//        cout<<parent[i]<<" ";
//    }
//    cout<<endl;
    cout<<"Shortest Distance from "<<src<<" to "<<target<<" is "<<dist[5];
}

[Reference] http://www.shafaetsplanet.com/planetcoding/?p=604

Find repeating numbers

Problem: Given an array of n elements which contains elements from 0 to n-1, find repeating numbers in O(n) Time.

/* Time Complexity : O(N) */
#include<bits/stdc++.h>
using namespace std;
int main(){
    int arr[100] = {7,2,3,1,3,6,6,7,1,2};
    for(int i = 0;i<10;i++){
        int x = abs(arr[i]);
        if(arr[x]>=0){
            arr[x] *= -1;
        }
        else{
            cout<<abs(arr[i])<<" ";
        }
    }
}