Nyoj 42 stroke

Source: Internet
Author: User
One stroke problem time limit: 3000 MS | memory limit: 65535 kb difficulty: 4
Description

ZYC prefers to play games from an early age, including drawing a stroke. He wants to ask you to write a program for him to determine whether a graph can be used.

It is specified that all edges can be painted only once and cannot be repeated.

 

 
Input
The first row only has a positive integer n (n <= 10) indicating the number of test data groups.
The first row of each group of test data has two positive integers p, q (P <= 1000, q <= 2000), indicating the number of vertices and connections in the painting respectively. (The vertex number ranges from 1 to P)
In the next Q row, each row has two positive integers A and B (0 <a, B <p), indicating that there is a line between the two points numbered A and B.
Output
If there is a line that meets the conditions, the output is "yes ",
If no matching line exists, output "no ".
Sample Input
24 31 21 31 44 51 22 31 31 43 4
Sample output
NoYes

Http://acm.nyist.net/JudgeOnline/problem.php? PID = 42

Problem Analysis:

Euler's theorem: If a network is connected and the number of odd vertices is equal to 0 or 2, it can draw one stroke; otherwise, it cannot draw one stroke.

How to judge a stroke:

① Is connected. A graph is called a connected graph if any two points have a line segment connected. If it is not connected, you cannot draw a row.

② The number of singularity is 0 or 2. The endpoint of a line segment can be divided into two types: singularity and even number. A point. The number of line segments whose endpoint is an odd number is called a singularity, and the number of line segments is an even number.

If an image is a stroke, it depends on the number of singularity. If the number of singularity is 0 or 2, it is a stroke. Otherwise, it is not a stroke.

Therefore, this problem can be completely transformed:

Step 1: first, let's determine the connectivity, regardless of the past 20 days.

Step 2:

(1) If it is connected, we can judge whether the number of degree singularity of this image is 0 or 2. If it is, it indicates that it is a euclidean, that is, it can be drawn in one stroke, otherwise, you cannot draw

(2) If it is not connected, it means that this graph cannot be drawn in one stroke.

 

Code 1: -- Deep Search

 

1 # include <stdio. h> 2 # include <string. h> 3 int p, q; 4 int Bian [1005]; 5 bool map [1005] [1005], vis [1005]; 6 void DFS (INT cur) 7 {8 vis [cur] = true; 9 for (INT I = 1; I <= P; I ++) 10 if (Map [cur] [I]) 11 {12 Bian [cur] ++; 13 if (! Vis [I]) 14 DFS (I); 15} 16} 17 18 int main () 19 {20 int t; 21 scanf ("% d", & T ); 22 while (t --) 23 {24 int OK = 1; 25 memset (MAP, false, sizeof (MAP); 26 memset (VIS, false, sizeof (VIS )); 27 memset (Bian, 0, sizeof (Bian); 28 29 scanf ("% d", & P, & Q); 30 31 for (INT I = 0; I <q; I ++) 32 {33 int A, B; 34 scanf ("% d", & A, & B ); 35 map [a] [B] = true, map [B] [a] = true; 36} 37 38 DFS (1); // determine whether the connection is successful, if Vis has a false value, it is not connected to 39 40 for (INT I = 1; I <= P; I ++) 41 if (! Vis [I]) 42 {43 OK = 0; 44 break; 45} 46 47 If (! OK) printf ("NO \ n"); 48 else49 {50 int xx = 0; 51 for (INT I = 1; I <= P; I ++) 52 If (Bian [I] % 2) xx ++; 53 If (xx = 0 | xx = 2) printf ("Yes \ n "); 54 else printf ("NO \ n"); 55} 56} 57 Return 0; 58}

 

After the AC, there is another way to identify connectivity-and check the set of ideas on the Internet.

And query set information: http://www.cnblogs.com/cyjb/p/UnionFindSets.html

Code 2: -- And query the set

 

1 // check 2 # include <stdio. h> 3 # include <string. h> 4 int father [1010], ANS [1010]; 5 void Init () 6 {7 for (INT I = 0; I <1010; I ++) 8 father [I] = I; 9} 10 int find (INT X) 11 {12 if (father [x] = x) 13 return X; 14 else15 return father [x] = find (father [x]); 16} 17 int main () 18 {19 int ncases, n, m, X, Y, count, jdcount; 20 scanf ("% d", & ncases); 21 While (ncases --) 22 {23 memset (ANS, 0, sizeof (ANS); 24 Init (); 25 COUNT = jdcount = 0; 26 SC ANF ("% d", & N, & M); 27 for (INT I = 1; I <= m; I ++) 28 {29 scanf ("% d", & X, & Y); 30 ans [x] ++; ans [y] ++; 31 X = find (x); y = find (y); 32 If (X! = Y) 33 father [x] = Father [y]; 34} 35 36 for (I = 1; I <= N; I ++) 37 If (find (I) = I) 38 count ++; 39 for (I = 1; I <= N; I ++) 40 if (ANS [I] % 2 = 1) 41 jdcount ++; 42 if (jdcount = 0 | jdcount = 2) & COUNT = 1) 43 printf ("Yes \ n "); 44 else45 printf ("NO \ n"); 46} 47}

 

Nyoj 42 stroke

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.