codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1361A.md (3693B)


      1 Source: https://codeforces.com/problemset/problem/1361/A
      2 
      3 A. Johnny and Contribution
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Today Johnny wants to increase his contribution. His plan assumes writing $n$ blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.
     22 
     23 There are $n$ different topics, numbered from $1$ to $n$ sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. 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. It's easy to see that this strategy will always allow him to choose a topic because there are at most $n - 1$ neighbors.
     24 
     25 For example, if already written neighbors of the current blog have topics number $1$, $3$, $1$, $5$, and $2$, Johnny will choose the topic number $4$ for the current blog, because topics number $1$, $2$ and $3$ are already covered by neighbors and topic number $4$ isn't covered.
     26 
     27 As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
     28 
     29 Input
     30 
     31 The first line contains two integers $n$ $(1 \leq n \leq 5 \cdot 10^5)$ and $m$ $(0 \leq m \leq 5 \cdot 10^5)$ — the number of blogs and references, respectively.
     32 
     33 Each of the following $m$ lines contains two integers $a$ and $b$ ($a \neq b$; $1 \leq a, b \leq n$), which mean that there is a reference between blogs $a$ and $b$. It's guaranteed that the graph doesn't contain multiple edges.
     34 
     35 The last line contains $n$ integers $t_1, t_2, \ldots, t_n$, $i$-th of them denotes desired topic number of the $i$-th blog ($1 \le t_i \le n$).
     36 
     37 Output
     38 
     39 If the solution does not exist, then write $-1$. Otherwise, output $n$ distinct integers $p_1, p_2, \ldots, p_n$ $(1 \leq p_i \leq n)$, which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 3 3
     47 1 2
     48 2 3
     49 3 1
     50 2 1 3
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 2 1 3
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 3 3
     63 1 2
     64 2 3
     65 3 1
     66 1 1 1
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 -1
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 5 3
     79 1 2
     80 2 3
     81 4 5
     82 2 1 2 2 1
     83 ```
     84 
     85 Output
     86 
     87 ```
     88 2 5 1 3 4
     89 ```
     90 
     91 Note
     92 
     93 In the first example, Johnny starts with writing blog number $2$, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number $1$, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number $3$, it has references to blogs number $1$ and $2$ so it receives the third topic.
     94 
     95 Second example: There does not exist any permutation fulfilling given conditions.
     96 
     97 Third example: First Johnny writes blog $2$, it receives the topic $1$. Then he writes blog $5$, it receives the topic $1$ too because it doesn't have reference to single already written blog $2$. Then he writes blog number $1$, it has reference to blog number $2$ with topic $1$, so it receives the topic $2$. Then he writes blog number $3$ which has reference to blog $2$, so it receives the topic $2$. Then he ends with writing blog number $4$ which has reference to blog $5$ and receives the topic $2$.