Thứ Hai, 26 tháng 11, 2012

Problem Colorful Graph

Link: http://codeforces.com/problemset/problem/246/D

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 asV(k). Let's denote the value of the neighbouring color diversity for color k as the cardinality of the setQ(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 integersai, 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


Thuật toán:

Code:

/*
    Coder : Nguyen Duc Tam
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>

using namespace std;

#define REP(i, start, end, step) for(int i = start; i < end; i += step)
#define DOWN(i, start, end, step) for(int i = start; i > end; i -= step)
#define FOR(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())
#define X first
#define Y second

#define L(x,i) ((x) << (i))
#define R(x,i) ((x) >> (i))
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a) | (b))
#define XOR(a,b) ((a) ^ (b))
#define NOT(a) (~(a))
#define SB(x,i) (OR((x), L(1, (i))))    // x | 1 << i 
#define CB(x,i) (AND((x),NOT(L(1,(i))))) // x & ~(1 << i)
#define TB(x,i) (AND((x), L(1,(i))))    // x & (1 << i) 

#define FILL(a,val) memset(a,val,sizeof(a));
#define INIT(a,l,r,val) REP(i,l,r,1) (a)[i] = val;
#define DIG(c) (int)((c) - '0')
#define CHR(c) (char)((c) + '0')
#define LOW(c) (char)((c) + 32)
#define UPP(c) (char)((c) - 32)

#define EPS 1e-7
#define OO 1000000005
#define N 100005

const int DAY[13] = {-1,31,29,31,30,31,30,31,31,30,31,30,31};


typedef pair<int,int> II;
typedef pair<II,int> D;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned char UC; 

int n,m,c[N],a,b,deg[N],ca,cb;
set<int> q[N];
int ans, number = 0;

int main()
{
    #define Off  false
    if(Off)
    {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    }       
    
    cin >> n >> m;
    REP(i,0,n,1)
        scanf("%d",&c[i]);
    
    REP(i,0,m,1)
    {
        scanf("%d %d",&a,&b);   
        ca = c[--a];
        cb = c[--b];
        if(ca != cb && q[ca].find(cb) == q[ca].end())
        {
            q[ca].insert(cb);
            q[cb].insert(ca);
            deg[ca]++;
            deg[cb]++;
        }
    }
    ans = c[0];
    REP(i,1,n,1)
    {
        if(deg[c[i]] > number)
        {
            number = deg[c[i]];
            ans = c[i]; 
        }   
        if(number == deg[c[i]] && ans > c[i])
        {
            ans = c[i]; 
        }
    }
    cout << ans << endl;
    return 0;
}



Không có nhận xét nào:

Đăng nhận xét