luogu#P7895. 『JROI-3』删树

    ID: 7169 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2021交互题Special JudgeO2优化分治树论虚树洛谷月赛

『JROI-3』删树

Background

The testdata for this problem has been strengthened. If you got AC during the contest, it is recommended to submit again to make sure your solution is correct.

Do not misread the statement!

——command_block “Pre-exam Tips”

In 2021, you participated on Luogu in a contest called EZEC Round 6, which had a tree-building problem. You thought it was very easy and solved it casually. (So if you have not done the problem in the link, go do it now!!!)

Now you are participating in the JROI-3 monthly contest. You feel that building trees is too easy, and you want to delete the tree instead, so the kind problem setter gives you a chance. However, before deleting the tree, djy wants to know the sum of edge weights of the tree.

Problem Description

This is an interactive problem.

There is a weighted tree with nn nodes, numbered 1n1-n. The degree of each node is known. djy wants to know the sum of the weights of all edges in the tree, but he is too weak and cannot solve such a simple problem, so he throws it to you.

Since you are strong, you are allowed to make some changes to this tree: delete all nodes with degree 11, and obtain the number of remaining nodes and the degree of each remaining node.

You may ask the interactive library three types of queries:

  • For a node that currently exists in the tree, ask for its dfs order1^1.
  • For a pair of nodes that currently exist in the tree, ask for the distance between them2^2.
  • Delete all nodes with degree 11 in the current tree, delete the edges adjacent to those nodes, and renumber all remaining nodes. It is guaranteed that the remaining nodes will be numbered 1k1-k, where kk is the number of remaining nodes.

You need to use no more than 142 operations (including submitting the answer), and before the tree is deleted to empty, find the sum of the weights of all edges in the current tree.


Notes:

  • dfs order1^1: The dfs order means the order in which nodes are first visited when performing depth-first search starting from the current node 11. The dfs order of a tree is not unique. After each deletion operation, the dfs order will be reset. It is guaranteed that the dfs order does not change due to other operations. That is, for two queries asking the dfs order of the same node, if there is no deletion operation in between, the returned value is guaranteed to be the same.
  • Distance2^2: The sum of edge weights along the path between two nodes in the tree. In particular, the distance between two identical nodes is 00.

Input Format

Interactive Mode.

This problem uses IO interaction.

Before interaction starts, you need to read nn, the number of nodes in the tree.

In the next line, there are nn numbers, representing the degree of each node.

You can make three types of queries:

  • dfn u: Query the dfs order of the node with index uu in the interactive library. The library returns one integer in a line, which is the dfs order of uu.

  • dis u v: Query the distance between nodes uu and vv in the interactive library. The library returns one integer in a line, which is the distance between uu and vv.

  • del: Ask the interactive library to delete all nodes with degree 11 and the edges connected to them. The library will renumber the nodes and rerun a dfs order. The library returns: the first line is one integer, the size of the tree mm; the second line contains mm integers, where the ii-th integer is the degree of the node numbered ii.

If you have found the sum of the weights of all edges in the current tree, output the answer xx in the format ! x, and terminate the program immediately.

Please ensure that any node used as a query parameter has not been deleted, and that after a del operation the tree is not empty.

If your operation is illegal or the number of operations exceeds 142, the interactive library will terminate the program immediately, and the result will be judged as WA/RE/TLE/MLE.

After each query, do not forget to output a newline and flush the buffer, otherwise unknown errors may occur. To avoid this, you may use:

  • For C++, use fflush(stdout) or cout.flush().
  • For Java, use System.out.flush().
  • For Python, use stdout.flush().
  • For other languages, please read related documents.

Output Format

See Interactive Mode.

6
3 1 2 1 1 2

1

5




dfn 1

dis 6 2

! 17

Hint

The sample is only for understanding the interaction process, and may not be logically consistent.

[Sample Explanation]

The shape of the tree is shown above.

In the first query, the dfs order of node 11 is 11.

In the second query, the distance between node 22 and node 66 is 55.

The sum of the weights of all edges in the current tree is 1717.


[Constraints]

This problem uses bundled test.

  • Subtask 1 (1 pts): n2n \le 2.
  • Subtask 2 (4 pts): n4n \le 4.
  • Subtask 3 (20 pts): n150n\le 150.
  • Subtask 4 (10 pts): The tree is a chain.
  • Subtask 5 (30 pts): It is guaranteed that the number of nodes with degree 11 is at most 5050.
  • Subtask 6 (20 pts): n2000n\le 2000.
  • Subtask 7 (15 pts): No special restrictions.

For 100%100\% of the data, 1n50001\le n\le 5000, the weight of each edge is at most 10510^5 and is a positive integer.

If any incorrect solution passed, please send a private message to the problem setter to strengthen the testdata. (A hack would be even better.)

Translated by ChatGPT 5