Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

UPCOMING CHALLENGES:

CURRENT CHALLENGES:

May the 4th Challenge

PAST CHALLENGES

The Great Code Off 2021

The Doge 2021

The Matrix 2021

The OLX Group challenge

Silver 2020

Palladium 2020

Rhodium 2019

Ruthenium 2019

Technetium 2019

Molybdenum 2019

Niobium 2019

Zirconium 2019

Yttrium 2019

Strontium 2019

Rubidium 2018

Arsenicum 2018

Krypton 2018

Bromum 2018

Future Mobility

Grand Challenge

Digital Gold

Selenium 2018

Germanium 2018

Gallium 2018

Zinc 2018

Cuprum 2018

Cutting Complexity

Nickel 2018

Cobaltum 2018

Ferrum 2018

Manganum 2017

Chromium 2017

Vanadium 2016

Titanium 2016

Scandium 2016

Calcium 2015

Kalium 2015

Argon 2015

Chlorum 2014

Sulphur 2014

Phosphorus 2014

Silicium 2014

Aluminium 2014

Magnesium 2014

Natrium 2014

Neon 2014

Fluorum 2014

Oxygenium 2014

Nitrogenium 2013

Carbo 2013

Boron 2013

Beryllium 2013

Lithium 2013

Helium 2013

Hydrogenium 2013

Omega 2013

Psi 2012

Chi 2012

Phi 2012

Upsilon 2012

Tau 2012

Sigma 2012

Rho 2012

Pi 2012

Omicron 2012

Xi 2012

Nu 2011

Mu 2011

Lambda 2011

Kappa 2011

Iota 2011

Theta 2011

Eta 2011

Zeta 2011

Epsilon 2011

Delta 2011

Gamma 2011

Beta 2010

Alpha 2010

A network consisting of N cities and N − 1 roads is given. Each city is labeled with a distinct integer from 0 to N − 1.

Roads connect cities in such a way that each distinct pair of cities is connected either by a direct road or through a path consisting of direct roads. There is exactly one way to reach any city from any other city.

Each city also has its own *attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

You are planning a trip to visit some of the most attractive cities. You want to select cities to visit based on the following requirements:

- At most K cities can be included in the trip plan.
- It must be possible to travel among the cities included in the trip plan without having to travel through cities that have been excluded from the trip plan.
- None of the cities included in the trip plan can be less attractive than any of the excluded cities. City attractiveness levels do not have to be unique, though, so it is permissible to visit only a subset of cities that are equally attractive.

The goal is to maximize the number of cities selected while satisfying the above requirements.

The network of cities is described using arrays C and D, each of length N. Array C describes a network of cities as follows: if C[P] = Q and P ≠ Q, then there is a direct road between cities P and Q. Array D describes attractiveness of the cities: D[P] is the attractiveness level of city P.

For example, consider the following network consisting of seven cities (each circle represents a city: the city label appears inside the circle and its attractiveness level outside the circle):

If K = 2, we can select a maximum number of two cities: either 2 and 0 or 2 and 4. In both cases, the attractiveness levels of the selected cities are greater than or equal to 6 and the attractiveness levels of the excluded cities are less than or equal to 6.

If, however, K = 5, the maximum number of cities we can select according to the rules above is four: we must select cities 2, 0, 4 and 5.

Write a function:

class Solution { public int solution(int K, int[] C, int[] D); }

that, given the integer K and non-empty arrays C and D of length N describing a network of cities and their attractiveness, returns the maximum number of cities that can be included in a valid trip plan.

For example, given arrays C and D describing the network above:

and K = 2 the function should return 2, as explained above. Similarly, given the same network but with K = 5, the function should return 4, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.