تم الحل ✓
categoryعلوم الحاسوب
schoolبكالوريوس
event_available2026-07-15
السؤال
Transcribed Image Text:
M mandeep singh say
SS
1
2
Task
Codility
https://app.codility.com/c/run/KEBREJ-6QF/
A network consisting of N cities and N-1 roads connecting them is given. Cities are labeled with
distinct integers within the range [0..N-1].
Roads connect cities in such a way that each pair of distinct cities is connected either by a direct
road or along a path consisting of direct roads. There is exactly one way to reach any city from any
other city. In other words, cities and direct roads form a tree.
For example, consider the following network consisting of ten cities and nine roads:
Jack lives in city number 0. He wants to plan a trip and visit as many cities as possible.
Nonetheless, he does not want to visit any city more than once and he can only move along direct
roads. The trip may finish in any city.
There is one issue. Each city labeled with an odd number (1,3,5,...) requires a special ticket in
order to visit it. The tickets are already sold out, but luckily Jack has already bought one such
ticket, so he can visit at most one odd-numbered city.
Jack wonders how many cities (including the starting one) he can visit.
Write a function:
class Solution (public int solution(int[] T); }
that, given a non-empty array T consisting of N Integers describing a network of N cities and N-1
roads, returns the maximum number of cities that Jack can visit.
Array T describes a network of cities as follows:
If TIPI-Q and P 0, then there is a direct road between cities P and Q.
Examples:
1. Given T = [0,9,0,2,6,8, 0, 8, 3, 01
e
6
田
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.