Hdu 1689 Alien's neck.pdf (bfs layered graph pruning)

Source: Internet
Author: User

Hdu 1689 Alien's neck.pdf (bfs layered graph pruning)

Alien's neck.pdf Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1526 Accepted Submission (s): 415


Problem DescriptionJYY is taking a trip to Mars. to get accepted by the Martians, he decided to make a magic neck.pdf for their king. (Otherwise, JYY will be eaten) Now, he has collected using magic bils, and he is going to string them up.
Unfortunately, only particle pairs of bils can be adjacent in the neck.pdf, otherwise they will explode. notice that the first and the last ball in the neckdeskare also adjacent. besides, the Martians think even numbers are unlucky, so the number of bils in the neck?must be odd. (Of course each ball can be used only once)
A necklace contains at least 3 bils. Because the bils are really precious, JYY wants the neck1_has as few bils as possible. (Then he can give rest bils to his GF)
So JYY wonders the number of bils he has to use to make this necktasks.

InputThe input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
For each input, the first line contains 2 numbers N and M, N is the number of bils JYY collected, and M is the pairs of compatible bils. bils are numbered from 1 to N. followed M lines, each contains 2 numbers A and B, means that ball A and ball B are compatible. for each case, 0 <N <= 1,000, 0 <M <= 20,000.

OutputIf the gift can't be done, just print "Poor JYY." in a line, otherwise, print the minimal number of bils in the neck.pdf. Use the format in the example.

Sample Input
25 61 22 41 33 54 34 52 11 2

Sample Output
Case 1: JYY has to use 3 balls.Case 2: Poor JYY.

Let's find a ring so that the number of points in the ring is odd and the number of points is at least 3 ,.

Because one point can arrive in multiple ways, but the first arrival from one point must be the shortest path, and the novelty required by the question is marked by the parity of each arrival of one point step.

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define N 1005 # define ll _ int64const int inf = 0x7fffffff; vector
     
      
G [N]; int vis [N] [2]; struct node {int u, t; friend bool operator <(node a, node B) {return. t> B. t ;}}; int bfs (int s) {int I, u, v; priority_queue
      
        Q; node cur, next; cur. u = s; cur. t = 1; memset (vis, 0, sizeof (vis); vis [s] [1] = 1; // odd point, with a beads q. push (cur); while (! Q. empty () {cur = q. top (); q. pop (); u = cur. u; for (I = 0; I
       
         3) // The Node arrives twice, so it should be reduced by a return next. T-1; if (! Vis [v] [next. t % 2]) {vis [v] [next. t % 2] = 1; q. push (next) ;}}return inf;} int main () {int I, n, m, u, v, tt, cnt = 0; scanf ("% d", & tt); while (tt --) {scanf ("% d", & n, & m); for (I = 1; I <= n; I ++) g [I]. clear (); while (m --) {scanf ("% d", & u, & v); g [u]. push_back (v); g [v]. push_back (u);} int ans = inf; for (I = 1; I <= n; I ++) ans = min (ans, bfs (I )); if (ans = inf) printf ("Case % d: Poor JYY. \ n ", ++ cnt); else printf (" Case % d: JYY has to use % d bils. \ n ", ++ cnt, ans);} return 0 ;}
       
      
     
    
   
  
 


The following method uses an array to record the number of steps to reach this point. When you access this point again, it indicates that a ring appears and you can directly judge the parity.

It is required that the number of beads is greater than three. Therefore, a pre variable should be used to record the label of the previous node and determine whether two points are directly ring.

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define N 1005 # define ll _ int64const int inf = 0x7fffffff; vector
     
      
G [N]; int vis [N] [2]; int ans; struct node {int u, t, pre; friend bool operator <(node a, node B) {return. t> B. t ;}}; int bfs (int s) {int I, u, v, t; priority_queue
      
        Q; node cur, next; cur. u = s; cur. t = 1; cur. pre =-1; memset (vis, 0, sizeof (vis); vis [s] [1] = 1; q. push (cur); while (! Q. empty () {cur = q. top (); q. pop (); u = cur. u; for (I = 0; I
       
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.