Codeforces Round #647 (Div. 2) D. Johnny and Contribution

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Today Johnny wants to increase his contribution. His plan assumes writing nn 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 nn different topics, numbered from 11 to nn 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−1n−1 neighbors.

For example, if already written neighbors of the current blog have topics number 11, 33, 11, 55, and 22, Johnny will choose the topic number 44 for the current blog, because topics number 11, 22 and 33 are already covered by neighbors and topic number 44 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 nn (1≤n≤5⋅105)(1≤n≤5⋅105) and mm (0≤m≤5⋅105)(0≤m≤5⋅105) — the number of blogs and references, respectively.

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

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

Output

If the solution does not exist, then write −1−1. Otherwise, output nn distinct integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n)(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

Copy

3 3
1 2
2 3
3 1
2 1 3

output

Copy

2 1 3

input

Copy

3 3
1 2
2 3
3 1
1 1 1

output

Copy

-1

input

Copy

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

output

Copy

2 5 1 3 4

Note

In the first example, Johnny starts with writing blog number 22, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number 11, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number 33, it has references to blogs number 11 and 22 so it receives the third topic.

Second example: There does not exist any permutation fulfilling given conditions.

Third example: First Johnny writes blog 22, it receives the topic 11. Then he writes blog 55, it receives the topic 11 too because it doesn't have reference to single already written blog 22. Then he writes blog number 11, it has reference to blog number 22 with topic 11, so it receives the topic 22. Then he writes blog number 33 which has reference to blog 22, so it receives the topic 22. Then he ends with writing blog number 44 which has reference to blog 55 and receives the topic 22.

题意:

给出一个无向图,再给出每个点的最终染色,对某个点染色的规则为每次从1~n中选取最小的颜色,并且这个颜色没有被其相邻点使用。让你输出一种染色顺序,如果不能就输出-1。

思路:

判断对于某个点与其相邻点是否合法,即能不能染成目标颜色。最后如果所有点都可以染色,直接按找颜色排序输出即可。

需要判断三点

1.相邻点的个数,如果都染上也没法达到当前点的颜色,直接不行。

2.相邻点有没有颜色相同的,如果有,直接不行。

3.判断从相邻点从1~(a[now] - 1)是不是都有,如果没有,直接不行。

时间复杂度O(N + E) + O(N * log(N))。

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#ifdef LOCAL
#define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n"
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#else
#define TIME 0
#endif
#define hash_ 1000000009
#define Continue(x) { x; continue; }
#define Break(x) { x; break; }
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }
ll fpow(ll a, int b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
vector<int>G[N];
int a[N];
int ans[N];
struct node
{
	int id, c;
	bool operator< (const node &oth)const
	{
		return c < oth.c;
	}
}x[N];
int vis[N];
int main()
{
#ifdef LOCAL
	freopen("E:/input.txt", "r", stdin);
#endif
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int flag = 1;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		x[i] = { i, a[i] };
	}
	sort(x + 1, x + 1 + n);
	for (int i = 1; i <= n; i++)
	{
		int cnt = 0;
		if (G[i].size() + 1 < a[i])
		{
			flag = 0;
			break;
		}
		for (auto &j : G[i])
		{
			if (a[j] == a[i])
			{
				flag = 0;
				break;
			}
			vis[a[j]] = 1;
		}
		for (int j = 1; j < a[i]; j++)
		{
			if (!vis[j])
			{
				flag = 0;
				break;
			}
		}
		for (auto &j : G[i])
		{
			vis[a[j]] = 0;
		}
	}
	if (!flag)
	{
		puts("-1");
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			cout << x[i].id << " ";
		}
		cout << endl;
	}
	return TIME;
}

猜你喜欢

转载自blog.csdn.net/weixin_43731933/article/details/106568028