Full
Tips:
Hint: Oralu's decision
Little ho: Good trouble, it is my words to take a few steps, to no road can not go!
Small hi: In that case, the collection of props will be much less, if you want to use later, and have to re-read the document.
Little ho: Well, let me think about it first.
< two minutes after >
Little ho: This seems like a stroke of a question. Hey, we're asking a way to get all the edges out of a stroke?
Little hi: Yes, it's a stroke problem, but it's a more formal name called Euler's road problem. It is defined as
Given the no-orphaned node graph G, if there is a path, once per side of the diagram and only once, the path is called Euler Road.
Little ho: Since there is a name, it proves that this thing has a solution?
Little hi: Yes, Euler's path is conditional: an undirected graph exists Oralu when and only if the graph is connected and there are only 2 points in degrees that are odd, at which point the two points can only serve as the starting and ending points of the Euler path.
If there are no odd points in the graph, then the starting and ending points must be the same point, so the Euler path is called the Euro loop .
For any point, the number of times from other points to it and the number of times from it to other points are necessarily equal, otherwise the number of times and the number of entry will be different. If the number of entry, then the point of the end, if the number of out more than the point is the starting point.
For an out-of-direction graph, the number of incoming and outgoing times is exactly reflected in the amount of degrees. So the odd-numbered point can only have a maximum of 2.
Strict proof of the words:
If figure g is connected, there are 0 or two odd-point nodes, we always have the following method to construct a Euler road:
- If there are two odd number of nodes, then from one of the nodes to construct a trace, that is, from v[0] from the association edge E[1] "into" v[1 ", if the degree of v[1] is even, it will be v[1] and then by the associated side e[2] into the v[2], so go on, each side only take once. Since G is connected, it is necessary to stop at another odd few nodes and get a trace l:v[0]-e[1]-v[1]-e[2]...v[i]-e[i+1]...v[k]. If the G does not have an odd few nodes from any node v[0], the above method must be returned to the node v[0], the above a closed trace L1.
- If the L1 passes all the sides of G, then L1 is Euler's path.
- If the G to get rid of L1 after the sub-figure g′, then the g′ in the degree of each node is even, because the original diagram is connected, so L1 and g′ at least one node V[i] coincident, in g′ from v[i] start repeating the first step of the method, to get closure L2.
- When the L1 is combined with the L2, if it is G, then the Euler road is obtained, otherwise the third step can get the closed trace L3, and so on until a path is obtained from all sides of the graph G.
Consider the previous example:
For this diagram, the number of points numbered 4,5 is odd and the others are even. According to the nature of the above, we know that the starting and ending points must be 4, 5 nodes. Let's start at 4 and draw an edge until there's no way to go:
In this step we connect the 4-5-6-3-2-5. According to Oralu's construction, we got L1. Because L1 does not walk through all sides, so we perform step 3, we can find that for 4 and 2 is the sub-graph G ' coincident points, on the sub-chart we can get L2 (2-4-1-2):
The combination of L1 and L2 constitutes the Euler road.
Little ho: Since this is the nature, I just need to calculate the degree of each point to know if I can walk through all sides.
Little hi: Yes, but don't forget the most important point is that the whole diagram is connected.
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 voidDFS (vector<vector<int> >& Graph,inti,vector<BOOL>& visited,int&CNT)7 {8 if(i>=graph.size ())9 {Ten return; One } A - intSZ =graph.size (); - the if(!Visited[i]) - { -Visited[i] =true; -cnt++; + for(intj =0; J<graph[i].size (); J + +) - { + if(!Visited[graph[i][j]]) A DFS (Graph, Graph[i][j], visited, CNT); at } - } - } - - BOOLIsconnectedgraph (vector<vector<int> >&Graph) - { in intSZ =graph.size (); -vector<BOOL> Visited (SZ,false); to + intCNT =0; -DFS (Graph,0, visited, CNT); the if(cnt<sz) * return false; $ return true;Panax Notoginseng } - the intMain () + { A intN, M; theCin>>n>>M; +vector<int>Edge; -vector<vector<int> >Graph (N, edge); $vector<int> Degree (N,0); $ - intI=M; - while(i--) the { - intA, B;WuyiCin>>a>>b; thea--; -b--; Wu Graph[a].push_back (b); -degree[a]++; Aboutdegree[b]++; $ } - - intCntofodd =0; - A for(intI=0; i<n; i++) + { the if(degree[i]%2==1) -cntofodd++; $ } the if(cntofodd!=2&& cntofodd!=0) thecout<<" Part"; the Else the { - if(Isconnectedgraph (Graph)) incout<<" Full"; the Else thecout<<" Part"; About } the the the return 0; +}
View Code