Elevated Profits

시간 제한2초메모리 제한1024 MB

요약
트리에서 R에서 시작해 모든 도시를 방문하는 순서를 정할 때, 1부터 N까지의 가중치와 인기 지수의 곱의 합이 최대가 되도록 한다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 트리, DFS
정답자
아직 제출이 없습니다

문제

Marina, a digital influencer who loves traveling the world, is embarking on a promotional tour for a women’s clothing brand called W2M (From Woman to Woman Marina). Marina’s journey takes her through NN cities in Latin America, each with its unique charm, and identified with a distinct integer from 11 to NN, called its popularity index.

To facilitate Marina’s travels, W2M has provided her with N−1N - 1 transfers, connecting pairs of cities in a way that guarantees accessibility to all NN cities. Marina can traverse these connections as many times as she pleases.

Marina’s mission is to showcase the brand’s dresses in each of the NN cities, with a twist. Each time she visits a city for the first time, she must select a dress she hasn’t worn before and capture the city’s essence in a social media post. Every new picture she shares attracts followers, creating anticipation for the next one. The anticipation value for her first picture is 11, and it increments by 11 for each subsequent picture.

Marina can revisit any city as often as desired, but a new picture must only be posted on her initial visit to a city. Her goal is to maximize the profit of her tour, which is computed as the sum of the anticipation value of each picture multiplied by the popularity index of the city where the picture is taken. More precisely, let p_ip\_i be the popularity index of the city where the ii-th picture is taken. With this information, the profit can be calculated as

∑_i=1Ni×p_i=1×_1+2×p_2+⋯+N×p_N\sum\_{i=1}^{N}{i \times p\_i} = 1 \times \_1 + 2 \times p\_2 + \cdots + N \times p\_N

Now, Marina seeks your assistance. Given that the tour has to start in city p_1=Rp\_1 = R, your task is to help Marina determine the maximum profit she can achieve by strategically planning the order of her city visits.

입력

The first line contains two integers NN (1≤N≤3×1051 ≤ N ≤ 3 \times 10^5) and RR (1≤R≤N1 ≤ R ≤ N), indicating respectively the number of cities and the initial city of the tour.

Each of the next N−1N - 1 lines contains two integers UU and VV (1≤U,V≤N1 ≤ U, V ≤ N and U≠VU \ne V), indicating that there is a transfer between cities UU and VV. It is guaranteed that it is possible to reach every city by using the transfers.

출력

Output a single line with an integer indicating the maximum profit Marina can achieve on her promotional tour.

예제2

  1. 예제 1

    입력
    7 3
    3 5
    3 7
    5 1
    5 4
    7 2
    7 6
    
    예상 출력
    121
    
  2. 예제 2

    입력
    1 1
    
    예상 출력
    1