C

来源:互联网 发布:2016淘宝刷不了单 编辑:程序博客网 时间:2024/06/17 01:21

Colorful Graph CodeForces - 246D
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.

Example
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
题目大意:给你一张图,图上的每个顶点都有颜色,问内种颜色是相邻最多种类颜色的颜色。(说起来有点绕hh)
解题思路:相邻不同种颜色需要去重第一时间想到的就是set,一开始用set将下标用顶点来算了结果光荣wa了要注意一种颜色是会出现在不同顶点上的还有需要注意0的情况在都是0的情况下应该输出颜色最小的(特别要注意的:输出具有多个最优颜色选择数字最小的颜色输出)特别收获发现当set的size()为空的时候返回值不是数字上网搜了下是NULL。
ac代码:

#include <iostream>#include <cstring>#include <set>#include<stdio.h>using namespace std;#define maxn (int)1e5+10int n,m;int c[maxn];set<int>st[maxn];int main(){    scanf("%d%d",&n,&m);    int ans=100000;    for (int i=1;i<=n;i++)    {        scanf("%d",&c[i]);        ans=min(ans,c[i]);    }    for (int i=1;i<=m;i++)    {        int a,b;        scanf("%d%d",&a,&b);        if (c[a]!=c[b])//相同颜色不需要放入        {            st[c[a]].insert(c[b]);            st[c[b]].insert(c[a]);        }    }    for (int i=1;i<=n;i++)        if (st[i].size()>st[ans].size())            ans=i;    printf("%d\n",ans);    return 0;}