Вы находитесь на странице: 1из 7

15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

 
  01h : 19m
Codenation - Campus Pool -15th October
to test end

N

 Long Journey

There is a country of n cities, numbered from 1 to n. The cities are connected by n - 1


1 undirected roads, such that each city is reachable from all the others. You want to have a
journey, which starts in some city and ends in some city. You can't visit any city twice. You
2 can move only by roads. You want to visit as many cities as it's possible. A journey can be
represented as an array of integers, denoting the cities of the journey in the order of
3 visiting. If there are several longest journeys, find the lexicographically minimal. 

Function Description
4
Complete the function findJourney in the editor below. The function must return an array
of integers, denoting the cities you will visit.

findJourney has the following parameter(s):

    n:  the number of cities in the country

    u:  an array of n - 1 integers representing the first endpoint of the ith road

    v:  an array of n - 1 integers representing the second endpoint of the ith road

Constraints

2 ≤ n ≤ 105
1 ≤ u[i], v[i] ≤ 105 , where 0 ≤ i < n - 1

Input Format For Custom Testing

Locked stub code reads input from stdin and passes it to the function.

The first line contains an integer n.


The next line contains an integer n - 1.
ith (starting from 0) of the next n - 1 lines contains an integer u[i].
The next line contains an integer n - 1.
ith (starting from 0) of the next n - 1 lines contains an integer v[i].

Sample Case 0

Sample Input 0

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 1/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

4
3
2
1
1
3
1
3
4

Sample Output 0

2 1 3

Explanation 0

n = 4
u = [2, 1, 1]
v = [1, 3, 4]

There are 6 longest journeys: [2, 1, 3], [2, 1, 4], [3, 1, 2], [3, 1, 4], [4, 1, 2], [4, 1, 3].  [2, 1, 3]
is the lexicographically smallest.

Sample Case 1

Sample Input 1

5
4
1
2
3
4
4
2
3
4
5

Sample Output 1

1 2 3 4 5

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 2/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

Explanation 1

n = 5
u = [1, 2, 3, 4]
v = [2, 3, 4, 5]

YOUR ANSWER

We recommend you take a quick tour of our editor before you proceed. The timer ✖
will pause up to 90 seconds for the tour.   Start tour

Original code C++  ⚙

1 ▸ #include ↔
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7
8
9 ▾ /*
10 * Complete the 'findJourney' function below.
11 *
12 * The function is expected to return an INTEGER_ARRAY.
13 * The function accepts following parameters:
14 * 1. INTEGER n
15 * 2. INTEGER_ARRAY u
16 * 3. INTEGER_ARRAY v
17 */
18
19 ▾ vector<int> findJourney(int n, vector<int> u, vector<int> v)
{vector<int>t;return t;
20
21 }
22
23
24 int main()
25 ▾ {
26    ofstream fout(getenv("OUTPUT_PATH"));

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 3/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

27
28    string n_temp;
29    getline(cin, n_temp);
30
31    int n = stoi(ltrim(rtrim(n_temp)));
32
33    string u_count_temp;
34    getline(cin, u_count_temp);
35
36    int u_count = stoi(ltrim(rtrim(u_count_temp)));
37
38    vector<int> u(u_count);
39
40 ▾    for (int i = 0; i < u_count; i++) {
41        string u_item_temp;
42        getline(cin, u_item_temp);
43
44        int u_item = stoi(ltrim(rtrim(u_item_temp)));
45
46 ▾        u[i] = u_item;
47   }
48
49    string v_count_temp;
50    getline(cin, v_count_temp);
51
52    int v_count = stoi(ltrim(rtrim(v_count_temp)));
53
54    vector<int> v(v_count);
55
56 ▾    for (int i = 0; i < v_count; i++) {
57        string v_item_temp;
58        getline(cin, v_item_temp);
59
60        int v_item = stoi(ltrim(rtrim(v_item_temp)));
61
62 ▾        v[i] = v_item;
63   }
64
65    vector<int> result = findJourney(n, u, v);
66
67 ▾    for (int i = 0; i < result.size(); i++) {
68 ▾        fout << result[i];
69
70 ▾        if (i != result.size() - 1) {
71            fout << "\n";
72       }
73   }
74

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 4/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

75    fout << "\n";


76
77    fout.close();
78
79    return 0;
80 }
81
82 ▾ string ltrim(const string &str) {
83    string s(str);
84
85    s.erase(
86        s.begin(),
87        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>
(isspace)))
88   );
89
90    return s;
91 }
92
93 ▾ string rtrim(const string &str) {
94    string s(str);
95
96    s.erase(
97        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>
(isspace))).base(),
98        s.end()
99   );
100
101    return s;
102 }
103

Line: 19 Col: 85

Test against custom input Run Code Submit code & Continue

(You can submit any number of times)

 Download sample test cases The input/output files have Unix line endings. Do not use
Notepad to edit them on windows.

Status: No test cases passed.


 Tip: Debug your code against custom input

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 5/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

Testcase 1: Wrong Answer


Input [ Download]

4
3
2
1
1
3
1
3
4

Your Output

Expected Output [ Download]

2
1
3

Testcase 2: Wrong Answer


Input [ Download]

5
4
1
2
3
4
4
2
3
4
5

Your Output

Expected Output [ Download]

1
2
3
4
5

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 6/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

About Privacy Policy Terms of Service

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 7/7

Вам также может понравиться