Skip to content

Commit c0111cb

Browse files
authored
Updated TopSort.cpp
1 parent 2d2e7e2 commit c0111cb

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

Diff for: Graph theory/Concepts/TopSort.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,42 @@ using namespace std;
33

44
#define rep(i,a,b) for(int i = a ; i < b ; i++)
55

6-
void IO()
7-
{
8-
freopen( "in.txt" , "r" ,stdin);
9-
}
10-
11-
const int N = 109;
12-
6+
const int N = 100007;
137
std::vector<int> graph[N];
148
bool vis[N];
159

1610
stack<int> ans;
1711

1812
void dfs(int source){
1913
vis[source] = 1;
20-
14+
2115
for(auto child:graph[source]){
2216
if(!vis[child]) dfs(child);
2317
}
24-
2518
ans.push(source); // when all the childs of a node have been visited then we will push it into the stack
2619
}
2720

2821
void solve(int t){
29-
int n,m; cin >> n >> m;
22+
int node,edge; cin >> node >> edge;
3023

3124
rep(i,0,m){
3225
int u , v;
3326
cin >> u >> v;
3427
graph[u].push_back(v); // directed graph
3528
}
3629

37-
rep(i,1,n+1) if(!vis[i]) dfs(i);
30+
rep(i,1,node+1)
31+
if(!vis[i])
32+
dfs(i);
3833

3934
while(!ans.empty()){
4035
cout << ans.top() << ' '; ans.pop();
4136
}
37+
cout << '\n';
4238
}
4339

4440
int32_t main()
45-
{
46-
IO();
41+
{
4742
int t = 1;
4843
// cin >> t;
4944

0 commit comments

Comments
 (0)