We are given an undirected graph with n vertices (blogs) and m edges (references). The process: Johnny will write blogs in some order p_1,...,p_n. When writing a blog v, he looks at its neighbors that have already been written (i.e., appear earlier in the order). Among the topics they have assigned, he chooses the smallest positive integer not used among those neighbor topics. Topics are numbers from 1 to n. He wants that the resulting topic assignment (function f: V -> [1..n]) equals a given desired assignment t_i (for each vertex i, we want f(i)=t_i). We need to output a permutation p such that if Johnny writes in that order, using the lazy strategy, he ends up assigning exactly t_i to each vertex.
Observations: The assignment is defined recursively based on the order. It's similar to a greedy coloring in order using the smallest available color not used by earlier neighbors. That is exactly the greedy coloring algorithm with vertex ordering. The result is a proper coloring? Not exactly because colors can be repeated if they are not adjacent; but adjacent vertices must have different colors because if two adjacent vertices have the same color, then when the later one is colored, its neighbor already has that color, so it would not pick it (since it chooses smallest integer not in neighbor set; if neighbor has color c, it cannot pick c). However, there is a nuance: The later vertex will look at all its already-colored neighbors, and if any neighbor has color c, then c is forbidden. So indeed if two adjacent vertices are colored, the later one cannot have the same color as any earlier neighbor. So the final coloring is a proper coloring (adjacent vertices have distinct colors) because if two adjacent vertices have the same color, consider the later one: it sees the earlier neighbor with that color, thus cannot assign that color, contradiction. So the process yields a proper coloring.
Thus t must be a proper coloring of the graph (adjacent vertices have different topics). Also the greedy algorithm with order p must produce t. Not every proper coloring can be obtained by some ordering of the greedy algorithm; the ordering must satisfy certain constraints. The question: Given desired colors t (maybe not a proper coloring? we need to check), find an order that yields exactly that coloring.
We need to understand condition for existence. For a given coloring, the greedy algorithm works as follows: Process vertices in order p_1,...,p_n. For each vertex v, assign the smallest positive integer not in S(v) = { t(u) | u neighbor of v and u appears earlier }. This assigned value must equal t(v).
Thus for each vertex v, we need that t(v) is the smallest positive integer not in S(v). Equivalently, for each vertex v, for all colors c from 1 to t(v)-1, there exists at least one neighbor u of v that appears earlier with t(u)=c. Because if any smaller color c (< t(v)) is missing from earlier neighbors, then the algorithm would pick that missing smallest color. Also, t(v) itself must not appear among earlier neighbors (otherwise algorithm would skip it, but it might still be available later? Actually algorithm chooses smallest missing color; if t(v) is missing, then it could pick it; but if t(v) is present among earlier neighbors, then it's not missing, so the algorithm would not pick it unless all smaller colors are also present? Let's think: The algorithm selects the smallest positive integer NOT in S(v). If t(v) is in S(v) but all smaller colors are also in S(v), then the smallest missing might be > t(v). But is that possible? For example, neighbor colors include 1,2,3, and t(v)=2. Then S includes 1,2,3, so smallest missing is 4, not 2. So t(v) cannot be in S(v) if it is to be assigned. But could t(v) be assigned if it is not the smallest missing? No, algorithm only picks the smallest missing. So t(v) must be exactly the smallest missing color. Therefore:
Condition (A): For each vertex v, define the set of colors of earlier neighbors S(v). Then:
- For all c in [1, t(v)-1]: c ∈ S(v).
- t(v) ∉ S(v).
Since t(v) can be up to n, this condition essentially says that the earlier neighbors must contain all colors from 1 to t(v)-1, and not contain t(v). Also note that if t(v)=1, then the condition is simply: 1 ∉ S(v) (since no smaller colors). That means no earlier neighbor has color 1.
This is reminiscent of a "greedy coloring order" also known as "no broken color" or something like that. Equivalent: For each vertex v, among its neighbors, all colors less than t(v) must appear on neighbors that come before v; but any neighbor with color < t(v) could be after v, that wouldn't help. So for each v, all neighbors with color < t(v) must appear earlier than v. Additionally, no neighbor with color = t(v) appears earlier (otherwise t(v) would be blocked).
Thus we have a set of constraints on the relative order of vertices based on their colors:
For any edge (u,v):
- If t(u) < t(v): Then since v requires that all colors < t(v) appear earlier, in particular t(u) (< t(v)) must appear earlier among v's neighbors. That means u must appear before v. So we get a directed constraint: u (with smaller color) must precede v (with larger color).
- If t(u) = t(v): They are equal. For either vertex, consider the condition: For v, t(v) should not appear among earlier neighbors, so u cannot be before v if u has same color. Similarly, u cannot be before v if they have same color because then t(u) would be in S(u) if v appears before u? Wait check both: For v, if u appears before v and t(u)=t(v), then t(v) ∈ S(v), violating condition that t(v) ∉ S(v). So u cannot appear before v. Conversely, for u, if v appears before u, then t(u) ∈ S(u). So v cannot appear before u either. Thus two vertices with same color cannot be adjacent? Actually if they are adjacent and have the same color, then no matter the order, one will be earlier and cause the later to have that color in its earlier neighbors, violating condition. But is it possible if they are not adjacent? They are adjacent in this edge, so they are neighbors. So if two adjacent vertices have same color, it's impossible because the later one would see the earlier one with same color. So t must be a proper coloring; indeed for adjacent u,v, t(u) ≠ t(v). So we already have that. So the case t(u)=t(v) cannot happen for adjacent vertices if a valid ordering exists. So we can ignore equal colors among edges.
Thus from edges, we get direction: if t(u) < t(v), then u must come before v.
This yields a partial order: For any edge (u,v), the vertex with smaller color must precede the vertex with larger color. This is necessary because for v (the larger color), it requires all smaller colored neighbors to be before it. But is that sufficient? Let's check: Suppose we have an order that satisfies for all edges (u,v): if t(u) < t(v) then u precedes v. Does that guarantee that for each vertex v, all neighbors with color < t(v) appear before v? Yes, because every neighbor with smaller color is covered by this rule. Also, we need to ensure that no neighbor with color = t(v) appears before v. Since colors are distinct for adjacent vertices, neighbors have colors ≠ t(v). So the only possible conflict is if a neighbor has color larger than t(v); that does not cause a problem because t(v) is not in S(v) from larger colors. Wait, we need t(v) ∉ S(v). Since all neighbors have different colors, S(v) only contains colors from neighbors. If a neighbor has color > t(v), that's fine; if a neighbor has color < t(v), they are before v, so S(v) includes those small colors, but they are not equal to t(v). So condition holds automatically provided the order ensures all smaller neighbors are before v. But do we also need that neighbors with color > t(v) are not before v? They can be before or after; if they are before, they contribute colors > t(v) to S(v), but the smallest missing is still t(v) as long as all colors 1..t(v)-1 are present and t(v) is absent. But is it possible that if a neighbor with color > t(v) appears before, it could cause the smallest missing to be something else? For the smallest missing to be t(v), we need that all colors 1..t(v)-1 are present, and t(v) is absent. The presence of colors > t(v) doesn't affect the smallest missing. So it's fine.
Thus the necessary condition (from edges) that for every edge (u,v), if t(u) < t(v) then u precedes v, is also sufficient? Let's double-check: Suppose we have order that satisfies that. For vertex v, consider its neighbors. All neighbors with smaller color are before v. So S(v) includes all those smaller colors. It does NOT include t(v) because neighbors have distinct colors (since proper coloring) and none equal t(v). So indeed S(v) = {t(u) : u neighbor and t(u) < t(v)} (because any neighbor with larger color may be before or after; but if before, they'd also be in S(v), but that doesn't matter). The smallest positive integer not in S(v) is then the smallest integer not among those smaller colors. Since we have exactly the set of all smaller colors from neighbors, but is it guaranteed that this set covers all integers from 1 to t(v)-1? Not necessarily: It only contains colors of neighbors that are smaller. It's possible that some color c < t(v) is not represented among any neighbor of v. Then S(v) would not contain c, so the smallest missing would be c, not t(v). This is crucial! The condition for v is that all colors from 1 to t(v)-1 must appear in S(v). That means for each c < t(v), v must have at least one neighbor u with t(u)=c, and that neighbor must appear before v. But our edge rule only ensures that if a neighbor exists with smaller color, then that neighbor must be before v. However, it doesn't guarantee existence of neighbors covering all smaller colors. Actually the condition is that v must have neighbors covering all smaller colors. That is an additional requirement: For each vertex v, and for each color c from 1 to t(v)-1, there must exist a neighbor u of v with t(u)=c. Because otherwise, when v is processed, the smallest missing color could be some c < t(v) that is not present among neighbors (earlier or later) but earlier neighbors only are those before v; but could a neighbor with color c appear after v? That wouldn't help because at the time v is processed, that neighbor is not yet written, so its color is not considered. So v must have at least one neighbor with each smaller color, and that neighbor must be before v. Since our order forces all smaller-colored neighbors to be before v, existence of such neighbors is necessary. Therefore, the necessary and sufficient conditions for existence of an ordering are:
- t is a proper coloring: For each edge (u,v), t(u) ≠ t(v).
- For each vertex v, the set of colors among its neighbors must include all integers from 1 to t(v)-1. Equivalently, for each v, for every c < t(v), there exists a neighbor u of v with t(u) = c.
- The partial order defined by directing edges from smaller color to larger color is acyclic (which it automatically is because colors are integers and edges go from lower to higher, so it's a DAG). However, we need to ensure that there exists a total order consistent with these precedence constraints. Since the constraints are that for each edge (u,v) with t(u) < t(v), u must be before v. That is a partial order; it is always possible to topologically order because there cannot be a cycle (since along any directed path, colors strictly increase, so no cycle). So a topological order exists.
But are these conditions enough? Let's test with examples.
Example 1: n=3, triangle edges, t = [2,1,3]. Check condition 2:
- Vertex 1: t=2, needs neighbor with color 1. It has neighbors 2 (t=1) and 3 (t=3). So neighbor with 1 exists (2). OK.
- Vertex 2: t=1, needs no smaller colors. OK.
- Vertex 3: t=3, needs neighbors with colors 1 and 2. Neighbors: 1 (t=2) and 2 (t=1). So both exist. OK.
Proper coloring? Yes (1-2:2≠1, 2-3:1≠3, 1-3:2≠3). So exists. Order 2,1,3 works: directed constraints: edges: (1,2) t1=2>t2=1 so direction from 2 to 1 (since smaller precedes larger? Actually edge (1,2): t2=1 < t1=2, so 2 before 1). Edge (2,3): t2=1 < t3=3 so 2 before 3. Edge (1,3): t1=2 < t3=3 so 1 before 3. So constraints: 2 before 1, 2 before 3, 1 before 3. Topological order: 2,1,3. Good.
Example 2: t = [1,1,1] on triangle. Not proper coloring (adjacent same), so impossible. Output -1.
Example 3: n=5, edges: 1-2,2-3,4-5. t = [2,1,2,2,1]. Check proper coloring: edges: 1-2:2≠1 OK; 2-3:1≠2 OK; 4-5:2≠1 OK. Others not adjacent. Condition 2:
- Vertex1 (t=2): neighbor 2 (t=1) OK, need color1 present, yes.
- Vertex2 (t=1): no requirement.
- Vertex3 (t=2): neighbor 2 (t=1) OK.
- Vertex4 (t=2): neighbor 5 (t=1) OK.
- Vertex5 (t=1): no requirement.
All satisfied. So exists. Directed constraints: Edges: 1-2: t2=1<2 => 2 before 1; 2-3: t2=1<3=2? Actually t3=2, so 2 before 3; 4-5: t5=1<4=2? Actually t4=2, t5=1, so 5 before 4. So constraints: 2 before 1,2 before 3,5 before 4. No constraints between components. Topological order e.g., 2,5,1,3,4 or 2,5,1,3,4? But output example is 2 5 1 3 4. That matches.
Thus conditions seem correct.
Now we need to find any permutation p satisfying: for every edge (u,v) with t(u) < t(v), u appears before v. Also we need to respect that vertices with lower color generally must appear before those with higher color? Not necessarily: For an edge where t(u) < t(v), we only require u before v. But if two vertices are not connected by an edge, there might be no direct constraint. However, condition 2 indirectly imposes that for each vertex v with color k, it must have neighbors covering all colors 1..k-1. This ensures that there are edges from lower-colored vertices to v. But that doesn't impose ordering between vertices of the same color (they are not adjacent because proper coloring, so no constraint), nor between vertices of different colors that are not adjacent (no direct constraint). But we could topologically order according to the DAG formed by directed edges from lower color to higher color.
Thus algorithm: Check proper coloring: For each edge, if t[u]==t[v], output -1.
Check condition 2: For each vertex v, we need that for every c from 1 to t[v]-1, there exists at least one neighbor u with t[u] == c. Since n up to 5e5, and sum of degrees up to 1e6. We need to verify efficiently. For each vertex v, we can compute the set of neighbor colors, and check if it contains all numbers from 1 to t[v]-1. But t[v] can be up to n; doing per vertex O(t[v]) would be too large. However, we can observe that condition 2 is equivalent to: For each vertex v, the minimum neighbor color is 1? Not exactly. Actually it requires that the set of neighbor colors covers an interval [1, t[v]-1]. Since t[v] can be large, we need a more clever check. But note that graph edges are at most 5e5, and each vertex's degree may not be huge, but t[v] can be large even if degree small. For condition 2 to hold, degree must be at least t[v]-1 because it needs at least t[v]-1 distinct colors among neighbors (since each color from 1 to t[v]-1 must appear, and colors are distinct? Actually neighbors can have same color? No, proper coloring forbids same color among adjacent vertices, but two neighbors of v could have the same color if they are not adjacent to each other? Actually edges are only between v and its neighbors, but two neighbors of v might share an edge? Not necessarily. However, condition 2 requires that for each c < t[v], there exists at least one neighbor with that color. It doesn't require those neighbors to have distinct colors? But if two different c's are different, they are different colors, so they must be assigned to possibly same neighbor? No, a neighbor has a single color. So to cover k distinct colors, you need at least k distinct neighbors. So degree(v) must be at least t[v]-1. So we can first check: if deg(v) < t[v]-1, impossible. That's easy to compute.
But even if degree is large enough, we still need to check that the set of neighbor colors actually includes all those numbers. For each vertex v, we can collect the neighbor colors into a set or sort them, then check if the smallest is 1 and if it's contiguous up to t[v]-1? Because they need to cover exactly [1, t[v]-1]? Actually they need to cover all integers from 1 to t[v]-1; it's not required that there are no other colors; they can have colors > t[v]. So we need to check that for each c=1..t[v]-1, there exists neighbor with that color. Since degree could be large, we need an efficient method.
Approach: For each vertex v, we can iterate over its neighbors and record which colors from 1 to t[v]-1 are present. However t[v] can be large, but we can bound total work by something like sum over v of min(deg(v), t[v])? Since deg(v) might be large, but t[v] can be up to n. We need to be careful not to O(n^2). n up to 5e5, edges 5e5, so total degree sum is 2m ≤ 1e6. So sum of degrees is 1e6. If for each vertex we iterate over all its neighbors and mark up to t[v]-1, that could be O(m + sum of t[v])? But t[v] can be up to n, and n is 5e5, sum t[v] can be O(n^2) worst-case, which is too high. However, we can use a global approach: condition 2 can be restated as: For each vertex v, the smallest missing color among neighbors? Actually we need that for each v, all colors 1..t[v]-1 appear among its neighbors. This is a local condition, but we can use the fact that colors are integers and we can use sorting or something.
Alternative perspective: For each color value c, consider all vertices with that color. Then for any vertex v with color > c, it must have at least one neighbor of color c. That is, for each color c, the set of vertices with color c must be a dominating set for all vertices with color > c? Not exactly: Each vertex v with t[v] > c must have at least one neighbor of color c. So for each c, for every vertex v with t[v] > c, there is an edge from v to some vertex with color c. This is a strong condition.
We can check this efficiently using data structures: For each vertex v, we need to know if its neighbor set covers all colors less than t[v]. Since t[v] might be large but degree is limited, it's often that t[v] is not too large. However, there could be high-degree vertices with large t[v] requiring many colors. But total edges is limited to 5e5, so high-degree vertices are limited. Actually worst-case: a star graph with center vertex degree 5e5, t[center] could be 5e5, requiring all colors 1..n-1 among its neighbors. That's plausible: center has many neighbors; if they each have distinct colors 1..n-1, it's possible. So condition 2 would require that center's neighbor set includes all colors 1..n-1. Checking that for the center would involve scanning its neighbors (degree 5e5) and verifying that colors 1..n-1 are present. That's O(deg) which is O(m) = 5e5, which is fine. But if there are many vertices with high t[v] and high degree, the sum of degrees across all vertices is 1e6, so scanning each vertex's neighbors once is O(m). However, for each vertex, we also need to check that for each c from 1 to t[v]-1, there is a neighbor with color c. Scanning neighbors and building a boolean array of size t[v] for each vertex could be too heavy if t[v] is large for many vertices. But note total work per vertex could be O(deg(v) + t[v]) if we allocate an array of size t[v]. Since sum of deg(v) is 1e6, but sum of t[v] could be O(n^2) in worst case, e.g., if all t[v] are about n/2, sum t[v] = O(n^2) ~ 2.5e11, impossible. So we need a method that avoids per-vertex O(t[v]) time.
Idea: Use the fact that each neighbor color can be used to satisfy the requirement for many vertices? But requirement is per vertex, not global.
We need an efficient way to verify condition 2 for all vertices without iterating over ranges.
Observation: Condition 2 can be transformed into a condition on ordering: Since we already have the precedence constraints, maybe condition 2 is automatically enforced if we also enforce that the ordering is a topological sort of the DAG defined by edges? Not necessarily; consider a vertex v with t[v]=3, but its neighbors are only of colors 1 and 2? Actually if it has both 1 and 2, then condition 2 holds. But if it only has 1 and not 2, then even though we have constraints (if there is an edge to a color 2 neighbor? But if neighbor with color 2 is absent, then there is no constraint, but condition 2 fails, and any order would fail because when v is processed, colors present among earlier neighbors might be just {1}? But wait if v has no neighbor of color 2, then its neighbor set lacks color 2. But if we order vertices appropriately, maybe v is processed before any neighbor of color 2 appears? But color 2 neighbor is a vertex w with t[w]=2. Since v and w are not neighbors, w's color doesn't affect v. So when v is processed, S(v) only includes neighbors that are before. If v has no neighbor with color 2 at all, then regardless of order, color 2 will never be in S(v). So the smallest missing might be 2 if 1 is present. But we need v to be assigned color 3, so we need smallest missing to be 3. That requires that both 1 and 2 appear among earlier neighbors. 2 cannot appear because v has no neighbor of color 2. So impossible. So condition 2 is necessary.
But can we check it more efficiently?
We can do the following: For each vertex v, we need to check that the set of colors of its neighbors includes all colors from 1 to t[v]-1. This is equivalent to: The minimum missing color in neighbor set is at least t[v]. Or that the smallest positive integer not in neighbor colors (i.e., mex of neighbor colors) is at least t[v]. Actually mex (minimum excluded) of neighbor colors would be the smallest c >=1 not present. For v to have t[v] assigned, we need that mex of neighbor colors (considering all neighbors, not just earlier) is at least t[v]? Wait careful: The algorithm only sees earlier neighbors, not all neighbors. However, condition 2 requires that every c < t[v] appears among neighbors (some neighbor) to be potentially before v. But they must be before v for v to get t[v]. However, if a neighbor with color c exists but comes after v, then it doesn't help. But we can always order vertices such that all neighbors with smaller colors come before v, because we have constraints that those neighbors (since t(u) < t(v)) must appear before v. That's exactly the partial order. So as long as such neighbors exist and we respect the partial order, they will be before v. So condition 2 reduces to existence of a neighbor of each smaller color. So we need to check existence, not order. So we need to verify for each v: for all c in [1, t[v]-1], there exists neighbor u with t[u]=c.
Thus we need to check this for all v. This is a local condition but we can precompute for each vertex the set of neighbor colors and compute the mex? Actually we need to check if the mex of neighbor colors is >= t[v]? Because if mex of neighbor colors is m, then colors 1..m-1 are present, but color m is missing. For v, we need that all colors 1..t[v]-1 are present. So we need mex(neighbor colors) >= t[v] (or maybe > t[v]? If mex = t[v], then colors 1..t[v]-1 are present, but color t[v] is missing, which is fine). So condition: mex(neighbor colors) >= t[v]. However, note that mex(neighbor colors) is the smallest positive integer not present among neighbors. If mex >= t[v], then indeed all colors 1..t[v]-1 are present. That is necessary and sufficient.
But wait: Is it possible that neighbor colors include all colors 1..t[v]-1 but also maybe missing some higher color, that's fine. So condition is exactly that mex(neighbor colors) >= t[v].
But we also need proper coloring: adjacent vertices have different colors. So overall, existence conditions:
- For each edge (u,v): t[u] != t[v].
- For each vertex v: mex(neighbor colors) >= t[v].
Let's test: Example 1: vertex 1 neighbor colors: {1,3} mex=2 >=2 ok; vertex 2 neighbor colors: {2,? Actually neighbors: 1 (2),3 (3)? Wait vertex 2 neighbor colors: from 1 (2) and 3 (3) => {2,3}, mex=1 >=1 ok; vertex 3 neighbor colors: {2,1} mex=3 >=3 ok.
Example 3: vertex 1 neighbor colors: {1} mex=2 >=2 ok; vertex 2 neighbor colors: {2,2}? Actually vertex 2 neighbors: 1 (2),3 (2) => {2} mex=1 >=1 ok; vertex 3 neighbor colors: {1} mex=2 >=2 ok; vertex 4 neighbor colors: {1} mex=2 >=2 ok; vertex 5 neighbor colors: {2} mex=1 >=1 ok.
Seems correct.
Now, is mex >= t[v] also sufficient along with proper coloring? I think yes, because if we then order vertices according to topological sort of DAG from low to high colors, we will get a valid order. But need to verify: Suppose mex(neighbor colors) >= t[v] for all v, and proper coloring. Then for each vertex v, all colors less than t[v] appear among neighbors. Since we will order by topological sort ensuring that for every edge (u,v) with t[u] < t[v], u comes before v. But is that guarantee that all neighbors with smaller colors are before v? Yes, because for each neighbor u with t[u] < t[v], the edge gives constraint u before v. Since we follow topological order, u will be before v. So indeed all smaller-colored neighbors are before v. And because proper coloring, no neighbor has color t[v]. So S(v) contains all colors from 1 to t[v]-1, and does not contain t[v]. So the smallest missing is t[v]. Good.
Thus condition is exactly that.
Now, we must ensure that topological order exists, which it does because the graph of constraints is acyclic: edges go from lower color to higher color. However, note: There may be edges between vertices with same color? Already excluded by proper coloring. So all edges have endpoints of different colors, thus we direct from lower to higher. This directed graph has no cycles because any directed path would have strictly increasing colors, so cannot return to start. So it's a DAG.
Thus the solution: Check proper coloring; for each edge, if t[u]==t[v] -> impossible. Then compute for each vertex v: mex of neighbor colors? But computing mex for each vertex naively would be O(deg(v) + something). We need to do it efficiently for up to 5e5 vertices. Since sum of degrees is 1e6, we can for each vertex v, collect its neighbor colors, then compute mex. But careful: mex can be up to deg(v)+1, but we need to check if mex >= t[v]. To compute mex, we can sort neighbor colors or use a boolean array of size (deg(v)+1) because mex is at most deg(v)+1 (since there are at most deg(v) distinct colors, the mex is at most deg(v)+1). However, colors are up to n, but we only need to know if all colors from 1 to t[v]-1 are present. So we can check if t[v]-1 <= deg(v) (since at most deg(v) distinct colors can be present, but they could be not the necessary ones). Actually to have all colors from 1 to t[v]-1, deg(v) must be at least t[v]-1, as argued. So first we can check deg(v) >= t[v]-1; if not, impossible. Then we can check more precisely by looking at neighbor colors.
Since sum of degrees is 1e6, we can for each vertex v, iterate over its neighbors, and record which colors from 1 to t[v]-1 are covered. But t[v]-1 could be large (up to n) while deg(v) is small; but if deg(v) is small, then deg(v) < t[v]-1 would have already failed, so we only need to check vertices where deg(v) >= t[v]-1, which means t[v] <= deg(v)+1. So for those vertices, t[v]-1 <= deg(v). So the range we need to check is at most deg(v). So we can allocate an array (or vector) of size deg(v)+1 for each vertex, but that could lead to total memory O(m) if we do it per vertex? Actually sum over v of deg(v) = 2m = up to 1e6, so total size of all such temporary arrays could be O(m) if we allocate per vertex and then discard. That's acceptable. However, we must be careful about time: For each vertex v, we need to mark colors up to t[v]-1, but t[v]-1 <= deg(v). So we can create a boolean vector of length t[v] (or t[v]-1) initially false, then iterate neighbors, for each neighbor color c if c < t[v], set mark[c]=true. After scanning neighbors, we check if all marks 1..t[v]-1 are true. This would be O(deg(v) + t[v]) per vertex, but t[v] <= deg(v)+1, so O(deg(v)). Summing over all vertices, total O(sum deg(v)) = O(m). But careful: For a vertex with deg(v)=500k and t[v]=500k, we would allocate a boolean array of size 500k and run through neighbors (500k) and then scan through 1..t[v]-1 (500k) to check all true. That's O(1e6) for that vertex, acceptable. Since total deg sum is 1e6, the extra scanning of 1..t[v]-1 could be O(deg(v)) per vertex but sum over vertices of deg(v) is 1e6, and each t[v]-1 <= deg(v), so sum of t[v] over vertices that pass the degree condition is at most sum deg(v) + n (since each vertex that fails deg condition we might not check? Actually we only check those that pass? But we can first compute deg and t, and if deg(v) < t[v]-1, output -1. For those that pass, we do the detailed check. Then sum_{v: deg(v) >= t[v]-1} t[v] <= sum_{v} (deg(v)+1) = O(m + n) = up to 1.5e6. Because for each such v, t[v] <= deg(v)+1, so sum t[v] <= sum (deg(v)+1) = 2m + n ≤ 1.5e6. That's fine. So overall O(n + m) time.
But we must be careful about memory: Using boolean arrays per vertex could be allocated and deallocated; we can use a global array and reuse it with a timestamp technique to avoid repeated allocations. Since total sum of needed sizes is about 1e6, we could allocate a single vector of size n for all vertices, but we need to reset marks for each vertex. Using timestamp array (an array of ints last_seen) and a counter can work. But simpler: For each vertex, we can collect neighbor colors, sort them, and then check if they contain 1,2,...,t[v]-1 consecutively? But sorting each adjacency list could be O(d log d), sum might be O(m log m) which is also acceptable (1e6 log). However, we need to check presence of all numbers 1..t[v]-1; after sorting, we can iterate through sorted list and keep a pointer to expected numbers. But careful: neighbor colors may have duplicates (since two neighbors could have same color? Wait, proper coloring forbids adjacent vertices to have same color, but two neighbors of v might not be adjacent to each other, so they could have the same color. That is allowed: v is adjacent to both, but they can have the same color because they are not necessarily adjacent to each other. Condition 2 only requires existence of at least one neighbor with each color; duplicates are fine. So we need to check distinctness? Actually we just need existence, so we can use a set or boolean marks.
So the boolean marking approach O(deg(v) + t[v]) is straightforward and O(m) total.
Implementation plan:
-
Read n, m.
-
Build adjacency list (undirected).
-
Read array t[1..n].
-
Check proper coloring: for each edge (u,v) (we'll have edges stored), if t[u]==t[v], output -1.
-
Also compute degree deg[v] = len(adj[v]).
-
For each vertex v: if deg[v] < t[v]-1, output -1.
-
Then for each vertex v, we need to verify that all colors 1..t[v]-1 appear among neighbors.
Approach:
We'll allocate a global vector mark of size (n+1) perhaps, but resetting for each vertex would be O(n^2). Better to use a timestamp technique: an array used[1..n] storing last "visited" id (e.g., version). We'll have a global integer cur = 0. For each vertex v:
cur++;
For each neighbor u of v:
int c = t[u];
if (c < t[v]) {
used[c] = cur;
}
Then we check for i = 1 to t[v]-1:
if used[i] != cur -> missing -> output -1.
This is O(deg(v) + t[v]) per vertex, with t[v] <= deg(v)+1, so total O(m+n) as argued.
However, note that used array of size n+1, resetting via cur integer and checking used[i] == cur is O(1) per check. cur increments up to n (since we process each vertex). So used array size n+1, fine.
-
If all checks pass, we need to produce a permutation p (order) such that for every edge (u,v) with t[u] < t[v], u appears before v. This is equivalent to a topological sort of the DAG where edges are directed from lower color to higher color. Since it's a DAG, we can simply sort vertices by their color (increasing), but need to ensure that if t[u] < t[v], u precedes v. That's exactly sorting by t. But wait: If t[u] < t[v], then in sorting by t, u will come before v. However, what about vertices with equal t? They are not adjacent (since proper coloring), so there is no constraint between them. So any order among equal color vertices is fine. However, is it always valid to simply order vertices by increasing t? Let's test with example 1: t = [2,1,3] -> sorted by t: vertices: t=1:2, t=2:1, t=3:3 => order 2,1,3 works. Example 3: t: 2,1,2,2,1. Sorted by t: all t=1: vertices 2,5; then t=2: vertices 1,3,4. In that order, we need to ensure within t=2 group there is no constraint? There are no edges between vertices of same color because proper coloring? Actually vertices of same color may be adjacent? No, proper coloring ensures adjacent vertices have different colors, so within same color, they are not adjacent. So no constraints. So any order among them works. But example output is 2 5 1 3 4, which is exactly sorted by color: first t=1 (2,5) then t=2 (1,3,4). Within t=1, order (2,5) or (5,2) both fine. Within t=2, order could be any. So output matches.
Thus it seems ordering vertices by increasing t works! But is it always valid? Let's double-check the condition: For each edge (u,v) with t[u] < t[v], we need u before v. Sorting by t ensures that. Additionally, we need that for each vertex v, all neighbors with smaller colors appear before v. That is satisfied because all vertices with smaller color are before v in this order. So indeed ordering by t (non-decreasing) works. However, consider if there are vertices with same color but there is an edge? That would be violation of proper coloring, which we already ruled out. So yes.
But wait: There is also a subtlety: The algorithm says Johnny chooses the smallest topic not covered by already written neighbors. In our construction, when we order by increasing t, at the time we write vertex v, all its neighbors with smaller t are already written (since they appear earlier). But what about neighbors with larger t? They are written later. So S(v) includes all neighbors with smaller t (colors less than t[v]) and also possibly some neighbors with larger t if they appear earlier? But they cannot appear earlier because we order by increasing t, so larger t vertices come later. So S(v) = { t(u) | u neighbor of v and t(u) < t(v) }. That's exactly the set of colors less than t[v]. And by condition 2, this set includes all colors from 1 to t[v]-1. Hence smallest missing is t[v]. So ordering by increasing t yields the desired assignment.
Thus the answer is simply: If conditions hold, output any permutation where vertices are sorted by their desired topic numbers (non-decreasing). That's it.
But is there any case where ordering by increasing t fails because of some subtlety about the mex condition? Let's think: Condition 2 ensures that for each vertex v, all colors 1..t[v]-1 appear among its neighbors. However, does it guarantee that among neighbors with color < t[v], all those colors are present? Yes. But does it guarantee that when we order by increasing t, the set of earlier neighbors exactly equals all neighbors with smaller t? Yes, because earlier vertices are exactly those with t < t[v]. So S(v) = { t(u) | u neighbor, t(u) < t[v] }. That set is exactly the set of colors from neighbors that are less than t[v]. Condition 2 says that this set contains all numbers 1..t[v]-1. So the smallest missing is t[v]. So works.
Now, could there be a valid ordering that is not sorted by t? Yes, many. But sorted by t is easiest.
But before we finalize, we must ensure that condition 2 (mex >= t[v]) is indeed necessary and sufficient given proper coloring and ordering by increasing t. We already argued necessity. For sufficiency: proper coloring + mex condition => ordering by increasing t works. Let's test on a potential counterexample.
Consider a graph: 1-2, 2-3, 1-3 (triangle). t: 1:2, 2:1, 3:3. Check proper coloring: yes. Condition mex: For vertex1 neighbors colors: {1,3} mex=2 >=2 ok. vertex2 neighbors: {2,3} mex=1 >=1 ok. vertex3 neighbors: {2,1} mex=3 >=3 ok. Order by t: 2 (1), 1 (2), 3 (3). Works.
Consider a graph: 1-2, 2-3, t: 1:3, 2:2, 3:1. Proper coloring? 1-2:3≠2 ok, 2-3:2≠1 ok. Condition mex: vertex1 neighbors: {2} mex=1? neighbor colors: only 2, so mex=1. But t[1]=3, mex=1 <3 -> fails. So impossible. Indeed can we find an order? Let's try order: we need for edge (1,2): since t2=2 < t1=3 => 2 before 1. Edge (2,3): t3=1 < t2=2 => 3 before 2. So constraints: 3 before 2 before 1. So order: 3,2,1. Now process: v3: no earlier neighbors, picks topic 1 -> ok. v2: earlier neighbor 3 with topic 1, so picks smallest not covered =2 -> ok. v1: earlier neighbors: 2 (topic 2). Also neighbor 3? 3 is not adjacent? Actually graph: 1-2 only? Wait edges: 1-2 and 2-3, so 1 is not adjacent to 3. So only earlier neighbor is 2 with color 2. Set S={2}. smallest missing is 1? Actually smallest positive integer not in S is 1, but t1=3. So assignment would be 1, not 3. So indeed fails. So condition needed.
Now consider a graph where condition holds but ordering by increasing t might not work due to some vertices with same color? But same color vertices are not adjacent, so fine.
What about a vertex v with t[v]=k and its neighbors with smaller colors cover all 1..k-1, but there is also a neighbor u with color < k that appears later if we sort by t? That's impossible because if t(u) < t(v), then in ordering by increasing t, u comes before v. So all smaller colored neighbors are earlier. Good.
Thus solution appears straightforward: Check proper coloring, check for each vertex that mex(neighbors colors) >= t[v], then output vertices sorted by t. However, we must also consider that t[v] can be 1: then condition trivially holds (no requirement). For vertex with t[v]=1, we need to ensure that no neighbor has topic 1? Actually condition from earlier: For vertex v with t[v]=1, we need t(v) not in S(v). Since there are no smaller colors, the only condition is that when v is processed, no earlier neighbor has color 1. In ordering by increasing t, all vertices with t=1 come first. Among those, they might be adjacent? They cannot be adjacent because proper coloring: if two vertices both have t=1, they cannot be adjacent (otherwise they'd have same color). So no edges among t=1 vertices. Therefore, for a vertex v with t=1, all its neighbors have t > 1 (since proper coloring), and those neighbors appear later in the order. So at time v is written, it has no earlier neighbors, so S(v)=empty, smallest missing =1, works. So condition holds automatically if proper coloring and mex >= t[v]? For t=1, mex(neighbors) is smallest positive integer not in neighbor colors; it could be 1 (if no neighbor has 1) or >1. For t=1, we need mex >=1, which is always true. Actually mex >=1 trivially true. So condition okay.
Now verify that condition 2 (mex >= t[v]) also implies proper coloring? Not necessarily; it's possible that adjacent vertices have same color but mex condition holds? Example: two vertices u and v both color 2, edge between them. Then for u, neighbor colors include v's color 2, so mex of neighbor colors? If v is neighbor, neighbor colors = {2}. mex=1, but t[u]=2, so mex=1 <2 -> fails. So proper coloring is enforced indirectly? Not always: consider triangle with all t=2? But then for each vertex, neighbor colors set includes 2 (from both neighbors), mex=1 <2, fails. So maybe condition 2 automatically forces proper coloring? Let's test: Suppose adjacent u and v have same color c. Consider vertex u: its neighbors include v with color c. So the set of neighbor colors includes c. Then mex(neighbor colors) is the smallest positive integer not in that set. Since c is in the set, mex <= c? Actually if c > 1 and set includes all numbers from 1 to c-1, mex could be c if 1..c-1 are present. But if set includes all numbers 1..c-1 as well, mex would be c if c not present? Wait c is present, so mex cannot be c because c is present; the smallest missing could be something else. For mex to be >= c+1, we need all numbers 1..c present. But c is present, but we need all numbers 1..c present? Actually mex >= t[u] = c means smallest missing integer is at least c. That requires that all integers 1..c-1 are present. It does not require that c is present or not. But if c is present, that's okay because mex could be > c if 1..c are all present. But is it possible for mex to be > c? If the set of neighbor colors includes all numbers from 1 to c, then mex = c+1 (if c+1 not present). Then mex >= c holds (since c+1 >= c). So it's possible that mex >= c even though neighbor includes color c. For example, vertex u has neighbors with colors 1,2,3, and t[u]=3. Then neighbor colors = {1,2,3}, mex=4 >=3. So proper coloring could be violated if adjacent vertices have same color? But if u and v have same color c, then v is neighbor of u, so u's neighbor colors include c. For mex >= c to hold, we need that all numbers 1..c-1 appear among other neighbors. That's possible if u has many neighbors. So condition 2 alone does not forbid adjacent same color? Let's test a simple graph: two vertices connected by an edge, both t=1? Then mex for each: neighbor colors = {1} (since each sees the other), mex=1? Actually set {1}, smallest positive integer not in set is 2, so mex=2? Wait: smallest positive integer not in {1} is 2, so mex=2. t=1, mex=2 >=1 holds. So condition 2 would be satisfied for both vertices. But proper coloring fails because adjacent vertices have same color. However, check the process: If we order by increasing t (both t=1), any order; suppose we order u then v. u gets topic 1 (no earlier neighbors). v gets: earlier neighbor u has topic 1, so S(v)={1}, smallest missing is 2, so v gets topic 2, not 1. So the desired assignment fails. So condition 2 alone does not guarantee proper coloring; we must also enforce proper coloring separately. In that example, both t=1, but they are adjacent, proper coloring check would catch it (t[u]==t[v] on edge). So we need both checks.
Thus algorithm:
- Read n,m.
- Build adjacency list (undirected).
- Read t[1..n].
- For each edge (a,b) from input, check if t[a] == t[b]; if yes, print -1 and exit.
- Compute degree for each vertex (len adj).
- For each vertex v:
if deg[v] < t[v] - 1: print -1 and exit.
- For each vertex v:
Use timestamp array to mark colors of neighbors that are < t[v].
For each neighbor u:
c = t[u];
if c < t[v]:
mark[c] = cur; // cur is unique for v
Then loop i from 1 to t[v]-1:
if mark[i] != cur: print -1 and exit.
This checks mex condition.
Alternatively, we could early exit if any fails.
- If all passed, construct order: sort vertices by t increasing. Output their indices.
Complexity: O(n + m + total deg) = O(n + m).
Now we need to think about correctness of the condition: mex(neighbor colors) >= t[v] is equivalent to requiring that all colors 1..t[v]-1 appear among neighbors. That's correct. But is it also sufficient together with proper coloring? We argued yes with order by increasing t. However, is there any scenario where ordering by increasing t fails even though conditions hold? Let's test a more complex case. Consider vertex v with t[v]=k, and it has neighbors with colors covering 1..k-1, but among those neighbors, some have color <k but also there are neighbors with color also <k but that's fine. In order by increasing t, all neighbors with color <k come before v. But could it be that some neighbor with color <k appears after v? No, because ordering by t ensures that any vertex with color <k is placed before v. So all such neighbors are before v. So S(v) includes exactly the colors from those neighbors, which include all 1..k-1. So smallest missing is at least k. Could it be that S(v) also includes k? That would require a neighbor with color k that appears before v, but any neighbor with color k has t = k, which equals t[v]; in order by increasing t, where do we place vertices with equal t? They could be before or after v. If a neighbor with same color k is placed before v, then S(v) would include k, causing smallest missing maybe >k if all 1..k-1 present. But condition (proper coloring) ensures that no neighbor has same color as v. So no neighbor with color k exists. So S(v) does not contain k. Therefore the smallest missing is exactly k. So assignment matches.
Now, what about vertices with t[v] = something, but there might be a neighbor with color less than t[v] that is also before v but there is also a neighbor with color less than t[v] that is after v? That doesn't matter. So fine.
Thus ordering by t works.
Edge case: t[v] may be > n? Input says t_i between 1 and n. So ok.
Now let's verify with some random tests.
Consider n=4, edges: 1-2, 2-3, 3-4, 1-4? Actually make a path 1-2-3-4. t: 1:3, 2:1, 3:2, 4:4. Check proper coloring: 1-2:3≠1 ok; 2-3:1≠2 ok; 3-4:2≠4 ok. Condition mex:
- v1: neighbors: {1} from 2; mex of {1} is 2; t1=3 -> 2<3 fails? Actually need all 1..2? t1=3 needs colors 1,2. It has neighbor with 1 only, missing 2. So condition fails. So impossible.
If we adjust t: 1:2, 2:1, 3:3, 4:4? Check v1 neighbors: {1} -> mex=2 >=2 ok? t1=2 needs 1 present -> yes. v2: neighbors {2,3}? Actually 2 connected to 1 (2) and 3 (3) -> neighbors colors {2,3}, mex=1 >=1 ok. v3 neighbors: {1,4}? Actually 3 connected to 2 (1) and 4 (4) -> colors {1,4}, mex=2? t3=3 needs 1,2 present; missing 2 -> fails. So impossible.
Now create a valid example: Suppose graph: star with center 1 connected to leaves 2,3,4,5. t: center=5, leaves: 1,2,3,4. Check proper: all leaves distinct colors? Leaves not adjacent among themselves, so ok. mex for center: neighbors colors {1,2,3,4} mex=5 >=5 ok. Each leaf: t leaf = 1..4, need check. For leaf with t=1: neighbors colors {5} mex=1? Actually set {5} -> smallest positive not in set is 1, so mex=1 >=1 ok. leaf t=2: neighbor color {5} mex=1 <2 fails! Wait leaf with t=2 (say vertex 2) has neighbor center with color 5 only. Need colors 1 present? t=2 requires neighbor with color 1, but not present. So fails. So we need leaves also have neighbors covering 1..t-1. Since each leaf only connects to center (color 5), they cannot have colors 1..t-1 unless t=1. So leaves must have t=1. So this assignment invalid. So condition 2 enforces that any vertex with t>1 must have neighbors covering all smaller colors.
Thus the problem reduces to verifying that the given t is a "greedy coloring" with respect to some order, which is equivalent to these two conditions.
Now we need to be careful: The condition we derived (mex >= t[v]) is equivalent to: For each vertex v, the set of colors among its neighbors includes all integers from 1 to t[v]-1. Is there any additional subtlety about multiple neighbors covering same color? No.
Now, can we prove that if both conditions hold, ordering by t works? Yes.
Thus solution is quite simple.
But wait, the problem statement: "He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors." That's exactly greedy coloring.
Now we must confirm that ordering by t always yields a valid order, and that if the conditions are satisfied, the resulting assignment will be exactly t. Let's simulate a potential tricky case: Suppose we have vertex v with t[v]=3, neighbors: u (t=1), w (t=2). And also some other neighbor x with t=4 (but that's fine). Condition holds: neighbors cover 1 and 2. Order by t: u (1), w (2), v (3), x (4). At v's turn, earlier neighbors: u and w (both earlier) so S(v)={1,2}. Smallest missing =3. Good.
What about if there is also neighbor y with t=1, but that's duplicate; still fine.
Now, if there is a vertex v with t[v]=3, but its neighbors covering 1 and 2 are not both before v if we order strictly by t? They are before because they have smaller t. So fine.
Now, consider the possibility of vertices with same t that are adjacent? That would violate proper coloring, so we already check that.
Thus solution stands.
Implementation details:
-
Input size up to 5e5, so use fast I/O.
-
We'll store edges as list of pairs for checking t equality and also for adjacency list building.
-
We'll have vector<vector> adj(n+1).
-
Compute deg array as vector deg(n+1).
-
For proper coloring check: while reading edges, we can add to adj and also check t[a]==t[b]. But careful: we need to check after reading t array. So we should first read t array, then read edges? Input order: first line n,m, then m lines edges, then last line t. So we read edges before we know t. So we need to store edges first, then read t, then do checks. So we'll store edges in vector<pair<int,int>> edges(m). Then after reading t, we can build adjacency list and check proper coloring.
-
For mex check: We need to iterate neighbors for each vertex. Since adjacency list may be large, we can build it after reading t.
Steps:
- Read n,m.
- edges.resize(m)
- for i in 0..m-1: read a,b; store edges[i] = {a,b}
- Read t[1..n].
- Initialize adj(n+1).
- For each edge (a,b):
adj[a].push_back(b);
adj[b].push_back(a);
if t[a] == t[b]: print -1; return.
- Compute deg[v] = adj[v].size().
- For each v: if deg[v] < t[v] - 1: print -1; return.
- Create array used of size n+1, and an integer cur=0. Possibly vector mark(n+1, 0).
- For each v from 1 to n:
if t[v] == 1: continue; // no need to check as condition vacuous (mex>=1 always true? Actually need to ensure no neighbor with t=1? But proper coloring already ensures neighbors have different colors, so no neighbor has t=1 if t[v]=1? Wait t[v]=1, neighbor could have t=1? That would be same color, prohibited by proper coloring check. So it's fine. Also mex condition: we need all colors from 1 to 0 (none). So it's fine. So we can skip.
else:
cur++;
for each u in adj[v]:
c = t[u];
if (c < t[v]):
used[c] = cur;
for i=1; i <= t[v]-1; i++:
if used[i] != cur:
print -1; return.
But note: used array of size n+1. That's okay. However, we need to ensure that used indices up to t[v]-1 are valid. t[v] <= n, so ok.
Potential optimization: We can break early if we find missing.
Now, if all checks pass, produce order: sort vertices 1..n by t[i] ascending. If tie, any order.
Print the indices.
But we must ensure that sorting by t yields a permutation. For large n, sorting O(n log n) is fine.
Now test with sample inputs.
Sample 1:
n=3,m=3 edges: 1-2,2-3,3-1. t=[2,1,3].
- Proper coloring: all edges have different t? 1-2:2!=1,2-3:1!=3,3-1:3!=2. ok.
- deg: each deg=2. t[1]-1=1 <=2, t[2]-1=0, t[3]-1=2 <=2.
- mex check:
v1 t=2: neighbors:2(t=1),3(t=3). Process: used[1]=cur; i=1: used[1]==cur ok.
v2 t=1 skip.
v3 t=3: neighbors:1(t=2),2(t=1). used[1]=cur, used[2]=cur. Check i=1,2 ok.
So pass.
Order sorted by t: vertices with t=1: 2; t=2:1; t=3:3 -> 2 1 3. matches.
Sample 2:
t all 1. Proper coloring check: edges: each edge connects vertices both t=1 -> t[a]==t[b] -> -1.
Sample 3:
edges: 1-2,2-3,4-5. t: 2,1,2,2,1.
- Proper coloring: 1-2:2!=1 ok; 2-3:1!=2 ok; 4-5:2!=1 ok.
- deg: 1:1, t1-1=1 <=1; 2:2, t2-1=0; 3:1, t3-1=1 <=1; 4:1, t4-1=1 <=1; 5:1, t5-1=0.
- mex:
v1 t=2: neighbors:2(t=1) -> used[1]=cur; i=1: ok.
v3 t=2: neighbors:2(t=1) -> used[1]=cur; i=1: ok.
v4 t=2: neighbors:5(t=1) -> used[1]=cur; i=1: ok.
others skip.
So pass.
Order sorted by