-
Notifications
You must be signed in to change notification settings - Fork 3
/
B.cpp
135 lines (135 loc) · 2.87 KB
/
B.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
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
// LUOGU_RID: 159223145
#include<bits/stdc++.h>
using namespace std;
namespace Luo_ty{
const int MAXN = 200005;
struct G{
int head[MAXN], CNT;
struct edge{
int next, to;
}ed[MAXN];
void add_edge(int u, int v){
ed[++CNT] = (edge){head[u], v};
head[u] = CNT;
}
}g1, g2;
int a[MAXN];
int deg1[MAXN], deg2[MAXN];
int L[MAXN], R[MAXN];
int q[0x66ccff], l, r;
int n, m, k;
void topo_sort1(){
l = r = 0;
for(int i = 1;i <= n;i++){
R[i] = a[i] ? a[i] : k;
if(!deg1[i]) q[++r] = i;
}
while(l < r){
int u = q[++l];
for(int i = g1.head[u];i;i = g1.ed[i].next){
int v = g1.ed[i].to;
deg1[v]--;
R[v] = min(R[v], R[u] - 1);
if(!deg1[v]) q[++r] = v;
}
}
}
void topo_sort2(){
l = r = 0;
for(int i = 1;i <= n;i++){
L[i] = a[i] ? a[i] : 1;
if(!deg2[i]) q[++r] = i;
}
while(l < r){
int u = q[++l];
for(int i = g2.head[u];i;i = g2.ed[i].next){
int v = g2.ed[i].to;
deg2[v]--;
L[v] = max(L[v], L[u] + 1);
if(!deg2[v]) q[++r] = v;
}
}
}
int id[MAXN];
bool cmp(int a, int b){
if(L[a] == L[b]) return R[a] < R[b];
return L[a] < L[b];
}
struct S{
int u, v;
};
bool operator < (S a, S b){
if(a.v == b.v) return a.u < b.u;
return a.v < b.v;
}
set<S> s;
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i = 1;i <= n;i++) scanf("%d", &a[i]);
for(int i = 1, u, v;i <= m;i++){
scanf("%d%d", &u, &v);
g1.add_edge(u, v);
g2.add_edge(v, u);
deg1[v]++;
deg2[u]++;
}
topo_sort1();
topo_sort2();
// puts("");
for(int i = 1;i <= n;i++){
// printf("%d %d\n", L[i], R[i]);
if(deg1[i] || L[i] > R[i]){
printf("-1");
return 0;
}
}
for(int i = 1;i <= n;i++) id[i] = i;
sort(id + 1, id + n + 1, cmp);
int now = 1;
// puts("");
for(int i = 1;i <= k;i++){
// puts("---------------");
// set<S>::iterator It = s.begin();
// while(It != s.end()){
// printf("{%d %d} ", It->u, It->v);
// It++;
// }
// puts("");
while(now <= n && L[id[now]] <= i){
s.insert((S){id[now], R[id[now]]});//set的判重也和重载的运算符有关系...
// printf("_%d %d %d_\n", id[now], R[id[now]], s.size());
now++;
}
// printf("\n%d---\n", now);
// It = s.begin();
// while(It != s.end()){
// printf("{%d %d} ", It->u, It->v);
// It++;
// }
// puts("\n------------");
set<S>::iterator it = s.begin();
if((it->v) < i){
printf("-1");
return 0;
}
if((it->v) > i){
a[it->u] = i;
// printf("%d %d\n", it->u, it->v);
s.erase(it);
continue;
}
while((it->v) == i){
a[it->u] = i;
// printf("{%d %d} ", it->u, it->v);
s.erase(it);
it = s.begin();
}
// puts("");
}
for(int i = 1;i <= n;i++) printf("%d ", a[i]);
return 0;
}
}
int main(){
return Luo_ty::main();
}