-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBFSdirectedcycle.cpp
57 lines (47 loc) · 904 Bytes
/
BFSdirectedcycle.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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void BFS(int v, map<int,set<int>> adj){
vector<int> indegree(v);
for(auto i: adj){
for(auto j:i.second){
indegree[j]++;
}
}
queue<int> q;
for(int i=1;i<v;i++){
if(indegree[i]==0){
q.push(i);
}
}
int count = 0;
while(!q.empty()){
int frontnode = q.front();
q.pop();
count++;
for(int it:adj[frontnode]){
indegree[it]--;
if(indegree[it]==0){
q.push(it);
}
}
}
if(count==v){
cout<<"FALSE";
}
else{
cout<<"TRUE";
}
}
int main(){
int n,m;
cin>>n>>m;
map<int, set<int>> adj;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].insert(v);
}
BFS(n, adj);
return 0;
}