-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopologicalSort.txt
More file actions
161 lines (140 loc) · 4.24 KB
/
TopologicalSort.txt
File metadata and controls
161 lines (140 loc) · 4.24 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <math.h>
#include <map>
#include <string>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cmath>
#include <set>
#include <queue>
#define vpii vector< pair<int,int> >
#define vi vector<int>
#define for0(i,a) for(int i=0;i<a;i++)
#define pii pair <int,int>
#define pll pair <long long,long long>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define db double
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define sqr(x) (x)*(x)
#define VI vector <int>
#define DBG pf("Hi\n")
#define MOD 1000000007
#define SZ(a) (int)a.size()
#define sf(a) scanf("%d",&a)
#define sfl(a) scanf("%lld",&a)
#define sff(a,b) scanf("%d %d",&a,&b)
#define sffl(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define loop(i,n) for(int i=0;i<n;i++)
#define loop1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=a;i>=b;i--)
#define intlim 2147483648
#define ull unsigned long long
// #define gcd(a, b) __gcd(a, b)
// #define lcm(a, b) ((a)*((b)/gcd(a,b)))
#define mk(x1,y1) make_pair(x1, y1)
#define maxl 10000
#define psz 20
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
/*----------------------Graph Moves----------------*/
const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
/*------------------------------------------------*/
// freopen("in.txt", "r", stdin);
// ios::sync_with_stdio(0);
// cin.tie(0);
/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
// ios::sync_with_stdio(0);
// cin.tie(0);
using namespace std;
vector<int>v;
int n, m;
vector<int>g[maxl + 2];
int inDegree[maxl + 2];
int lebel[maxl + 2];
int maxLabel = 0;
int position[maxl + 2];
bool TopoLogicalSort()
{
queue<int>q;
vector<int>afterSort;
for(int i = 0; i < int( v.size() ); i++)
{
int indegreeZero = v[i];
if(inDegree[indegreeZero] != 0){
continue;
}
q.push(indegreeZero);
afterSort.pb(indegreeZero);
lebel[indegreeZero] = 0;
}
int cnt=0;
while( !q.empty() )
{
int u = q.front();
cnt++;
q.pop();
// cout<<"U "<<u << endl;
for(int i = 0; i < int( g[u].size() ); i++)
{
int v = g[u][i];
if(--inDegree[v] == 0){
q.push(v);
// cout<<"C "<<v<<endl;
lebel[v] = lebel[u] + 1;
}
}
}
// cout<<"T "<<cnt <<" "<<n<<endl;
if( cnt != n) return false; // if traversing nodes is not equal to total nodes, means not possible.
return true;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
int serial[n + 2];
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
inDegree[v]++;
}
for(int i = 1; i <= n; i++){
if(inDegree[i] == 0){
v.pb(i);
}
}
int ans = true;
if(TopoLogicalSort()){
cout<< "YES" << endl;
}else{
cout << "NO" << endl;
}
return 0;
}