[Hdoj2454] degree sequence of graph G

Source: Internet
Author: User

In a word, the degree sequence Havel theorem of vertices ~

Definition: the vertex degree sequence {DN} of an undirected graph is given. It is required to determine whether a simple undirected graph can be constructed.

Analysis:

The greedy method is to sort the vertices by degree from large to small each time, retrieve the vertices vi with the highest degree, connect them to those vertices with a higher degree, and subtract the degree of VJ. After the connection is complete, the VI will not be considered, and the remaining points will be sorted again and then connected with the highest degree ...... In this way, a feasible solution can be constructed.
There are two points for determining that there is no solution. If a selected VI has more than the remaining vertices, there is no solution. If the degree of a VJ is reduced to a negative number, there is no solution.
As for what is the Havel theorem, the above construction process is ~

A Simple Proof of the theorem is as follows:
(<=) If d' can be simply visualized, we only need to link the largest vertex in the source image and the D1 vertex in D' to the largest vertex in the source image. This is a simple graph.
(=>) If D can be converted into a simple graph, the resulting simple graph is G. There are two considerations:
(A) If G contains edges, remove these edges to a simple graph G', and then d' can be converted into G'
(B) if there is a point VI, VJ makes I = Dj, K must exist so that (Vi, VK) is in G, but (vj, VK) is not in G. In this case, GG = G-{(Vi, VK), (V1, vj)} + {(VK, vj), (V1, vi )}. The degree sequence of GG is still D, and we return to the case ().

View code

1 # include <cstdio>
2 # include <cstring>
3 # include <algorithm>
4
5 usingnamespace STD;
6
7 constint NF = 2;
8 constint size = 1024;
9
10 int CNT [NF];
11 int deg [NF] [size];
12
13 bool CMP (constint & A, constint & B)
14 {
15 return A> B;
16}
17
18 int main ()
19 {
20 int CAS;
21
22 scanf ("% d", & CAS );
23 For (INT cc = 0; CC <CAS; CC ++)
24 {
25 scanf ("% d", & CNT [0]);
26 For (INT I = 0; I <CNT [0]; I ++)
27 scanf ("% d", & deg [0] [I]);
28
29 sort (deg [0], deg [0] + CNT [0], CMP );
30
31 While (deg [0] [CNT [0]-1] = 0 & CNT [0]> 0) CNT [0] --;
32
33 int oo = 0;
34 while (CNT [oo]> 1)
35 {
36 sort (deg [oo], deg [oo] + CNT [oo], CMP );
37
38 If (CNT [oo]-1 <deg [oo] [0]) break;
39
40 CNT [1-oo] = 0;
41 for (INT I = 1; I <CNT [oo]; I ++)
42 {
43 if (I <= deg [oo] [0]) deg [oo] [I] --;
44 If (deg [oo] [I]> 0)
45 deg [1-oo] [CNT [1-oo] ++] = deg [oo] [I];
46}
47
48 oo = 1-Oo;
49}
50
51 printf ("% s \ n", CNT [oo] = 0? "Yes": "no ");
52}
53 return0;
54}

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.