Problem I: Hanoi Tower Once More
Description
You know the game of Hanoi Tower, right? People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task.
But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr. S invented a little game on it. The game consists of N pegs and a LOT of balls. Each ball has a different number. The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other.
The player should place one ball on the top of a peg at a time. And we must start from the smallest number all the way to the largest .So no ball with a smaller number should be above a ball with a larger number.
Here comes our question: Given the number on each ball, how many pegs, at least, are needed to make all the balls placed?
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing one integers n (n≤300), which is the amount of balls. In the next line, n different positive integers, the number on each ball, is given.
Output
Print the minimum pegs needed to make all the balls placed.
Sample Input
2
4
1 2 3 4
4
1 3 6 10
Sample Output
3
1
最小路径覆盖的模板题,“黑书”中有原题,并且给出了结论: 所求的路径数 = 顶点数- 最大匹配
下面算法就没什么好说的了,用了KM算法,这里是O((N+1)^3)的复杂度