One stroke (nyoj42) (and query set + European Union), nyoj42 European Union
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
-
Source
-
[Zhang yuncong] original
-
Uploaded
Zhang yuncong
The important nature of the European Union:
A graph with Euler's loop is the Euclidean diagram, which is a connected graph with a loop,
And this loop passes through each edge in the graph only once.
The nature of the European region:
(1). the undirected connected graph G is a European region. if and only when G does not contain an odd number of nodes (the degrees of all nodes in G are an even number );
(2). undirected connected graph G contains Euler's path. if and only when G has zero or two odd-degree nodes;
(3). A directed connected graph D is a European region. if and only when the graph is a connected graph and each node in D is inbound = outbound
(4). A directed connected graph D contains Euler's path. if and only when the graph is a connected graph and except two nodes in D, the inbound degree of each node is equal to the outbound degree,
And the two points meet the requirements of deg-(u)-deg + (v) = ± 1. (The inbound degree of start point s = outbound degree-1, the outbound degree of end point t = inbound degree-1 or the inbound degree of two points = outbound)
This question can be solved by the second nature.
In two steps:
(1) determine the connectivity (dfs or query the Set );
(2) count the number of singularity.
This question is easy to understand and query:
-
#include<stdio.h>#include<string.h>int pre[1002];int degree[1002];int find(int x){return pre[x]==x?x:find(pre[x]);}void join(int x,int y){x=find(x),y=find(y);if(x!=y){ if(x>=y) pre[x]=y; else pre[y]=x;}return;}int main(){ int test,i,n,m,t,k; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); memset(degree,0,sizeof(degree)); for(i=1;i<=n;i++) pre[i]=i; for(i=1;i<=m;i++) { scanf("%d %d",&t,&k); join(t,k); degree[t]++; degree[k]++; } int l=0,odd=0; for(i=1;i<=n;i++) { if(find(i)!=1) { l=1; break; } if(degree[i]&1) odd++; } if(l) printf("No\n"); else { if(odd==0||odd==2) printf("Yes\n"); else printf("No\n"); } } return 0;}