-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.cpp
81 lines (63 loc) · 1.57 KB
/
c.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
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define db(x) cout << '>' << #x << ':' << x << endl;
#define sz(x) ((int)(x).size())
#define newl cout << "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<long long>
#define vvll vector<vll>
#define pll pair<long long, long long>
#define fast_io() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const int mxN = 1e5 + 5;
const int inf = 1e9;
ll tc, n, m, k;
vll g[2][mxN];
ll coins[mxN], vis[mxN];
vll comp[mxN];
ll scc = 0;
vll topo;
void dfs(ll u, ll t) {
vis[u] = 1;
if(t == 1) comp[scc].pb(u);
for(auto v:g[t][u]) {
if(!vis[v]) dfs(v, t);
}
if(t==0) topo.pb(u);
}
int main() {
fast_io();
#ifndef ONLINE_JUDGE
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#endif
cin >> n >> m;
rep(u, 1, n+1) cin>>coins[u];
rep(i, 0, m) {
ll u, v;
cin>>u>>v;
g[0][u].pb(v);
g[1][v].pb(u);
}
rep(u, 1, n+1) {
if(!vis[u]) {
scc++;
dfs(u, 0);
}
}
rep(u, 1, n+1) vis[u] = 0;
rep(i, n, 0) {
auto u = topo[i];
if(!vis[u]) dfs(u, 1);
}
return 0;
}