nyoj-42-a stroke problem-dfs

Source: Internet
Author: User

Time limit for a stroke problem: theMs | Memory Limit:65535KB Difficulty:4
Describe

Zyc like to play some small games since childhood, including painting a stroke, he would like to ask you to help him write a program, judge whether a figure can use a stroke down.

Rules, all sides can only draw once, can not repeat the painting.

Input
the first line has only one positive integer N (n<=10) that represents the number of groups of test data.
The first line of each set of test data has two positive integer p,q (p<=1000,q<=2000), representing how many vertices and lines are in the picture. (number of points from 1 to p)
The subsequent Q line has two positive integers, a, a, B (0<a,b<p) for each line, indicating that there is a connection between the two points numbered a and a.
Output
If there is a qualifying connection, output "Yes",
If there are no qualifying lines, output "no".
Sample input
24 31 21 31 44 51 22 31 31 43 4
Sample output
NoYes
 
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream   > #include <algorithm> #include <queue> #include <stack>using namespace Std;int line[1010];   Statistic the degree of each node int visit[1010]; Record the visited point int g[1010][1010]; Record whether i,j is connected to int vertex,edge;    void DFS (int i)//determine connectivity {//int v=i;visit[i]=1; I have visited for (int v=0;v<=vertex;v++) {if (v!=i && g[i][v] &&!visit[v])//Find one! =i/With I connect/no access point DFS (v);//Continue from this point down access}}int main () {int t,x,y,count,flag;while (~scanf ("%d", &t)) {while (t--) {Coun T=0;FLAG=1;SCANF ("%d%d", &vertex,&edge); Number of vertices and number of sides memset (line), memset (visit,0,sizeof (visit)), memset (g,0,sizeof (G)); for (int i=1;i<=edge; i++) {scanf ("%d%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;}


nyoj-42-a stroke problem-dfs

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.