diff --git a/Graphs/DFS.cpp b/Graphs/DFS.cpp index 47494e9..466825e 100644 --- a/Graphs/DFS.cpp +++ b/Graphs/DFS.cpp @@ -1,22 +1,40 @@ -void Graph::DFSUtil(int v, bool visited[]) -{ - visited[v] = true; - cout << v << " "; +#include - list::iterator i; - for (i = adj[v].begin(); i != adj[v].end(); ++i) - if (!visited[*i]) - DFSUtil(*i, visited); -} +using namespace std; -void Graph::DFS(int v) -{ - bool *visited = new bool[V]; - for (int i = 0; i < V; i++) - visited[i] = false; - - DFSUtil(v, visited); -} +typedef pair iPair; +vector g[100005]; -//font source: geekforgeeks +void dfs(int node , int visited[]){ + visited[node] = 1; + cout<>nodes; + cout<<"Enter the number of edges"<>edges; + cout<<"Enter the edges"; + for(int i = 0;i>u>>v; + g[u].push_back(v); + g[v].push_back(u); + } + cout<<"DFS traversal of this graph is"<= '0' && c <= '9'); + + if (neg) + return -ret; + return ret; + } + + public long nextLong() throws IOException + { + long ret = 0; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + if (neg) + return -ret; + return ret; + } + + public double nextDouble() throws IOException + { + double ret = 0, div = 1; + byte c = read(); + while (c <= ' ') + c = read(); + boolean neg = (c == '-'); + if (neg) + c = read(); + + do { + ret = ret * 10 + c - '0'; + } + while ((c = read()) >= '0' && c <= '9'); + + if (c == '.') + { + while ((c = read()) >= '0' && c <= '9') + { + ret += (c - '0') / (div *= 10); + } + } + + if (neg) + return -ret; + return ret; + } + + private void fillBuffer() throws IOException + { + bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); + if (bytesRead == -1) + buffer[0] = -1; + } + + private byte read() throws IOException + { + if (bufferPointer == bytesRead) + fillBuffer(); + return buffer[bufferPointer++]; + } + + public void close() throws IOException + { + if (din == null) + return; + din.close(); + } + } + public static void main(String args[] ) throws Exception { + Reader fr=new Reader(); + int t=fr.nextInt(); + int i,j; + String s[]; + for(i=1;i<=t;i++) + { + int n=fr.nextInt(); + long a[]=new long[n]; + s=fr.readLine().split(" "); + for(j=0;j