zoukankan      html  css  js  c++  java
  • D. Colorful Graph(坑爹题)

    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 ≤ 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 test(s)
    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
    这题各种坑……其中一组易错数据
    3 1
    13 13 4
    1 2
    ans = 4
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <set>
     5 #include <map>
     6 #include <vector>
     7 #include <queue>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <cmath>
    11 using namespace std;
    12 
    13 const int maxn = 100003;
    14 map<int, bool> adj[maxn];
    15 int cnt[maxn]; //集合基数
    16 int v[maxn]; //vertex的颜色
    17 int n, m;
    18 
    19 int main()
    20 {
    21     while(scanf("%d %d", &n, &m) != EOF)
    22     {
    23         memset(cnt, -1, sizeof(cnt));
    24         int maxC = 0; //颜色的最大标号
    25         for(int i = 1; i <= n; i++)
    26         {
    27             scanf("%d", &v[i]);
    28             cnt[v[i]] = 0;
    29             if(maxC < v[i]) maxC = v[i];
    30         }
    31         for(int i = 1; i <= maxC; i++) adj[i].clear();
    32         int a, b;
    33         while(m--)
    34         {
    35             scanf("%d %d", &a, &b);
    36             if(v[a] != v[b] && !adj[v[a]][v[b]])
    37             {
    38                 cnt[v[a]]++;
    39                 cnt[v[b]]++;
    40                 adj[v[a]][v[b]] = adj[v[b]][v[a]] = true;
    41             }
    42         }
    43         int num = 0, id = maxC + 1;
    44         for(int i = 1; i <= maxC; i++)
    45         {
    46             if(num < cnt[i])
    47             {
    48                 num = cnt[i];
    49                 id = i;
    50             }
    51             else if(num == cnt[i] && id > i) id = i;
    52         }
    53         //for(int i = 1; i <= maxC; i++) cout << i << ':' << cnt[i] << endl;
    54         printf("%d\n", id);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    磁盘冗余阵列之RAID 5
    磁盘冗余阵列之RAID10
    Linux常见的命令与vi的介绍
    通过挂在系统光盘搭建本地yum
    Windows 2008 系统安装
    在windows主机中,利用XSHELL生成“密钥”进行虚拟机与物理机的传输
    在VMware下进行的使用ssh服务管理远程主机
    Linux中的快捷方式
    Linux中常用命令
    Linux中vi命令的详细总结
  • 原文地址:https://www.cnblogs.com/cszlg/p/2966673.html
Copyright © 2011-2022 走看看