HDU 1941 justice leleague

Source: Internet
Author: User

Hdu_1941

To put it bluntly, we need to delete some vertices, And the deleted vertices cannot have edges between them. The left vertices must have edges between them, that is, forming a complete graph.

At first glance, there is no idea, but there are probably two ways to go, either consider what kind of points will definitely stay, or consider what kind of points will be deleted first. After thinking about it, I thought the second approach was still feasible. So I started to think about it.

First, isolated points can be deleted (here and later we will discuss the degree of the default maximum degree is relatively large ), second, it seems that the leaf node, that is, the point with a degree of 1, can also be deleted, and then it seems that it is not very good to discuss, but the above two types of points have a common, that is, the degree is relatively small, then, can I delete a point with a relatively small degree first?

Consider a vertex A with a relatively small degree and a vertex B with a relatively large degree. There are only four cases based on the subordination between the two vertex sets: ① A and B are deleted; ② A is deleted, B is left; ③ a is left, and B is deleted. However, this is not the case, because assume that A is a point in the full graph, because B is higher than, even if it has an edge with all vertices in the full graph, there will still be extra degrees, so an edge will be formed with the deleted vertices, so this situation cannot happen; ④ if both A and B are left behind, this is also impossible, because the degree of A is smaller than that of B, and it is impossible to form a complete graph together with B. After analyzing these four cases, we will be happy, because we find that a will be deleted no matter what the situation is. So we can sort the vertices in order of degrees, and then delete them from small vertices.

Of course, there is another question to be discussed here. What if the degrees of A and B are equal? In this case, deleting a does not matter, because even if A is a point in the full graph, deleting a point in the full graph will also be a complete graph. So will it be because deleting a leaves B at the same time (if B can also be deleted, it will certainly not affect the formation of a complete graph) and will not form a complete graph?

In this case, we may discuss that if A and B are two vertices in a complete graph, the effect of deleting a and B is the same and will not be affected. If A and B are not two vertices in a complete graph, if A is in a complete graph, B must be deleted, in this way, B will have redundant degrees and vertices in the deleted set form edges. In turn, if B is a vertices in the complete graph, it is the same if B is left to delete, therefore, if A and B are not two vertices in a complete graph, there would be no legal solution. Therefore, deleting a would be a fun task, and there is no solution, finally, you can determine whether the left vertex will form a complete graph. All in all, deleting a will not cause any adverse consequences.

Discussed here,AlgorithmIt is basically formed:

The basic idea is to delete vertices that will be deleted, ensure that there is no edge between the deleted vertices, and finally check whether the remaining vertices can constitute a complete graph.

First, sort the points in ascending order of degrees, and then delete them from a smaller degree. Then, update the degrees of the points connected to them, mark those vertices as "mandatory vertices" and jump over when the marked vertices are scanned. Finally, let's see if the left points can form a complete graph.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxd 50010 # Define Maxm 200010 # Define INF 0x3f3f3f Int  N, m, first [maxd], E, next [maxm], V [maxm], DGR [maxd], isin [maxd], R [maxd];  Bool CMP ( Const   Int & X, Const  Int & Y ){  Return DGR [x] < DGR [y];}  Void Add ( Int X, Int  Y) {v [E] = Y; next [E] = First [X], first [x] = e ++ ;}  Void  Init (){  Int  I, X, Y; memset (first, -1 , Sizeof (First [ 0 ]) * (N + 1 ), E = 0  ; Memset (DGR,  0 , Sizeof (DGR [ 0 ]) * (N + 1  ));  For (I = 0 ; I <m; I ++ ) {Scanf ( "  % D  " , & X ,& Y ); ++ DGR [X], ++ DGR [y]; add (x, y), add (Y, x );}}  Void  Solve (){  Int I, j, X, CNT = 0 , Min = INF; memset (ISIN,  0 , Sizeof (ISIN [ 0 ]) * (N + 1 ));  For (I = 1 ; I <= N; I ++) R [I] = I; STD: Sort (R + 1 , R + 1 + N, CMP );  For (I = 1 ; I <= N; I ++ )  If (! ISIN [x = R [I]) {  For (J = first [X]; J! =-1 ; J = Next [J]) -- DGR [V [J], isin [V [J] = 1  ;}  For (I = 1 ; I <= N; I ++ )  If (ISIN [x = R [I]) ++ CNT, min = STD: min (Min, DGR [x]); printf (  "  % S \ n  " , CNT = min +1 ? "  Y  " : "  N  "  );}  Int  Main (){  While (Scanf ( "  % D  " , & N ,& M), n ){  If (M =0  ) {Printf (  "  Y \ n  "  );  Continue  ;} Init (); solve ();}  Return   0  ;} 

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.