zoukankan      html  css  js  c++  java
  • CODEFROCES 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

    题目大意为n个点,上有颜色,求某一个颜色的number,使得和该颜色相连的不同颜色最多

    题目巨难看懂(英文水平渣渣)

    有陷阱,比如每一个颜色都没有相邻的颜色

    第一次使用map……

    #include <cstdio> 
    #include <iostream> 
    #include <cstring> 
    #include <map> 
        
    #define MAXN 100005 
    std::map<int,bool> adj[MAXN]; 
        
    int x,y,n,m,i,ans,maxcol,k; 
    int cnt[MAXN],col[MAXN]; 
        
    int main() 
    { 
        while(scanf("%d%d",&n,&m)!=EOF) 
        { 
            memset(cnt,255,sizeof(cnt)); 
            for(i=1,maxcol=0;i<=n;++i) 
            { 
                scanf("%d",&col[i]); 
                cnt[col[i]]=0; 
                if(maxcol<col[i]) maxcol=col[i]; 
            } 
            for(i=1;i<=maxcol;++i) adj[i].clear(); 
            for(i=1;i<=m;++i) 
            { 
                scanf("%d%d",&x,&y); 
                if(col[x]!=col[y] && !adj[col[x]][col[y]]) 
                { 
                    cnt[col[x]]++; 
                    cnt[col[y]]++; 
                    adj[col[x]][col[y]]=adj[col[y]][col[x]]=1; 
                } 
            } 
            for(i=1,ans=-1,k=0;i<=maxcol;++i) 
            { 
                if(cnt[i]>ans) 
                { 
                    ans=cnt[i]; 
                    k=i; 
                } 
            } 
            printf("%d\n",k); 
        } 
        return 0; 
    }
    View Code


     

  • 相关阅读:
    1051 高度检查器
    Word+Excel 问题及解决
    Python——面向对象、绑定对象、组合
    Python——异常处理
    Python——包
    Python——模块
    Python——序列化模块
    Python——collections模块、time模块、random模块、os模块、sys模块
    Python——re模块
    Python——递归、二分查找算法
  • 原文地址:https://www.cnblogs.com/oyking/p/3116602.html
Copyright © 2011-2022 走看看