#P10017. [集训队互测 2023] Axium Crisis

    ID: 11420 远端评测题 10000ms 2048MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>集训队互测2023Special Judge最近公共祖先 LCA均摊分析状压 DP

[集训队互测 2023] Axium Crisis

Background

In front of the gloomy tower, Dui Li (pinyin: duì lì) saw a few fragments of light.

Those fragments of light lingered around Dui Li like flowers as decoration.

Stepping into the twisted maze, Dui Li tried to collect the fragments of conflict inside it, and attempted to destroy this maze.

Around Dui Li, fragments of light and conflict were everywhere, flying and crossing.

At last, Dui Li reached the deepest part of the maze.

On that memory shard with an extremely strange shape, what was reflected was a memory of a world heading toward destruction.

The apocalypse arrived: the sky was torn apart, and the earth collapsed.

Because the “energy” carried by this shard was far too huge, Dui Li tried to use the surrounding fragments of light and conflict to ease this tremendous mental impact.

Specifically, this twisted shard forms a “tree” structure, and Dui Li will place either a light fragment or a conflict fragment on each edge of the tree.

Dui Li will cut the edges of this tree into several chains, so that in the end every edge belongs to exactly one chain. Due to the special structure of the shard, a node in the tree can belong to multiple chains at the same time.

Dui Li will take some of these chains, merge the prefix segments that have the same placed fragments, and finally form a new tree, the so-called “Trie”.

The more nodes this new tree has, the more it can soothe Dui Li’s emotions and help them calm down.

In madness, Dui Li has already placed light fragments or conflict fragments on some edges of the shard.

In a brief moment of clarity, Dui Li realized something was off. Therefore, Dui Li can still freely choose to place either a light fragment or a conflict fragment on the remaining edges.

In a daze, Dui Li found that they did not know how to place and cut in the best way.

Thoughts raced rapidly. What is optimal?

You surely already have the answer.

Problem Description

You are given a tree with nn nodes, numbered 0n10\sim n-1.

Each edge has a weight. Usually the edge weight is 00 or 11, but some edges have an undetermined weight.

You need to assign each undetermined edge a weight of 00 or 11, and then take several directed paths from the tree such that these chains are pairwise edge-disjoint.

Then you will insert these paths into a 0/1-Trie, and you want to maximize the number of nodes in this 0/1-Trie (definition of 0/1-Trie omitted).

You may need to construct an explicit selection plan.

Input Format

There are multiple groups of testdata in each test point.

The first line contains two integers T,oT,o, where TT is the number of testdata groups, and oo indicates some special properties of this test point (see the description in the “Constraints and Hint” section).

Then follow TT groups of testdata.

For each group, the first line contains an integer nn, the number of nodes.

The next n1n-1 lines each contain three integers u,v,wu,v,w, representing an edge connecting nodes uu and vv. When 0w10\le w\le 1, it means the edge weight is ww; otherwise w=2w=2 always holds, meaning the edge weight is not yet determined.

It is guaranteed that these edges form a tree on the node set 0n10\sim n-1.

Output Format

At the beginning, output a number cc (c{0,1}c\in\{0,1\}), indicating whether you choose to output a construction for this test point. We will use a Special Judge to verify whether your output is correct. Note that this will affect the score upper bound for this test point. See the description in the “Constraints and Hint” section.

For each testdata group, first output one line with a number AA, the answer for this group.

When c=1c=1, you also need to continue outputting your constructed plan in the following format:

  • First output one line with an integer mm, the number of paths in your plan.
  • Then output one line with n1n-1 numbers, each being 00 or 11, giving the edge weights in the input order. For edges whose weights are already determined, output them unchanged.
  • Then output mm lines, each with two numbers u,vu,v, representing a path in the tree from uu to vv. You must have uvu\neq v, and the paths must be pairwise edge-disjoint.
9 0
9
1 2 1
3 4 1
5 6 1
7 8 1
2 0 0
4 0 0
6 0 0
8 0 0
9
1 2 2
3 4 1
5 6 1
7 8 1
2 0 0
4 0 0
6 0 0
8 0 0
5
1 2 2
3 4 1
0 3 1
2 3 0
17
1 2 1
2 3 0
3 4 1
4 0 0
5 6 1
6 7 0
7 8 1
8 0 0
9 10 1
10 11 0
11 12 1
12 0 0
13 14 1
14 15 0
15 16 1
16 0 0
17
1 2 1
2 0 0
3 4 1
4 0 0
5 6 1
6 0 0
7 8 1
8 0 0
9 10 1
10 11 0
11 12 1
12 0 0
13 14 1
14 15 0
15 16 1
16 0 0
17
1 2 2
2 0 2
3 4 2
4 0 2
5 6 2
6 0 2
7 8 2
8 0 2
9 10 2
10 11 2
11 12 2
12 0 2
13 14 2
14 15 2
15 16 2
16 0 2
18
1 2 1
2 0 0
3 4 1
4 0 0
5 6 1
6 0 0
7 8 1
8 0 0
9 10 1
10 11 0
11 12 1
12 0 0
13 14 1
14 15 0
15 16 1
16 0 0
0 17 2
18
1 2 2
2 0 2
3 4 2
4 0 2
5 6 2
6 0 2
7 8 2
8 0 2
9 10 2
10 11 2
11 12 2
12 0 2
13 14 2
14 15 2
15 16 2
16 0 2
17 0 2
18
1 2 2
2 3 2
3 4 2
4 5 2
5 6 2
6 7 2
7 8 2
8 9 2
9 10 2
10 11 2
11 12 2
12 13 2
13 14 2
14 15 2
15 16 2
16 17 2
17 0 2
1
8
3
1 1 1 1 0 0 0 0
1 3
5 6
6 7
9
2
0 1 1 1 0 0 0 0
3 5
1 7
5
2
0 1 1 0
4 3
1 0
16
3
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
5 1
13 14
14 9
14
5
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
3 1
5 6
14 13
14 7
6 9
16
3
0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0
7 5
1 3
13 9
15
4
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0
13 3
1 7
0 5
17 9
16
4
1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
1 7
17 0
5 3
13 9
18
1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0

Hint

Sample Explanation

The answer file corresponding to this sample is:

8
9
5
16
14
16
15
16
18

The sample output is the .out file, which is what you need to output. When c=1c=1, you need to construct one valid plan.

The sample answer is the .ans file. It only provides the answer for each group, and does not provide any construction.

Next are the illustrations of these 99 sample groups (one image before choosing edge weights, and one after).

sample0_1_1.pngsample0_1_2.png

sample0_2_1.pngsample0_2_2.png

sample0_3_1.pngsample0_3_2.png

sample0_4_1.pngsample0_4_2.png

sample0_5_1.pngsample0_5_2.png

sample0_6_1.pngsample0_6_2.png

sample0_7_1.pngsample0_7_2.png

sample0_8_1.pngsample0_8_2.png

sample0_9_1.pngsample0_9_2.png

More Samples

Because the data size of this problem is too large, submitting directly for judging will put great pressure on the judging machine. This problem will provide many large samples, so please try to reduce the number of submissions.

For more samples, see the provided files axiumcrisis*.in/ans, a total of 2020 groups, basically constructed according to the partial-score approach.

Note that the provided answer files do not give any construction, and only give the answer for each group.

A checker.cpp is also provided. You can compile it yourself and run it in a terminal to check validity. For the specific usage, see the description in the “Constraints and Hint” section. The Special Judge used in the official evaluation is not the same as it.

To help you understand the statement better, an extra hand-made sample is provided here. This sample is not included in the provided files.

It is recommended to use this sample and the sample explanation to verify your understanding, to avoid misreading.

Constraints and Hint

Unlike the version actually used in the mutual test, this problem uses a version with larger constraints.

For all data, it is guaranteed that 2n182\le n\le18 and 1T30001\le T\le3000.

The distribution of data sizes is shown in the table below. All subtasks have equal scores, each worth 5pts\rm5pts at full score. A column like (l,r)(l,r) corresponds to the number of groups with lnrl\le n\le r. “Unlimited” means there is no additional restriction.

All subtasks are judged in bundles: the score of a subtask is the minimum score among its test points. Subtask dependency means the current subtask is judged only if all dependent subtasks have non-zero scores, and the score is also the minimum among the current subtask and its dependent subtasks. The meaning of oo will be specified later.

Subtask (2,4)(2,4) (5,6)(5,6) (7,8)(7,8) (9,11)(9,11) (12,14)(12,14) (15,17)(15,17) (18,18)(18,18) oo Dependencies
11 1000\le1000 =0=0 =0=0 =0=0 =0=0 =0=0 =0=0 =0=0 None
22 Unlimited 15\le15 11
33 500\le500 10\le10 22
44 1000\le1000 50\le50 10\le10 =2=2 None
55 =3=3
66 =4=4 44
77 =0=0 3,5,63,5,6
88 Unlimited 1000\le1000 60\le60 10\le10 =2=2 44
99 =3=3 55
1010 =4=4 6,86,8
1111 =0=0 7,9,107,9,10
1212 Unlimited 300\le300 30\le30 10\le10 =2=2 88
1313 =3=3 99
1414 =4=4 10,1210,12
1515 =0=0 11,13,1411,13,14
1616 500\le500 60\le60 20\le20 10\le10 =1=1 None
1717 =2=2 1212
1818 =3=3 13,1613,16
1919 =4=4 14,16,1714,16,17
2020 =0=0 15,18,1915,18,19

Next, we explain the special properties described by oo.

  • When o=0o=0, no special property is guaranteed.
  • When o=1o=1, it is guaranteed that w=0w=0 in the input.
  • When o=2o=2, it is guaranteed that w=2w=2 in the input.
  • When o=3o=3, it is guaranteed that w=0w=0 or w=1w=1 in the input.
  • When o=4o=4, it is guaranteed that w=0w=0 or w=2w=2 in the input.

Next, we explain how outputting a construction affects your score.

  • If you choose c=0c=0, then if your answer is correct, you will get 80%80\% of the score for this test point; otherwise you get 00 for this test point.
  • If you choose c=1c=1, then only when both your answer and your construction are correct will you get the full score for this test point; otherwise you get 00 for this test point.

So if your construction output might be wrong, please carefully consider switching to not outputting a construction.

Next is how to use checker.cpp.

checker.cpp uses a command-line format similar to Testlib, but it is not based on Testlib, so it does not require the testlib.h file. It is also compatible with the Lemon format. Specifically, you can use it as follows:

Open a terminal, enter the folder containing checker.cpp, and first run g++ checker.cpp -o checker to generate the executable (your local compiler should use C++11 or above by default).

Suppose the input file is data.in, your output file is data.out, and the standard answer file is data.ans. Put the executable checker and the files data.in/out/ans in the same folder, then run the following command in the terminal:

  • If you use Windows, run checker data.in data.out data.ans 5 in cmd.
  • If you use Linux, run ./checker data.in data.out data.ans 5 in bash.

If you remove the last 5 in the command, it will treat c=0c=0 as AC as well.

After waiting a moment, it will print the message.

If you use Lemon for local judging, you can directly use the executable of checker.cpp as the “custom checker” in Lemon.

Epilogue

Watching the apocalyptic scene through the gaps between their fingers, Dui Li swallowed, relying on an unknown kind of courage, and moved their hand away from their face.

Dui Li reached out and took in the end of the world, storing it among the countless memories they had collected.

The rest of the tragic memories seemed not worth mentioning under the reflection of this shard.

Dui Li was sure they had become strong enough, and naturally wanted to destroy everything at once.

Thus, along with that sincere smile and weary laughter, Dui Li descended from the sky to the ground.

Driven by such power, that ancient tower gradually fell.

And Dui Li, holding firm to a hero-like belief, kept moving forward without hesitation.

Translated by ChatGPT 5