Topic 1: Oralu Shi
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
In the last back small hi and small ho control the protagonist gathered scattered on the wooden bridge props, these props are actually a piece of dominoes.
The protagonist continued to move forward, a stone bridge appeared in front of the stone bridge at the end of a fire wall, it seems unable to pass.
Little Hi noticed that there was a small piece of paper at the bridge, so the controlling protagonist picked up the paper, and saw it read:
The flame wall can be closed by placing the M-block dominoes in the concave of the stone bridge. Remember that dominoes need the same number to connect.
--by unknown adventurer.
Small hi and Small ho opened the main character of the props bar, found that the protagonist happens to have M fast Domino.
Small ho: That means to put all the dominoes in the groove to close the flame wall, what does the number mean?
Little hi: You see, each piece of dominoes has a number at each end, presumably only when the number is the same, can be placed together, such as:
Little ho: So, let's see if we can connect all the dominoes together.
Hint: Fleury algorithm to find Euler path
Input
Line 1th: 2 positive integers, n,m. Indicates the maximum number and number of dominoes that appear on the dominoes, respectively. 1≤n≤1,000,1≤m≤5,000
2nd.. M+1 lines: 2 integers per line, u,v. Line I+1 represents the number (U,V) at both ends of the block I, 1≤u,v≤n
Output
Line 1th: m+1 numbers, indicating the number after the end of a domino
For example, the status of the Domino connection is (1,5) (5,3) (3,2) (2,4) (4,3), then output "1 5 3 2 4 3"
You can output any set of legitimate solutions.
样例输入5 53 53 24 23 45 1样例输出1 5 3 4 2 3
Problem Solving Ideas:
Here we use a adjacency matrix to store the connection information of the graph, through the DFS to search the graph, the algorithm's stack path is that we need the Euler road
Some problems encountered in the process of problem solving:
1. The problem is to allow the re-edge, that is, there may be multiple identical dominoes, thus, in resetting the edges in the adjacency matrix, it is not easy to use
g_bridge[x][y] = 0; g_bridge[y][x] = 0;
And should use the following form:
g_bridge[x][y] += 1; g_bridge[y][x] += 1;
2. Only the stack order can form the Euler path we want, the stacking order is not, when the test data looping, the algorithm involves backtracking problems, will produce the wrong output.
Here you can give a set of test data:
When you use this set of data for testing, the sequential output of the stack is clearly not true
Source:
//================== "The determination of the" Euler's Road "===============//@ input//Line 1th: 2 positive integers, N, M. Indicates the maximum number and number of dominoes that appear on the dominoes, respectively. 1≤n≤1, 000,1≤m≤5,//2nd. M + 1 lines: 2 integers per line, u, v. The line I + 1 represents the number of the two ends of the Block I (U, v), 1≤u, V≤n//@ output//Line 1th: M + 1 digits, indicating the number after the domino is connected//For example, the status of the Domino connection is (1, 5) (5, 3) (3, 2) (2, 4) (4, 3), then output "1 5 3 2 4 3"//You can output any set of valid solutions. //================== "End of Oralu judgment" ===============#include <iostream>#include <string.h>using namespace STD;#define N_MAX 1020#define M_MAX 5020intN =0;intM =0;intG_bridge[n_max][n_max] = {0};intG_degree[n_max] = {-1};intG_res[m_max] = {0};intIter_num =0;voidInit () {Cin>> N >> M;if(N <1|| N > +|| M <1|| M > the) {Cerr<<"Error Input"<< Endl;Exit(0); }intx =0;inty =0; for(inti =0; I! = M; i++) {Cin>> x >> y; G_bridge[x][y] + =1; G_BRIDGE[Y][X] + =1; G_DEGREE[X] + =1; G_degree[y] + =1; }}intFindstartend () {intid[2] = {1};intId_iter =0; for(inti =1; I! = N +1; i++) {if(G_degree[i]%2==0)Continue;if(Id_iter = =2) {Cerr<<"Error occurred"<< Endl;Exit(-1); } id[id_iter++] = i; }returnid[0];}voidSearchintStartid) {if(Iter_num >= M +1)return;//g_res[++iter_num] = Startid; for(inti =1; I! = N +1; i++) {if(G_bridge[startid][i] >0) {G_bridge[startid][i]-=1; G_bridge[i][startid]-=1; Search (i); }} G_res[++iter_num] = Startid;}intMain () {init ();intStartid = Findstartend (); Search (Startid); for(inti =1; I! = M +2; i++) {cout<< G_res[i] <<" "; }cout<< Endl;return 0;}
Resources:
1. How does the DFS algorithm guarantee the correctness of the order, without reading the explanation, seeking the great God solution!
2. The question of Euler road two is probably the same dominoes.
Hihocoder 50th Zhou C implementation method