当前位置: 首页 > 工具软件 > thanks > 使用案例 >

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

邰德业
2023-12-01

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

这题题意乍一看很晦涩,但是你一画图就会发现这其实是一个简单的上色问题,就是让你判断着色合不合理即可~
我们把文章标题当作颜色,对每一种颜色,记录需要这种颜色的博客编号~
然后遍历每一种颜色,判断需要该颜色的博客编号能否标记上这种颜色即可,我们用 a n s ans ans 数组记录每个博客实际的颜色,用 l a s t last last 数组记录指向该博客的颜色,用 w h i l e while while 循环找到该博客能上的最小颜色编号,若 a n s [ u ] 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<<" ";
}
 类似资料: