Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution

Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution

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.

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.

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.

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?

Input

The first line contains two integers n (1≤n≤5⋅105) and m (0≤m≤5⋅105) — the number of blogs and references, respectively.

Each of the following m lines contains two integers a and b (a≠b; 1≤a,b≤n), which mean that there is a reference between blogs a and b. It’s guaranteed that the graph doesn’t contain multiple edges.

The last line contains n integers t1,t2,…,tn, i-th of them denotes desired topic number of the i-th blog (1≤ti≤n).

Output

If the solution does not exist, then write −1. Otherwise, output n distinct integers p1,p2,…,pn (1≤pi≤n), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.

Examples

input

3 3
1 2
2 3
3 1
2 1 3

output

2 1 3

input

3 3
1 2
2 3
3 1
1 1 1

output

-1

input

5 3
1 2
2 3
4 5
2 1 2 2 1

output

2 5 1 3 4

這題題意乍一看很晦澀,但是你一畫圖就會發現這其實是一個簡單的上色問題,就是讓你判斷着色合不合理即可~
我們把文章標題當作顏色,對每一種顏色,記錄需要這種顏色的博客編號~
然後遍歷每一種顏色,判斷需要該顏色的博客編號能否標記上這種顏色即可,我們用 ansans 數組記錄每個博客實際的顏色,用 lastlast 數組記錄指向該博客的顏色,用 whilewhile 循環找到該博客能上的最小顏色編號,若 ans[u]ans[u] 能染上當前顏色則錄入答案中,若不能則直接輸出 -1 退出,AC代碼如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+5;
vector<int>g[N],in[N];
int last[N],ans[N];
main(){
    int n,m,u,v,c;
    cin>>n>>m;
    while(m--){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1;i<=n;i++){
        cin>>c;
        in[c].push_back(i);
        if(c>n){
            puts("-1");
            exit(0);
        }
    }
    vector<int>result;
    for(int i=1;i<=n;i++){
        for(auto u:in[i]){
            for(auto v:g[u])
                last[ans[v]]=u;
            ans[u]=1;
            while(last[ans[u]]==u)
                ++ans[u];
            if(ans[u]!=i){
                puts("-1");
                exit(0);
            }
            result.push_back(u);
        }
    }
    for(auto i:result)
        cout<<i<<" ";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章