Codeforces 246D Colorful Graph 【DFS】

题目链接:Codeforces 246D Colorful Graph

D. Colorful Graph
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 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.

Examples
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个点,每个点有一种颜色,可以颜色相同。我们定义颜色k的系数为——所有颜色为k的节点周围的颜色种类之和。问你最小的k。

暴力,用个set记录下。

AC代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 1e5 + 1;
vector<int> G[MAXN], color[MAXN];
set<int> cnt[MAXN];
int c[MAXN];
bool vis[MAXN];
void DFS(int u, int id) {
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        cnt[id].insert(c[v]);
    }
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 1; i < MAXN; i++) {
            G[i].clear(); cnt[i].clear(); color[i].clear(); vis[i] = false;
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &c[i]); vis[c[i]] = true;
            color[c[i]].push_back(i);
        }
        for(int i = 1; i <= m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        int ans = 0;
        for(int i = 1; i < MAXN; i++) {
            if(vis[i] == false) continue;
            for(int j = 0; j < color[i].size(); j++) {
                int s = color[i][j];
                DFS(s, i); cnt[i].insert(c[s]);
            }
            ans = max(ans, (int)cnt[i].size()-1);
            //cout << (int)cnt[i].size() - 1 << endl;
        }
        for(int i = 1; i < MAXN; i++) {
            if(vis[i] == false) continue;
            //cout <<(int)cnt[i].size()-1 << endl;
            if((int)cnt[i].size()-1 == ans) {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}

你可能感兴趣的:(DFS,&&,BFS,codeforces)