CF1387B1.Village (Minimum)

提高+/省选-

通过率:0%

时间限制:0.75s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This problem is split into two tasks. In this task, you are required to find the minimum possible answer. In the task Village (Maximum) you are required to find the maximum possible answer. Each task is worth 5050 points.

There are NN houses in a certain village. A single villager lives in each of the houses. The houses are connected by roads. Each road connects two houses and is exactly 11 kilometer long. From each house it is possible to reach any other using one or several consecutive roads. In total there are N−1N-1 roads in the village.

One day all villagers decided to move to different houses — that is, after moving each house should again have a single villager living in it, but no villager should be living in the same house as before. We would like to know the smallest possible total length in kilometers of the shortest paths between the old and the new houses for all villagers.

Example village with seven houses

For example, if there are seven houses connected by roads as shown on the figure, the smallest total length is 88 km (this can be achieved by moving 1→61 \to 6, 2→42 \to 4, 3→13 \to 1, 4→24 \to 2, 5→75 \to 7, 6→36 \to 3, 7→57 \to 5).

Write a program that finds the smallest total length of the shortest paths in kilometers and an example assignment of the new houses to the villagers.

本题分为两个子任务。在本子任务中,你需要求出最小可能的答案;而在“村庄(最大值)”子任务中,你需要求出最大可能的答案。每个子任务各占 5050 分。

某村庄共有 NN 座房屋,每座房屋中恰好住着一位村民。房屋之间由道路相连。每条道路连接两座房屋,且长度恰好为 11 千米。从任意一座房屋出发,均可经由一条或多条连续的道路到达其余任意一座房屋。整个村庄中共有 N−1N-1 条道路。

某日,所有村民决定全部搬入不同的房屋——即搬迁完成后,每座房屋仍恰好住着一位村民,但没有任何一位村民留在自己原来的房屋中。我们希望知道:所有村民从原住所到新住所的最短路径长度(单位:千米)之和的最小可能值。

含七座房屋的示例村庄

例如,若村庄中有七座房屋,其道路连接方式如图所示,则最短路径长度总和的最小值为 88 千米(可通过如下搬迁方案实现:1→61 \to 6、2→42 \to 4、3→13 \to 1、4→24 \to 2、5→75 \to 7、6→36 \to 3、7→57 \to 5)。

请编写一个程序,计算最短路径长度总和(单位:千米)的最小值,并给出一个满足该最小总和的村民新住所分配方案。

输入格式

The first line contains an integer NN (1<N≤1051 \lt N \le 10^5). Houses are numbered by consecutive integers 1,2,…,N1, 2, \ldots, N.

Then N−1N-1 lines follow that describe the roads. Each line contains two integers aa and bb (1≤a,b≤N1 \le a, b \le N, a≠ba \neq b) denoting that there is a road connecting houses aa and bb.

第一行包含一个整数 NN(1<N≤1051 \lt N \le 10^5)。房屋编号为连续的整数 1,2,…,N1, 2, \ldots, N。

接下来是 N−1N-1 行,描述道路。每行包含两个整数 aa 和 bb(1≤a,b≤N1 \le a, b \le N,a≠ba \neq b),表示房屋 aa 和 bb 之间有一条道路相连。

输出格式

In the first line output the smallest total length of the shortest paths in kilometers.

In the second line describe one valid assignment of the new houses with the smallest total length: NN space-separated distinct integers v1,v2,…,vNv_1, v_2, \ldots, v_N. For each ii, viv_i is the house number where the villager from the house ii should move (vi≠iv_i \neq i). If there are several valid assignments, output any of those.

第一行输出最短路径总长度的最小值(单位:千米)。

第二行描述一种使总长度最小的有效新住房分配方案:NN 个用空格分隔的互不相同的整数 v1,v2,…,vNv_1, v_2, \ldots, v_N。对每个 ii,viv_i 表示原住在第 ii 号房屋的村民应搬迁至的房屋编号(要求 vi≠iv_i \neq i)。若存在多种有效分配方案,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    1 2
    2 3
    3 4

    输出#1

    4
    2 1 4 3
  • 输入#2

    7
    4 2
    5 7
    3 4
    6 3
    1 3
    4 5

    输出#2

    8
    3 4 6 2 7 1 5

输入解题思路,AI测评打分。不知道怎么写?

首页