"Programming Marathon" "019-a stroke"

Source: Internet
Author: User
Tags in degrees

"Programming Marathon algorithm Directory" " 019-A Stroke" "Project Download >>>" 1 Topic Description

Let's play a game, the rule is this: there is a connected graph, can find one that exactly contains all the edges, and there is no duplicate path.

1.1 Input Description:

The input contains multiple sets of data. The first row of each set of data contains two integers n and m (2≤n, m≤1000), where n is the number of vertices and M is the number of edges. followed by M-line, each line contains two integers from and to (1≤from, to≤n, from! = To), which represent both ends of the edge. The edges are bidirectional, and there may be more than one edge between the two vertices.

1.2 Output Description:

corresponding to each set of inputs, if one stroke can output "Yes", otherwise output "No".

1.3 Input Example:
3 31 22 31 34 71 22 11 31 41 42 34 3
1.4 Output Example:
YesNo
2 ideas for solving problems

The topic asks whether a connected graph can be finished with a stroke. This is a workable problem , that is, a vertex does not iterate through all the edges and back to the starting vertex, which is the Euler loop . Before you answer this question, let's introduce the relevant content of the Euler loop.

2.1 Euro-Pull circuit 2.1.1 Euler pathway, Eulerian graph circuit,

  no-show diagram:
1) Set G is a connected undirected graph, it is said that through the g of each edge once and only once the path is Euler path;
2) If the Oraton road is a loop (the starting and ending points are the same vertex), then this loop is called the Euler circuit (circuit);
3) The non-direction graph G with a Euler loop is called Eulerian graph (Euler graph).
to map:
1) Set D is the direction graph, the base diagram of D is connected, it is said that each edge of D is once and only once of the direction of the direction of the Euler path;
2) If there is a forward loop to the Oraton road, it is said that there is a forward loop to the Euler circuit (directed Euler circuit);
3) A direction diagram D with a forward Euler loop is called a Eulerian graph (directed Euler graph).
Figure 1 is a forward graph.

Fig. 1 The graph of the direction and the non-direction

2.1.2 Theorem and inference

The determination of Euler's path and Euler's circuit is very simple, see the following theorem and inference.
  Theorem 2.1 the necessary and sufficient conditions for the existence of Euler pathways in the non-direction graph G are:
G is a connected graph, and G has only two singular nodes (vertices with an odd degree) or no singularity nodes.
  Corollary 2.1:
1) When G is a connected graph with only two singular nodes, the Euler path of G must be the end point of the two nodes.
2) When G is a connected graph with no singularities, G must have a Euler loop.
3) The sufficient and necessary condition of G for Eulerian graph (presence of the Euler circuit) is that the G is a connected graph with no singularity junction.
Example 1 (a) shown in the graph, there are two singularities vertex v2 and V5, so there is a Oraton path, and Euler path must be the two vertices as the starting vertex and the terminating vertex, the graph does not exist the Euler loop. Figure 2-1 (b) shows that the graph is Eulerian graph.
  theorem 2.2 the necessary and sufficient conditions for the existence of Euler pathways to graph D are:
D is connected to the base of the graph, d, and all vertices are equal in the degree of the degree of the degrees, or in addition to the two vertices, the rest of the vertices are equal to the degree of the degrees, and the difference between the degrees of one vertex in the two vertices is 1, and the difference between the degree and the degree of the other vertex is-
  Corollary 2.2:
1) When d in addition to the difference between the degree of 1,-1 two vertices, the rest of the vertices of the degree and the degrees are equal, the d of the direction of the Euler path must be out, the difference between the degrees of 1 as the starting point, with the difference between the degree of 1 of the vertex as the endpoint.
2) When all the vertices of D are equal in the out and in degrees, there is a direction to the Euler loop in D.
3) There is a sufficient and necessary condition for the direction graph D to be Eulerian graph that the base graph of D is a connected graph, and all vertices have equal out and in degrees.
Example 1 (c) shown in the direction of the graph, Vertex v2 and V4 in and out of the degree are 1; the v1 of the vertex is 2, the degree is 1, the difference is 1, the V3 of vertex is 1, the degree is 2, the difference is 1; so the graph only exists to the Oraton road, and must be the vertex v1 as the starting point, with vertex v3 as the endpoint. The direction diagram shown in Figure 1 (d) does not exist to the Oraton road.

2.2 Steps to solve problems

First, according to the adjacency matrix of the input construction diagram, the adjacency matrix to determine whether the diagram is connected, not connected to the description can not be a stroke, if connected, and then judge whether there is a singularity vertex, there can not be a stroke finish, there is no indication of a stroke finish.

3 Algorithm Implementation
Import Java.util.arraylist;import Java.util.list;import Java.util.Scanner;/** * Author: Wang Junshu * time:2016-05-12 09:04 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public classMain { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (System.inch);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data3.txt"));         while(Scanner.hasnext ()) {intn = scanner.nextint ();intm = Scanner.nextint ();//Record Edge            int[] Edge =New int[M *2]; for(inti =0; i < edge.length;            i++) {Edge[i] = Scanner.nextint (); }if(Draw (n, Edge)) {System. out. println ("Yes"); }Else{System. out. println ("No");    }} scanner.close (); }/** * Whether the graph can finish the stroke (to determine if there is no Oraton path) * * @param n vertex number, vertex number from 1 to n * @param edge edge of the connection array, the two represent an edge * @re Turn true: You can finish a stroke, false: can not finish a stroke * *    Private StaticBooleanDraw(intNint[] edge) {int[] vertex =New int[n +1];//Count the degrees of each vertex         for(intI:edge) {vertex[i]++; }///////////////////////////////////////////////////////////////////////////////////////////        //undirected graph G the necessary and sufficient conditions for the existence of Euler pathways are: g is a connected graph, and G has only two singular nodes (vertices with an odd degree of degrees) or no singularity nodes.         ///////////////////////////////////////////////////////////////////////////////////////////        //Statistical singularity Vertex Count        intCount =0; for(inti =1; i < vertex.length; i++) {if(Vertex[i]%2!=0) {count++; }        }//The singularity vertex is not 0 and No 2 indicates that there is no Oraton road        if(Count! =0&& count! =2) {return false; }//adjacency matrix of construction edges        int[] Graph =New int[n +1][n +1]; for(inti =0; i < edge.length; i + =2) {intv = edge[i];intW = edge[i +1];            graph[v][w]++;        graph[w][v]++; }//Clear the top entry mark, use it as an access token, 0 means no access, 1 indicates access         for(inti =0; i < vertex.length; i++) {vertex[0] =0; } list<integer> List =NewArraylist<> (n);//have a graph connected, you can access other vertices from any vertex        //Access from the first vertex, with breadth-first traversalvertex[1] =1; List.add (1); while(!list.isempty ()) {intv = list.remove (0); for(inti =1; I <= N; i++) {//Side (V, i), T is 0 description v cannot be directly to I                intt = Graph[v][i];//if (V, i) reachable, and vertex i is not accessed, the tag has been accessed and added to the access queue                if(t! =0&& Vertex[i] = =0) {Vertex[i] =1;                List.add (i); }            }        } for(inti =1; i < vertex.length; i++) {///There is no access to the vertex, the illustration is not connected            if(Vertex[i] = =0) {return false; }        }return true; }}
4 Test Results

5 Other information

Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon" "019-a 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.