zoukankan      html  css  js  c++  java
  • Codeforces 246D. 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 theneighbouring 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
    


    找到相邻颜色最多的颜色。。。STL set的应用

    注意一堆0的情况


    #include <iostream>
    #include <cstring>
    #include <set>
    
    using namespace std;
    
    int n,m;
    int c[111111];
    set<int>st[111111];
    
    
    int main()
    {
        cin>>n>>m;
        int ans=100000;
        for (int i=1;i<=n;i++)
        {
            cin>>c[i];
            ans=min(ans,c[i]);
        }
        for (int i=1;i<=m;i++)
        {
            int a,b;
            cin>>a>>b;
            if (c[a]!=c[b])
            {
                st[c[a]].insert(c[b]);
                st[c[b]].insert(c[a]);
            }
        }
        for (int i=1;i<=100000;i++)
        {
            if ( ans==-1||st[i].size()>st[ans].size())
            {
                ans=i;
            }
        }
        cout<<ans<<endl;
        return 0;
    }




  • 相关阅读:
    最短路 dijkstra 优先队列
    树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)
    c++优先队列(堆)
    团体程序设计天梯赛 L3-012. 水果忍者
    团体程序设计天梯赛 L2-028. 秀恩爱分得快
    团体程序设计天梯赛 L1-049. 天梯赛座位分配(测试数据+不同方法)
    奇偶校验——设计可以检验错误位置的方法
    奇偶校验的错误概率
    Fibonacci数列时间复杂度之美妙
    团体程序设计天梯赛 L3-016. 二叉搜索树的结构
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038455.html
Copyright © 2011-2022 走看看