formatting source-code for 153fb7b8a5

This commit is contained in:
github-actions
2020-05-30 04:02:09 +00:00
parent 92fe9495ec
commit 8a2de9842b
175 changed files with 1671 additions and 3460 deletions

View File

@@ -1,7 +1,6 @@
#include <iostream>
using namespace std;
class graph
{
class graph {
int v;
list<int> *adj;
@@ -11,56 +10,46 @@ class graph
void printgraph();
void bfs(int s);
};
graph::graph(int v)
{
graph::graph(int v) {
this->v = v;
this->adj = new list<int>[v];
}
void graph::addedge(int src, int dest)
{
void graph::addedge(int src, int dest) {
src--;
dest--;
adj[src].push_back(dest);
// adj[dest].push_back(src);
}
void graph::printgraph()
{
for (int i = 0; i < this->v; i++)
{
void graph::printgraph() {
for (int i = 0; i < this->v; i++) {
cout << "Adjacency list of vertex " << i + 1 << " is \n";
list<int>::iterator it;
for (it = adj[i].begin(); it != adj[i].end(); ++it)
{
for (it = adj[i].begin(); it != adj[i].end(); ++it) {
cout << *it + 1 << " ";
}
cout << endl;
}
}
void graph::bfs(int s)
{
void graph::bfs(int s) {
bool *visited = new bool[this->v + 1];
memset(visited, false, sizeof(bool) * (this->v + 1));
visited[s] = true;
list<int> q;
q.push_back(s);
list<int>::iterator it;
while (!q.empty())
{
while (!q.empty()) {
int u = q.front();
cout << u << " ";
q.pop_front();
for (it = adj[u].begin(); it != adj[u].end(); ++it)
{
if (visited[*it] == false)
{
for (it = adj[u].begin(); it != adj[u].end(); ++it) {
if (visited[*it] == false) {
visited[*it] = true;
q.push_back(*it);
}
}
}
}
int main()
{
int main() {
graph g(4);
g.addedge(1, 2);
g.addedge(2, 3);

View File

@@ -10,28 +10,22 @@
using std::cout;
using std::min;
using std::vector;
class Solution
{
class Solution {
vector<vector<int>> graph;
vector<int> in_time, out_time;
int timer;
vector<vector<int>> bridge;
vector<bool> visited;
void dfs(int current_node, int parent)
{
void dfs(int current_node, int parent) {
visited.at(current_node) = true;
in_time[current_node] = out_time[current_node] = timer++;
for (auto& itr : graph[current_node])
{
if (itr == parent)
{
for (auto& itr : graph[current_node]) {
if (itr == parent) {
continue;
}
if (!visited[itr])
{
if (!visited[itr]) {
dfs(itr, current_node);
if (out_time[itr] > in_time[current_node])
{
if (out_time[itr] > in_time[current_node]) {
bridge.push_back({itr, current_node});
}
}
@@ -41,15 +35,13 @@ class Solution
public:
vector<vector<int>> search_bridges(int n,
const vector<vector<int>>& connections)
{
const vector<vector<int>>& connections) {
timer = 0;
graph.resize(n);
in_time.assign(n, 0);
visited.assign(n, false);
out_time.assign(n, 0);
for (auto& itr : connections)
{
for (auto& itr : connections) {
graph.at(itr[0]).push_back(itr[1]);
graph.at(itr[1]).push_back(itr[0]);
}
@@ -57,8 +49,7 @@ class Solution
return bridge;
}
};
int main(void)
{
int main(void) {
Solution s1;
int number_of_node = 5;
vector<vector<int>> node;
@@ -81,8 +72,7 @@ int main(void)
*/
vector<vector<int>> bridges = s1.search_bridges(number_of_node, node);
cout << bridges.size() << " bridges found!\n";
for (auto& itr : bridges)
{
for (auto& itr : bridges) {
cout << itr[0] << " --> " << itr[1] << '\n';
}
return 0;

View File

@@ -3,8 +3,7 @@
using std::vector;
class graph
{
class graph {
private:
vector<vector<int>> adj;
int connected_components;
@@ -14,48 +13,39 @@ class graph
public:
explicit graph(int n) : adj(n, vector<int>()) { connected_components = 0; }
void addEdge(int, int);
int getConnectedComponents()
{
int getConnectedComponents() {
depth_first_search();
return connected_components;
}
};
void graph::addEdge(int u, int v)
{
void graph::addEdge(int u, int v) {
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
void graph::depth_first_search()
{
void graph::depth_first_search() {
int n = adj.size();
vector<bool> visited(n, false);
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
for (int i = 0; i < n; i++) {
if (!visited[i]) {
explore(i, visited);
connected_components++;
}
}
}
void graph::explore(int u, vector<bool> &visited)
{
void graph::explore(int u, vector<bool> &visited) {
visited[u] = true;
for (auto v : adj[u])
{
if (!visited[v])
{
for (auto v : adj[u]) {
if (!visited[v]) {
explore(v, visited);
}
}
}
int main()
{
int main() {
graph g(4);
g.addEdge(1, 2);
g.addEdge(3, 2);

View File

@@ -5,28 +5,23 @@
int N; // denotes number of nodes;
std::vector<int> parent;
std::vector<int> siz;
void make_set()
{ // function the initialize every node as it's own parent
for (int i = 1; i <= N; i++)
{
void make_set() { // function the initialize every node as it's own parent
for (int i = 1; i <= N; i++) {
parent[i] = i;
siz[i] = 1;
}
}
// To find the component where following node belongs to
int find_set(int v)
{
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b)
{ // To join 2 components to belong to one
void union_sets(int a, int b) { // To join 2 components to belong to one
a = find_set(a);
b = find_set(b);
if (a != b)
{
if (a != b) {
if (siz[a] < siz[b])
std::swap(a, b);
parent[b] = a;
@@ -34,8 +29,7 @@ void union_sets(int a, int b)
}
}
int no_of_connected_components()
{ // To find total no of connected components
int no_of_connected_components() { // To find total no of connected components
std::set<int> temp; // temp set to count number of connected components
for (int i = 1; i <= N; i++) temp.insert(find_set(i));
return temp.size();
@@ -43,16 +37,14 @@ int no_of_connected_components()
// All critical/corner cases have been taken care of.
// Input your required values: (not hardcoded)
int main()
{
int main() {
std::cin >> N;
parent.resize(N + 1);
siz.resize(N + 1);
make_set();
int edges;
std::cin >> edges; // no of edges in the graph
while (edges--)
{
while (edges--) {
int node_a, node_b;
std::cin >> node_a >> node_b;
union_sets(node_a, node_b);

View File

@@ -1,28 +1,23 @@
#include <iostream>
using namespace std;
int v = 4;
void DFSUtil_(int graph[4][4], bool visited[], int s)
{
void DFSUtil_(int graph[4][4], bool visited[], int s) {
visited[s] = true;
cout << s << " ";
for (int i = 0; i < v; i++)
{
if (graph[s][i] == 1 && visited[i] == false)
{
for (int i = 0; i < v; i++) {
if (graph[s][i] == 1 && visited[i] == false) {
DFSUtil_(graph, visited, i);
}
}
}
void DFS_(int graph[4][4], int s)
{
void DFS_(int graph[4][4], int s) {
bool visited[v];
memset(visited, 0, sizeof(visited));
DFSUtil_(graph, visited, s);
}
int main()
{
int main() {
int graph[4][4] = {{0, 1, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}};
cout << "DFS: ";
DFS_(graph, 2);

View File

@@ -11,8 +11,7 @@ using namespace std;
int checked[999] = {WHITE};
void dfs(const list<int> lista[], int start)
{
void dfs(const list<int> lista[], int start) {
stack<int> stack;
int checked[999] = {WHITE};
@@ -20,33 +19,28 @@ void dfs(const list<int> lista[], int start)
stack.push(start);
checked[start] = GREY;
while (!stack.empty())
{
while (!stack.empty()) {
int act = stack.top();
stack.pop();
if (checked[act] == GREY)
{
if (checked[act] == GREY) {
cout << act << ' ';
for (auto it = lista[act].begin(); it != lista[act].end(); ++it)
{
for (auto it = lista[act].begin(); it != lista[act].end(); ++it) {
stack.push(*it);
if (checked[*it] != BLACK)
checked[*it] = GREY;
}
checked[act] = BLACK; //nodo controllato
checked[act] = BLACK; // nodo controllato
}
}
}
int main()
{
int main() {
int u, w;
int n;
cin >> n;
list<int> lista[INF];
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
cin >> u >> w;
lista[u].push_back(w);
}

View File

@@ -6,8 +6,7 @@ using namespace std;
#define INF 10000010
vector<pair<int, int>> graph[5 * 100001];
int dis[5 * 100001];
int dij(vector<pair<int, int>> *v, int s, int *dis)
{
int dij(vector<pair<int, int>> *v, int s, int *dis) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pq;
@@ -15,28 +14,23 @@ int dij(vector<pair<int, int>> *v, int s, int *dis)
pq.push(make_pair(0, s));
dis[s] = 0;
int u;
while (!pq.empty())
{
while (!pq.empty()) {
u = (pq.top()).second;
pq.pop();
for (vector<pair<int, int>>::iterator it = v[u].begin();
it != v[u].end(); it++)
{
if (dis[u] + it->first < dis[it->second])
{
it != v[u].end(); it++) {
if (dis[u] + it->first < dis[it->second]) {
dis[it->second] = dis[u] + it->first;
pq.push(make_pair(dis[it->second], it->second));
}
}
}
}
int main()
{
int main() {
int m, n, l, x, y, s;
// n--> number of nodes , m --> number of edges
cin >> n >> m;
for (int i = 0; i < m; i++)
{
for (int i = 0; i < m; i++) {
// input edges.
scanf("%d%d%d", &x, &y, &l);
graph[x].push_back(make_pair(l, y));

View File

@@ -13,10 +13,8 @@ using namespace std;
* @param V : vertices
* @return void
**/
void print(vector<int> a[], int V)
{
for (int i = 0; i < V; i++)
{
void print(vector<int> a[], int V) {
for (int i = 0; i < V; i++) {
if (!a[i].empty())
cout << "i=" << i << "-->";
for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << " ";
@@ -33,11 +31,9 @@ void print(vector<int> a[], int V)
* @param adj[] : array of vectors to represent graph
* @return void
**/
void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[])
{
void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[]) {
vis[v] = true;
for (auto i = adj[v].begin(); i != adj[v].end(); i++)
{
for (auto i = adj[v].begin(); i != adj[v].end(); i++) {
if (vis[*i] == false)
push_vertex(*i, st, vis, adj);
}
@@ -51,12 +47,10 @@ void push_vertex(int v, stack<int> &st, bool vis[], vector<int> adj[])
* @param grev[] : graph with reversed edges
* @return void
**/
void dfs(int v, bool vis[], vector<int> grev[])
{
void dfs(int v, bool vis[], vector<int> grev[]) {
vis[v] = true;
// cout<<v<<" ";
for (auto i = grev[v].begin(); i != grev[v].end(); i++)
{
for (auto i = grev[v].begin(); i != grev[v].end(); i++) {
if (vis[*i] == false)
dfs(*i, vis, grev);
}
@@ -72,21 +66,17 @@ no SCCs i.e. none(0) or there will be x no. of SCCs (x>0)) i.e. it returns the
count of (number of) strongly connected components (SCCs) in the graph.
(variable 'count_scc' within function)
**/
int kosaraju(int V, vector<int> adj[])
{
int kosaraju(int V, vector<int> adj[]) {
bool vis[V] = {};
stack<int> st;
for (int v = 0; v < V; v++)
{
for (int v = 0; v < V; v++) {
if (vis[v] == false)
push_vertex(v, st, vis, adj);
}
// making new graph (grev) with reverse edges as in adj[]:
vector<int> grev[V];
for (int i = 0; i < V + 1; i++)
{
for (auto j = adj[i].begin(); j != adj[i].end(); j++)
{
for (int i = 0; i < V + 1; i++) {
for (auto j = adj[i].begin(); j != adj[i].end(); j++) {
grev[*j].push_back(i);
}
}
@@ -95,12 +85,10 @@ int kosaraju(int V, vector<int> adj[])
// reinitialise visited to 0
for (int i = 0; i < V; i++) vis[i] = false;
int count_scc = 0;
while (!st.empty())
{
while (!st.empty()) {
int t = st.top();
st.pop();
if (vis[t] == false)
{
if (vis[t] == false) {
dfs(t, vis, grev);
count_scc++;
}
@@ -112,12 +100,10 @@ int kosaraju(int V, vector<int> adj[])
// All critical/corner cases have been taken care of.
// Input your required values: (not hardcoded)
int main()
{
int main() {
int t;
cin >> t;
while (t--)
{
while (t--) {
int a, b; // a->number of nodes, b->directed edges.
cin >> a >> b;
int m, n;

View File

@@ -24,47 +24,39 @@ typedef long long ll;
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
void in(int &x)
{
void in(int &x) {
register int c = gc();
x = 0;
int neg = 0;
for (; ((c < 48 || c > 57) && c != '-'); c = gc())
;
if (c == '-')
{
if (c == '-') {
neg = 1;
c = gc();
}
for (; c > 47 && c < 58; c = gc())
{
for (; c > 47 && c < 58; c = gc()) {
x = (x << 1) + (x << 3) + c - 48;
}
if (neg)
x = -x;
}
void out(int n)
{
void out(int n) {
int N = n, rev, count = 0;
rev = N;
if (N == 0)
{
if (N == 0) {
pc('0');
return;
}
while ((rev % 10) == 0)
{
while ((rev % 10) == 0) {
count++;
rev /= 10;
}
rev = 0;
while (N != 0)
{
while (N != 0) {
rev = (rev << 3) + (rev << 1) + N % 10;
N /= 10;
}
while (rev != 0)
{
while (rev != 0) {
pc(rev % 10 + '0');
rev /= 10;
}
@@ -72,53 +64,43 @@ void out(int n)
}
ll parent[mx], arr[mx], node, edge;
vector<pair<ll, pair<ll, ll>>> v;
void initial()
{
void initial() {
int i;
rep(i, node + edge) parent[i] = i;
}
int root(int i)
{
while (parent[i] != i)
{
int root(int i) {
while (parent[i] != i) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void join(int x, int y)
{
void join(int x, int y) {
int root_x = root(x); // Disjoint set union by rank
int root_y = root(y);
parent[root_x] = root_y;
}
ll kruskal()
{
ll kruskal() {
ll mincost = 0, i, x, y;
rep(i, edge)
{
rep(i, edge) {
x = v[i].second.first;
y = v[i].second.second;
if (root(x) != root(y))
{
if (root(x) != root(y)) {
mincost += v[i].first;
join(x, y);
}
}
return mincost;
}
int main()
{
int main() {
fast;
while (1)
{
while (1) {
int i, j, from, to, cost, totalcost = 0;
cin >> node >> edge; // Enter the nodes and edges
if (node == 0 && edge == 0)
break; // Enter 0 0 to break out
initial(); // Initialise the parent array
rep(i, edge)
{
rep(i, edge) {
cin >> from >> to >> cost;
v.pb(mp(cost, mp(from, to)));
totalcost += cost;

View File

@@ -7,19 +7,16 @@ using namespace std;
// Resource : https://cp-algorithms.com/graph/lca_binary_lifting.html
const int N = 1005;
const int LG = log2(N) + 1;
struct lca
{
struct lca {
int n;
vector<int> adj[N]; // Graph
int up[LG][N]; // build this table
int level[N]; // get the levels of all of them
lca(int n_) : n(n_)
{
lca(int n_) : n(n_) {
memset(up, -1, sizeof(up));
memset(level, 0, sizeof(level));
for (int i = 0; i < n - 1; ++i)
{
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
a--;
@@ -31,77 +28,59 @@ struct lca
dfs(0, -1);
build();
}
void verify()
{
for (int i = 0; i < n; ++i)
{
void verify() {
for (int i = 0; i < n; ++i) {
cout << i << " : level: " << level[i] << endl;
}
cout << endl;
for (int i = 0; i < LG; ++i)
{
for (int i = 0; i < LG; ++i) {
cout << "Power:" << i << ": ";
for (int j = 0; j < n; ++j)
{
for (int j = 0; j < n; ++j) {
cout << up[i][j] << " ";
}
cout << endl;
}
}
void build()
{
for (int i = 1; i < LG; ++i)
{
for (int j = 0; j < n; ++j)
{
if (up[i - 1][j] != -1)
{
void build() {
for (int i = 1; i < LG; ++i) {
for (int j = 0; j < n; ++j) {
if (up[i - 1][j] != -1) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
}
}
void dfs(int node, int par)
{
void dfs(int node, int par) {
up[0][node] = par;
for (auto i : adj[node])
{
if (i != par)
{
for (auto i : adj[node]) {
if (i != par) {
level[i] = level[node] + 1;
dfs(i, node);
}
}
}
int query(int u, int v)
{
int query(int u, int v) {
u--;
v--;
if (level[v] > level[u])
{
if (level[v] > level[u]) {
swap(u, v);
}
// u is at the bottom.
int dist = level[u] - level[v];
// Go up this much distance
for (int i = LG - 1; i >= 0; --i)
{
if (dist & (1 << i))
{
for (int i = LG - 1; i >= 0; --i) {
if (dist & (1 << i)) {
u = up[i][u];
}
}
if (u == v)
{
if (u == v) {
return u;
}
assert(level[u] == level[v]);
for (int i = LG - 1; i >= 0; --i)
{
if (up[i][u] != up[i][v])
{
for (int i = LG - 1; i >= 0; --i) {
if (up[i][u] != up[i][v]) {
u = up[i][u];
v = up[i][v];
}
@@ -111,8 +90,7 @@ struct lca
}
};
int main()
{
int main() {
int n; // number of nodes in the tree.
lca l(n); // will take the input in the format given
// n-1 edges of the form

View File

@@ -15,8 +15,7 @@
#include <vector>
// std::max capacity of node in graph
const int MAXN = 505;
class Graph
{
class Graph {
int residual_capacity[MAXN][MAXN];
int capacity[MAXN][MAXN]; // used while checking the flow of edge
int total_nodes;
@@ -25,26 +24,21 @@ class Graph
std::vector<std::tuple<int, int, int> > edge_participated;
std::bitset<MAXN> visited;
int max_flow = 0;
bool bfs(int source, int sink)
{ // to find the augmented - path
bool bfs(int source, int sink) { // to find the augmented - path
memset(parent, -1, sizeof(parent));
visited.reset();
std::queue<int> q;
q.push(source);
bool is_path_found = false;
while (q.empty() == false && is_path_found == false)
{
while (q.empty() == false && is_path_found == false) {
int current_node = q.front();
visited.set(current_node);
q.pop();
for (int i = 0; i < total_nodes; ++i)
{
if (residual_capacity[current_node][i] > 0 && !visited[i])
{
for (int i = 0; i < total_nodes; ++i) {
if (residual_capacity[current_node][i] > 0 && !visited[i]) {
visited.set(i);
parent[i] = current_node;
if (i == sink)
{
if (i == sink) {
return true;
}
q.push(i);
@@ -56,32 +50,26 @@ class Graph
public:
Graph() { memset(residual_capacity, 0, sizeof(residual_capacity)); }
void set_graph(void)
{
void set_graph(void) {
std::cin >> total_nodes >> total_edges >> source >> sink;
for (int start, destination, capacity_, i = 0; i < total_edges; ++i)
{
for (int start, destination, capacity_, i = 0; i < total_edges; ++i) {
std::cin >> start >> destination >> capacity_;
residual_capacity[start][destination] = capacity_;
capacity[start][destination] = capacity_;
}
}
void ford_fulkerson(void)
{
while (bfs(source, sink))
{
void ford_fulkerson(void) {
while (bfs(source, sink)) {
int current_node = sink;
int flow = std::numeric_limits<int>::max();
while (current_node != source)
{
while (current_node != source) {
int parent_ = parent[current_node];
flow = std::min(flow, residual_capacity[parent_][current_node]);
current_node = parent_;
}
current_node = sink;
max_flow += flow;
while (current_node != source)
{
while (current_node != source) {
int parent_ = parent[current_node];
residual_capacity[parent_][current_node] -= flow;
residual_capacity[current_node][parent_] += flow;
@@ -89,14 +77,11 @@ class Graph
}
}
}
void print_flow_info(void)
{
for (int i = 0; i < total_nodes; ++i)
{
for (int j = 0; j < total_nodes; ++j)
{
if (capacity[i][j] && residual_capacity[i][j] < capacity[i][j])
{
void print_flow_info(void) {
for (int i = 0; i < total_nodes; ++i) {
for (int j = 0; j < total_nodes; ++j) {
if (capacity[i][j] &&
residual_capacity[i][j] < capacity[i][j]) {
edge_participated.push_back(std::make_tuple(
i, j, capacity[i][j] - residual_capacity[i][j]));
}
@@ -106,8 +91,7 @@ class Graph
<< "\nEdge present in flow: " << edge_participated.size()
<< '\n';
std::cout << "\nSource\tDestination\tCapacity\total_nodes";
for (auto& edge_data : edge_participated)
{
for (auto& edge_data : edge_participated) {
int source, destination, capacity_;
std::tie(source, destination, capacity_) = edge_data;
std::cout << source << "\t" << destination << "\t\t" << capacity_
@@ -115,8 +99,7 @@ class Graph
}
}
};
int main(void)
{
int main(void) {
/*
Input Graph: (for testing )
4 5 0 3

View File

@@ -9,8 +9,7 @@ typedef std::pair<int, int> PII;
bool marked[MAX];
std::vector<PII> adj[MAX];
int prim(int x)
{
int prim(int x) {
// priority queue to maintain edges with respect to weights
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
int y;
@@ -18,8 +17,7 @@ int prim(int x)
PII p;
Q.push(std::make_pair(0, x));
while (!Q.empty())
{
while (!Q.empty()) {
// Select the edge with minimum weight
p = Q.top();
Q.pop();
@@ -29,8 +27,7 @@ int prim(int x)
continue;
minimumCost += p.first;
marked[x] = true;
for (int i = 0; i < adj[x].size(); ++i)
{
for (int i = 0; i < adj[x].size(); ++i) {
y = adj[x][i].second;
if (marked[y] == false)
Q.push(adj[x][i]);
@@ -39,8 +36,7 @@ int prim(int x)
return minimumCost;
}
int main()
{
int main() {
int nodes, edges, x, y;
int weight, minimumCost;
@@ -49,8 +45,7 @@ int main()
return 0;
// Edges with their nodes & weight
for (int i = 0; i < edges; ++i)
{
for (int i = 0; i < edges; ++i) {
std::cin >> x >> y >> weight;
adj[x].push_back(std::make_pair(weight, y));
adj[y].push_back(std::make_pair(weight, x));

View File

@@ -8,44 +8,37 @@ vector<vector<int>> G;
vector<bool> visited;
vector<int> ans;
void dfs(int v)
{
void dfs(int v) {
visited[v] = true;
for (int u : G[v])
{
for (int u : G[v]) {
if (!visited[u])
dfs(u);
}
ans.push_back(v);
}
void topological_sort()
{
void topological_sort() {
visited.assign(n, false);
ans.clear();
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
if (!visited[i])
dfs(i);
}
reverse(ans.begin(), ans.end());
}
int main()
{
int main() {
cout << "Enter the number of vertices and the number of directed edges\n";
cin >> n >> m;
int x, y;
G.resize(n, vector<int>());
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
cin >> x >> y;
x--, y--; // to convert 1-indexed to 0-indexed
G[x].push_back(y);
}
topological_sort();
cout << "Topological Order : \n";
for (int v : ans)
{
for (int v : ans) {
cout << v + 1
<< ' '; // converting zero based indexing back to one based.
}

View File

@@ -6,8 +6,7 @@
int *topoSortKahn(int N, std::vector<int> adj[]);
int main()
{
int main() {
int nodes, edges;
std::cin >> edges >> nodes;
if (edges == 0 || nodes == 0)
@@ -20,36 +19,29 @@ int main()
// 6 6
// 5 0 5 2 2 3 4 0 4 1 1 3
for (int i = 0; i < edges; i++)
{
for (int i = 0; i < edges; i++) {
std::cin >> u >> v;
graph[u].push_back(v);
}
int *topo = topoSortKahn(nodes, graph);
// topologically sorted nodes
for (int i = 0; i < nodes; i++)
{
for (int i = 0; i < nodes; i++) {
std::cout << topo[i] << " ";
}
}
int *topoSortKahn(int V, std::vector<int> adj[])
{
int *topoSortKahn(int V, std::vector<int> adj[]) {
std::vector<bool> vis(V + 1, false);
std::vector<int> deg(V + 1, 0);
for (int i = 0; i < V; i++)
{
for (int j = 0; j < adj[i].size(); j++)
{
for (int i = 0; i < V; i++) {
for (int j = 0; j < adj[i].size(); j++) {
deg[adj[i][j]]++;
}
}
std::queue<int> q;
for (int i = 0; i < V; i++)
{
if (deg[i] == 0)
{
for (int i = 0; i < V; i++) {
if (deg[i] == 0) {
q.push(i);
vis[i] = true;
}
@@ -57,19 +49,15 @@ int *topoSortKahn(int V, std::vector<int> adj[])
int *arr = new int[V + 1];
memset(arr, 0, V + 1);
int count = 0;
while (!q.empty())
{
while (!q.empty()) {
int cur = q.front();
q.pop();
arr[count] = cur;
count++;
for (int i = 0; i < adj[cur].size(); i++)
{
if (!vis[adj[cur][i]])
{
for (int i = 0; i < adj[cur].size(); i++) {
if (!vis[adj[cur][i]]) {
deg[adj[cur][i]]--;
if (deg[adj[cur][i]] == 0)
{
if (deg[adj[cur][i]] == 0) {
q.push(adj[cur][i]);
vis[adj[cur][i]] = true;
}