Codeforces--246D--Colorful Graph(STL set)(好题)


Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

Description

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.

Sample Input

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

Source

题意:一棵树有n个节点,每个节点都有一种颜色,构成了一颗颜色树,求某一节点附近不同颜色最多的 节点的 颜色编号,有点绕口,反正就是求一种颜色编号,如果有多个节点满足条件,输出最小的颜色编号
直接根据颜色,用set建图,set自动去重会节省很多事,set的大小就可以表示附近颜色的数量
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
#define MAXN 1000000
int color[MAXN],vis[MAXN],n,m;
set<int>G[MAXN];
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<MAXN;i++)
		G[i].clear();
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			color[i]=x;
			vis[x]=1;
		}
		for(int i=0;i<m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			if(color[x]!=color[y])
			{
				G[color[x]].insert(color[y]);
				G[color[y]].insert(color[x]);
			}
		}
		int ans=0,count=0;
		for(int i=0;i<MAXN;i++)
		{
			if(vis[i]&&G[i].size()>count)
			{
				count=G[i].size();
				ans=i;
			}
		}
		if(count==0)
		{
			for(int i=0;i<MAXN;i++)
			{
				if(vis[i])
				{
					ans=i;
					break;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值