[Topic]
Given n beads numbered from 1 to n, followed by m pairs (x, y), it indicates that beads X and Y can be adjacent and should be made up of the smallest beads, and the number of beads used is an odd number greater than or equal to 3. Calculate the value of the smallest number of beads.
[Detailed analysis]
When I first saw the question, I just got a DIY: the first question in the 2008 "sunline Cup" Invitational competition. I thought about it and thought it could be converted into a minimal ring, I will have the smallest ring on my own (For details, refer to my blog on the smallest ring ). The data volume is 1000. I think the Floyd algorithm is okay. Write it. The result is a timeout. After the game, I tried Baidu and read a lot of explanations. The following is a detailed explanation.
The complexity of this type of questions is analyzed. You can enumerate each point one by one. For the depth of each enumeration record accessing this point, you only need to add the current depth to the depth of access to this point. calculate the total number of points used and use two queues to form layer-by-layer traversal (ensure that the path is not repeated) it feels good that the minimum value calculated by each enumeration can be used as the next threshold value to narrow down the scale.
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
Struct point
{
Int X, Y;
Friend bool operator <(point a, point B)
{
If (A. X! = B. X)
Return A. x> B. X;
Return A. Y> B. Y;
}
};
Int main ()
{
Int m, n, I, J, K, h;
Int ans, sum;
Point cur, in;
Scanf ("% d", & N );
While (n --)
{
Priority_queue <point> q;
Scanf ("% d", & M );
For (I = 0; I <m; I ++)
{
Scanf ("% d", & cur. X, & cur. y );
Q. Push (cur );
}
Ans = 0;
Sum = 0;
While (! Q. Empty ())
{
Sum ++;
Cur = Q. Top ();
Q. Pop ();
If (ANS <cur. X)
Ans = cur. X;
If (sum % 2 = 0)
Continue;
In. x = cur. x + cur. Y;
In. Y = cur. Y;
Q. Push (in );
}
Printf ("% d \ n", ANS );
}
Return 0;
}