luogu#P8509. 如何得到 npy

    ID: 7173 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>搜索图论洛谷原创Special JudgeO2优化洛谷月赛

如何得到 npy

Background

As the most charming person in the grade, Steve always finds many things for himself, including but not limited to an npy. Steve has a crush on Ada and tries to get closer to her, but Ada is not very willing.

Problem Description

Hint: You can read the formal statement at the end of the description.

There are nn classrooms on Steve's campus, numbered from 11 to nn, connected by n1n-1 corridors. In other words, the classrooms and corridors form a tree. Each corridor has a length wiw_i, and the time to pass through this corridor equals its length.

Steve likes to wander around the campus. Of course, he hopes that in the end he can reach either his own classroom ss or Ada's classroom tt. However, the school is too complicated, and Steve does not want to walk back along the way he came, so he comes up with the following plan:

For every classroom (except Steve's and Ada's classrooms, where there should be no signs around these two classrooms), place a sign on one edge adjacent to it. Every time he arrives at this classroom, he leaves through the edge with the sign.

Steve may appear in any classroom in the school, so on one hand, Steve needs to make sure that from every classroom he can follow the signs back to either his classroom or Ada's classroom. On the other hand, he hopes the sum of times from all classrooms in the school to the destination is as small as possible.

Since Steve is going to look for Ada again, please help him complete this task.

Formal statement

Given an undirected tree with nn nodes and two key nodes s,ts,t, orient all edges such that:

  • Each edge is either directed or deleted.
  • For every node (except s,ts,t), the out-degree is exactly 11, and for s,ts,t the out-degree is 00.
  • From every node, it is possible to reach ss or tt by following directed edges.

Find the minimum possible value of the sum of distances from every node to ss or tt.

Input Format

The first line contains three positive integers nn, ss, tt.

The next n1n-1 lines each contain three positive integers uiu_i, viv_i, wiw_i, representing a tree edge (ui,vi)(u_i,v_i) with weight wiw_i.

Output Format

Output a single positive integer on the first line, representing the minimum total weight sum.

On the next line, output a string SS, where:

  • Si=0S_i=\texttt{0} means no sign is placed on either end of the ii-th edge.
  • Si=1S_i=\texttt{1} means a sign is placed at uiu_i of the ii-th edge.
  • Si=2S_i=\texttt{2} means a sign is placed at viv_i of the ii-th edge.

This problem is judged with a custom checker. If multiple solutions exist, output any one.

5 1 5
1 2 1
2 3 1
3 4 1
4 5 1
4
2201
13 4 5
1 3 3
2 3 2
6 4 5
7 4 10
4 8 2
11 8 3
5 13 6
8 13 5
8 3 4
10 5 8
12 10 3
13 9 9
85
111121202112
见下发文件 corridor/corridor3.in
见下发文件 corridor/corridor3.ans
见下发文件 corridor/corridor4.in
见下发文件 corridor/corridor4.ans

Hint

Explanation for Sample 1

2011 is also a valid answer, but 2211, 1102, etc. are not.

Explanation for Sample 2

The figure below shows an optimal solution for the sample (the edge (8,13)(8,13) is not drawn):

Explanation for Sample 3

This sample satisfies the constraints of Subtask 2.

Explanation for Sample 4

This sample satisfies the constraints of Subtask 5.


The provided files also include checker.cpp, which can be used to determine whether an output is valid. To use it, first compile it (assume the binary is checker (Linux/MacOS) or checker.exe (Windows)), then run the following command in the terminal:

./checker in.txt out.txt ans.txt

If you are using Windows and cannot run the above command, please try:

checker.exe in.txt out.txt ans.txt

Here, in.txt, out.txt, and ans.txt are the input file, the contestant's output, and the standard answer, respectively, placed in the same directory.

The result may be one of the following:

  • ok: the result is correct and you can get full score.
  • wrong answer: the first line is incorrect.
  • points 0.60: the first line is correct, but the second line is incorrect.

For all non-full-score cases, there will be an additional message with the following meanings:

  • A x y: the first line is incorrect; the standard answer is xx, and the contestant's answer is yy.
  • B: the length of the second line does not meet the requirement.
  • C: the second line contains an invalid character.
  • D: the construction on the second line does not satisfy the degree constraints in the statement.
  • E x y: the construction on the second line produces an answer of yy, while the actual answer is xx.

This checker may differ from the checker used in the final judging.

Note that in the output samples provided with the files, only the optimal value is shown, without a construction.

Constraints

This problem uses bundled testdata. For all testdata, 3n3×1053\le n\le 3\times 10^5, 1wi2×1081\le w_i\le2\times 10^8, 1s,tn1\le s,t\le n, and sts\neq t.

$$\def\arraystretch{1.5} \begin{array}{c|c|c||c|c}\hline \bf Subtask & \bf Points & \bf Dependencies & n\le & \bf Special\ Properties \\ \hline \hline 1 & 10 & / & 10 & /\\\hline 2 & 15 & 1 & 18 & /\\\hline 3 & 15 & / & / & v_i=u_i+1\\\hline 4 & 10 & / & / & u_i=1\\\hline 5 & 20 & / & / & There\ exists\ an\ edge\ (s,t)\\\hline 6 & 30 & 2\sim5 &/ & / \end{array}$$

If you compute the correct optimal value but your construction is wrong, you will get 60%60\% of the score for that test point. Note that even if you only implement the first part, you should still output any non-empty string on the second line, otherwise you may get no score.

In this problem, the dependencies mean: the score ratio of a subtask cannot exceed the score ratio of the subtasks it depends on. For example, if a contestant gets 60%60\% of Subtask 11, then their Subtask 22 will not exceed the corresponding 60%60\% score, i.e. not more than 99 points.

The answer can be very large, so please pay attention to the data type you use.


Until graduation, Steve still did not win Ada over. What a sad story. :-(

Translated by ChatGPT 5