luogu#P5027. Barracuda

Barracuda

Background

The small square’s adventure journey has not been smooth.

Along the way, the small square saw magnificent and beautiful islands being polluted, saw grand volcanoes, and encountered many enemies.

Now, the small square is dealing with a huge triangle.

Problem Description

The big triangle tells the small square about its past: it used to be a treasure miner, but later it was corrupted by the Lord of Darkness and ended up like this.

It also hopes that the small square can defeat the Lord of Darkness, but because the Lord of Darkness has spies everywhere, it must set obstacles for the small square to fool those “spies”.

The problem it gives to the small square is: it has nn small triangles, and each small triangle has a certain weight. It performed n+1n + 1 weighings on these triangles; however, due to an issue with the pan balance (?), one weighing result is wrong.

Now, the big triangle wants to know the index of the heaviest small triangle.

An input is considered valid if and only if it satisfies the following condition:

There do not exist two indices ii, jj such that, when we assume the ii-th weighing data is wrong, we can find a valid solution, and when we assume the jj-th weighing data is wrong, we can also find a valid solution.

A valid solution is defined as follows:

  1. There is exactly one heaviest triangle.

  2. There is no triangle whose weight is undetermined.

  3. The weights of all triangles are positive integers.

Input Format

The first line contains a positive integer nn, representing the number of small triangles.

The next n+1n + 1 lines are given in the following format:

First, an integer mm, representing how many small triangles are grabbed in this weighing.

Then mm integers, representing the indices of the small triangles being weighed.

The last integer weightweight represents the result of this weighing.

Output Format

If it is valid, output the index of the heaviest small triangle; otherwise output "illegal" (without quotes).

2
1 1 2
2 1 2 5
2 1 2 1
2
2
1 1 2
2 1 2 4
2 1 2 5
2
2
1 1 2
2 1 2 6
2 1 2 5
illegal

Hint

Sample 1:

If the result of the first weighing is wrong, then a correct solution cannot be obtained.

If the result of the second weighing is wrong, then the weight of the second small triangle is negative, which is obviously not correct.

If the result of the third weighing is wrong, we obtain that triangle 11 has weight 22, triangle 22 has weight 33, and triangle 22 is the heaviest.

This problem uses bundled testdata and has three subtasksubtasks, described as follows:

subtask030Ptssubtask 0 - 30Pts: It is guaranteed that the weights of small triangles are <= 20 and n<=5n <= 5. In this subtasksubtask, you can get 1010 points for each test you pass.

subtask130Ptssubtask 1 - 30Pts: It is guaranteed that the weights of small triangles are <= 100 and n<=100n <= 100. The testdata are randomly generated.

subtask240Ptssubtask 2 - 40Pts: It is guaranteed that the weights of small triangles are <= 100 and n<=100n <= 100.

In the last two subtasksubtasks, you must pass all testdata to get any score.

For 100%100\% of the testdata, 1<=m<=n1 <= m <= n

Translated by ChatGPT 5