forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
73 lines (63 loc) · 1.35 KB
/
G.cpp
File metadata and controls
73 lines (63 loc) · 1.35 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
#include <bits/stdc++.h>
using namespace std;
int n, d[100100];
vector <int> a[100100], order;
int check(int add, vector <int> deg)
{
priority_queue < pair<int,int> > q;
for (int i = 1; i <= n; i++)
if (!deg[i])
q.push(make_pair(d[i] + add, i));
order.clear();
while (!q.empty())
{
int x = q.top().second;
q.pop();
order.push_back(x);
for (auto y : a[x])
if (!--deg[y])
q.push(make_pair(d[y] + add, y));
}
for (int i = 0; i < n; i++)
if (n - i > d[order[i]] + add)
return 0;
return 1;
}
int main()
{
ios::sync_with_stdio(0);
freopen("grand.in", "r", stdin);
freopen("grand.out", "w", stdout);
int m, x, y;
while (cin >> n, n)
{
vector <int> deg(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> d[i];
a[i].clear();
}
cin >> m;
while (m--)
{
cin >> x >> y;
a[y].push_back(x);
deg[x]++;
}
int low = 0, high = n, ans = n;
while (low <= high)
{
int mid = (low + high) / 2;
if (check(mid, deg))
{
ans = mid;
high = mid - 1;
}
else low = mid + 1;
}
check(ans, deg);
cout << ans << endl;
for (int i = n - 1; i >= 0; i--)
cout << order[i] << (i ? ' ' : '\n');
}
}