-
Notifications
You must be signed in to change notification settings - Fork 8
/
Topological Sort.cpp
69 lines (65 loc) · 1.4 KB
/
Topological Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <sstream>
#include <iostream>
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <utility>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int mod=1e9+7;
#define rep(i,a,b) for (int i = a; i < b; i++)
#define vec vector<ll>
// Topological sort
// linear ordering of veritces such that for a pair u->v
// u appear befor v in that order
// topo is valid only for directed and acycl;ic graphs
// Using DFs traversal
// Time complexity O(n+e) space O(n)+O(n)
stack<int>st;
void dfs(int node,vector<int>visit,vector<int>adj[]){
visit[node]=1;
for(auto it:adj[node]){
if(!visit[it]){
dfs(it,visit,adj);
}
}
st.push(node);// when function call return then then from that
// node we insert it in the stack
}
void Toposort(int v,vector<int>adj[]){
vector<int>visit(v+1);
for(int i=1;i<=v;i++){
if(!visit[i]){
dfs(i,visit,adj);
}
}
}
void solve(){
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("in1.txt","r",stdin);
freopen("out1.txt","w",stdout);
#endif
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}