-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3760.cpp
82 lines (71 loc) · 1.92 KB
/
3760.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
82
/**
* @file 3760.cpp
* @author Macesuted ([email protected])
* @date 2022-12-08
*
* @copyright Copyright (c) 2022
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
#define maxn 2005
typedef pair<int, int> pii;
int a[maxn], indeg[maxn];
vector<vector<int>> graph;
priority_queue<pii, vector<pii>, less<pii>> que;
void solve(void) {
int n, m;
cin >> n >> m, graph.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1, x, y; i <= m; i++) cin >> x >> y, graph[y].push_back(x), indeg[x]++;
for (int i = 1; i <= n; i++)
if (!indeg[i]) que.emplace(a[i], i);
vector<int> ans;
while (!que.empty()) {
int p = que.top().second;
que.pop(), ans.push_back(p);
for (auto i : graph[p])
if (!--indeg[i]) que.emplace(a[i], i);
}
reverse(ans.begin(), ans.end());
for (auto i : ans) cout << i << ' ';
cout << endl;
for (int s = 1; s <= n; s++) {
for (int i = 1; i <= n; i++) indeg[i] = 0;
for (int i = 1; i <= n; i++)
for (auto j : graph[i]) indeg[j]++;
while (!que.empty()) que.pop();
for (int i = 1; i <= n; i++)
if (!indeg[i]) que.emplace(a[i], i);
int ans = n;
while (!que.empty()) {
int p = que.top().second;
que.pop();
if (p == s) continue;
if (a[p] < ans) break;
ans--;
for (auto i : graph[p])
if (!--indeg[i]) que.emplace(a[i], i);
}
cout << ans << ' ';
}
cout << endl;
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}