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
The following conditions must be met: 1. The graph is connected.
2. The odd degree node of the graph is 0 or 2.
DFS traversal is used here. Because a two-dimensional array is used to record graphs, it may cause a lot of unnecessary access and judgment. The efficiency is not very high, but it is still a. We recommend that you use the adjacent Table Record graph.
1 #include<stdio.h> 2 bool visited[1005] = {false}; 3 int dotin[1005] = {0}; 4 int graph[1005][1005]; 5 void dfs(int k, int n) 6 { 7 visited[k] = true; 8 int i,j; 9 for (i=k; i <= n; i++)10 {11 for (j=1; j <= n; j++)12 if (graph[k][j] != 0){13 if (!visited[j])14 dfs(j,n);15 }16 }17 }18 int main()19 {20 int n,p,q,a,b,i,j,dotnum = 0;21 bool yes = true;22 scanf("%d",&n);23 for (i=1; i < 1001; i++)24 for (j=1; j < 1001; j++)25 graph[i][j] = 0;26 while (n--)27 {28 scanf("%d%d",&p,&q);29 for (i=0; i < q; i++)30 {31 scanf("%d%d",&a,&b);32 graph[a][b] = 1;33 graph[b][a] = 1;34 dotin[a]++;35 dotin[b]++;36 }37 dfs(1,p);38 for (i=1; i <= p; i++)39 if (!visited[i])40 {41 yes = false;42 break;43 }44 for (i=1; i <= p; i++)45 if (dotin[i]%2)46 dotnum++;47 if ((yes && dotnum == 2) || (yes && dotnum == 0))48 printf("Yes\n");49 else50 printf("No\n");51 yes = true;52 dotnum = 0;53 for (i=1; i <= p; i++)54 dotin[i] = 0;55 for (i=1; i <= p; i++)56 visited[i] = false;57 for (i=1; i <= p; i++)58 for (j=1; j <= p; j++)59 graph[i][j] = 0;60 }61 }
Nyoj 42 stroke