-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirected_graph.cpp
More file actions
44 lines (40 loc) · 846 Bytes
/
directed_graph.cpp
File metadata and controls
44 lines (40 loc) · 846 Bytes
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
//
// Created by patha on 10-11-2021.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
int count = 0;
vector<int> inDegree(n, 0);
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
adj[x].push_back(y);
inDegree[y]++;
}
queue<int> que;
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
que.push(i);
}
}
while (!que.empty()) {
count++;
int cur = que.front();
que.pop();
cout << cur << " ";
for (auto it: adj[cur]) {
inDegree[it]--;
if (inDegree[it] == 0) {
que.push(it);
}
}
}
return 0;
}