Problem 3811 --Little prince and the garden of roses

3811: Little prince and the garden of roses

"
Time Limit $1$ 秒/Second(s) Memory Limit $128$ 兆字节/Megabyte(s)
提交总数 $0$ 正确数量 $0$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签
He was standing before a garden, all a-bloom with roses.
"Good morning," said the roses.
The little prince gazed at them. They all looked like his flower.
"Who are you?" he demanded, thunderstruck.
"We are roses," the roses said.
And he was overcome with sadness. His flower had told him that she was the only one of her kind in all the universe. And here were five thousand of them, all alike, in one single garden!
"She would be very much annoyed," he said to himself, "if she should see that... she would cough most dreadfully, and she would pretend that she was dying, to avoid being laughed at. And I should be obliged to pretend that I was nursing her back to life-- for if I did not do that, to humble myself also, she would really allow herself to die..."
Then he went on with his reflections: "I thought that I was rich, with a flower that was unique in all the world; and all I had was a common rose. A common rose, and three volcanoes that come up to my knees-- and one of them perhaps extinct forever... that doesn't make me a very great prince..."
And he lay down in the grass and cried.

Assume the garden of roses has n rows of roses, where there are n roses in each row, aligned in a square. Initially, each rose has a color(color of the petals, of course!). The rose in the i-th row and j-th column is colored with cij.(There are surely colorful roses! Why would people only think of red ones when it comes to roses?) 
When little prince wanders in the garden of roses, he will burst into tears when he sees two roses with the same color in the same row or the same column, as that would remind him his flower is not unique in this world. 
You, a kind person, definitely don't want to see the little prince cry. Fortunately, you arrived at the garden of roses just before the little prince. You have a magic painter that could paint the stems of some roses into different colors(You don't want to ruin the petals of the roses, do you?). Initially, the stems of all roses are green. You want to paint the stems of some roses into different colors, such that no two roses in the same row or column would no longer have both the same color of petals and stems. The only problem is the number of pigments with distinct colors you need to prepare. As preparing pigments with distinct colors is time-consuming, you want to use as little pigments as possible. Please find a way to paint the flowers such that the condition is satisfied. As long as the number of pigments is minimized, any valid way of painting flowers would be considered correct.

The first line contains a number T(1≤T≤100), denoting the number of test cases.
The first line of each test case contains an integer n(1≤n≤300), denoting the size of the garden.
Then n lines follow, the ith(1≤i≤n) of the n lines contain n integers ci1,ci2,…,cin, where cij(1≤i,j≤n,1≤cij≤n2) denotes the color of the rose in the ith row and jth column.

It is guaranteed that at most 10 cases n>20 and at most 4 cases n>100.
For each test case, output two integers d,m(0≤d≤n2,1≤m≤n2) in the first line, denoting the number of distinct pigments used and the number of roses to be painted.
For the next m lines, output three integers i,j,c(1≤i,j≤n,1≤c≤d) in a line, denoting that you would paint the stem of the rose in the ith row and jth column into color cIf the stem of some rose is not painted, it is regarded to have color 0.
As long as the number of pigments is minimized, any valid way of painting the stems of the roses would be considered correct.
3
3
1 1 1
1 1 1
1 1 1
3
1 2 1
2 1 2
1 2 1
3
1 2 3
4 5 6
7 8 9
2 6
1 2 2
1 3 1
2 1 1
2 3 2
3 1 2
3 2 1
1 4
1 3 1
3 1 1
2 3 1
3 2 1
0 0

推荐代码 查看3811 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$ $ms]
内存最少[$ $KB]
第一AC
第一挑战

赛题来源/所属竞赛 N/A

竞赛编号 竞赛名称 竞赛时间 访问比赛