NYoj-42-one stroke problem-DFS

Source: Internet
Author: User

NYoj-42-one stroke problem-DFS
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 subsequent Q rows, each row has two positive integers A and B (0 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
 
   
# Include
      
       
# Include
       
        
# Include
        
         
# Include
         
          
# Include
          
           
# Include
           
             # Include
            
              Using namespace std; int line [1010]; // count the degree of each node int visit [1010]; // records the accessed point int G [1010] [1010]; // record I, j whether to connect int vertex, edge; void DFS (int I) // determine whether to connect {// int v = I; visit [I] = 1; // I accessed for (int v = 0; v <= vertex; v ++) {if (v! = I & G [I] [v] &! Visit [v]) // find one! = I/connect to I/No accessed point DFS (v); // continue to access from this point} int main () {int T, x, y, count, flag; while (~ Scanf ("% d", & T) {while (T --) {count = 0; flag = 1; scanf ("% d", & vertex, & edge); // Number of vertices and edges memset (line, 0, sizeof (line); memset (visit, 0, sizeof (visit); memset (G, 0, sizeof (G); for (int I = 1; I <= edge; I ++) {scanf ("% d", & x, & y); G [x] [y] = G [y] [x] = 1; line [x] ++; // The degree of the point line [y] ++;} DFS (0); for (int I = 0; I <= vertex; I ++) {G [I] [I] = 1; if (! Visit [I]) flag = 0; if (line [I] % 2! = 0) count ++;} if (flag) {if (count = 2 | count = 0) {printf ("Yes \ n ");} else {printf ("No \ n") ;}} else {printf ("No \ n") ;}} 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.