Posts

Showing posts from March, 2021

Dynamic Creation of Graphs for competitive programming

  In competitive programming, usually STL is used for faster programming and there the problem when we need to create dynamic array if we represent the graph by adjacency list. in most of Visual studio, the only option is using new operator which is not very useful in competitive programming. here is alternative way to make a graph using vector of  vector instead of array in case we need to create a graph of  (n+1) nodes when we have taken "n" as input. // vector of vector for adjacency list. vector<vector<long long int>> adjlist; adjlist.resize(n+1);   // it will help to create a graph of (n+1) nodes. // input the edges for (long long int i = 0; i < m; i++) { long long int u; long long int v; cin >> u >> v; adjlist[u].push_back(v); adjlist[v].push_back(u); } //Visited array for DFS vector<long long int> visited(n + 1); for (long long int i = 0; i <= n; i++) visited[i] = -1; //Standard DFS Code. visited[0] ...