您的位置:首页 > 其它

【codeforces 246D Colorful Graph 】+ set

2016-12-07 20:29 274 查看
D. Colorful Graph

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ n; ai ≠ 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

题意~给出N个顶点的颜色~M条边~问连接其他颜色最多的顶点~解有多个时~输出字典序最小的点~

set~自带去重~在合适不过~~

AC代码:

#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 10;
set <int> s[MAXN];
int ma[MAXN];
int main()
{
int x,y,N,M;
scanf("%d %d",&N,&M);
for(int i = 1 ; i <= N; i++)
scanf("%d",&ma[i]);
while(M--){
scanf("%d %d",&x,&y);
if(ma[x] == ma[y]) continue;
s[ma[x]].insert(ma[y]);
s[ma[y]].insert(ma[x]);
}
int cut = ma[1];
for(int i = 2 ; i <= N ; i++){
if(s[cut].size() < s[ma[i]].size())
cut = ma[i];
else if(s[cut].size() == s[ma[i]].size() && cut > ma[i])
cut = ma[i];
}
printf("%d\n",cut);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces