function [output] = existscycle(A0) 
    A = abs(sign(A0+A0'));
    
    n = size(A,1);
    color = zeros(n,1);
    output=0; 
    hascycle = 0;
    for s=1:n 
        color = zeros(n,1);
        color(s) = 1;
        for i=1:n
            if (A(s,i)==1)
                if (color(i)==1)
                    hascycle=1;
                    break;
                else
                    [hascycle,color]=dfs(A,s,i,color);
                end
                if (hascycle==1) break; end
            end
            if (hascycle==1) break; end
        end
        if (hascycle==1) break; end
    end
    output= hascycle;
end

function [hascycle,color] = dfs(A0,parent,child,color) 
hascycle=0;
        color(child)=1; 
        n=size(A0,1);
        for i=1:n
            if (A0(child,i)==1 && i~=parent)
                if (color(i)==1)
                    hascycle=1;
                else
                    [hascycle,color]=dfs(A0,child,i,color);
                end
            end
        end
end