【CodeForces - 246D】Colorful Graph

版权声明:如果有什么问题,欢迎大家指出,希望能和大家一起讨论、共同进步。 https://blog.csdn.net/QQ_774682/article/details/83863225

You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph's vertices numbered with integers from 1 to n. Each vertex of the graph has a color. The color of the i-th vertex is an integer ci.

Let's consider all vertices of the graph, that are painted some color k. Let's denote a set of such as V(k). Let's denote the value of the neighbouring color diversity for color k as the cardinality of the set Q(k) = {cu :  cu ≠ k and there is vertex v belonging to set V(k) such that nodes v and u are connected by an edge of the graph}.

Your task is to find such color k, which makes the cardinality of set Q(k) maximum. In other words, you want to find the color that has the most diverse neighbours. Please note, that you want to find such color k, that the graph has at least one vertex with such color.

Input

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of vertices end edges of the graph, correspondingly. The second line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105) — the colors of the graph vertices. The numbers on the line are separated by spaces.

Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi) — the numbers of the vertices, connected by the i-th edge.

It is guaranteed that the given graph has no self-loops or multiple edges.

Output

Print the number of the color which has the set of neighbours with the maximum cardinality. It there are multiple optimal colors, print the color with the minimum number. Please note, that you want to find such color, that the graph has at least one vertex with such color.

Examples

Input

6 6
1 1 2 3 5 8
1 2
3 2
1 4
4 3
4 5
4 6

Output

3

Input

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

Output

2

思路:

set类型的数组,来存每个点周围有几种不同的颜色,最后遍历,找周围颜色最多的标号最小的,输出标号。

ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
int a[100100];
set<int> s1[100101];
int v[100100];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	int maxx=-1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		v[a[i]]=1;
		maxx=max(maxx,a[i]);//找到最大元素的编号
	}
	for(int i=0;i<m;i++)
	{
		int aa,b;
		scanf("%d%d",&aa,&b);
		if(a[aa]!=a[b])
		{
			s1[a[aa]].insert(a[b]);
			s1[a[b]].insert(a[aa]);
		}
		
	}
	int ans=-1,ot;//ans必须小于0,因为也可能有结果为0
	for(int i=1;i<=maxx;i++)
	{
		int t=s1[i].size();
		if(v[i]&&t>ans)
		{
			ans=t;
			ot=i;
		}
	}
	printf("%d\n",ot);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/QQ_774682/article/details/83863225
今日推荐