1. Title Description: Click to open the link
2. Problem-Solving ideas: The issue requires each point of the direction of the two roads are the same, if such a path is incomplete, you need to add as few edges as possible to satisfy the condition. In fact, the test is Euler circuit, and the European pull circuit must be an even number of paths. The existence of the Euler circuit is a condition that the degree of the odd number of nodes can not exceed two, but the subject must not exist in the degree of odd points, otherwise it is impossible to meet test instructions.
So all nodes must have an even number of degrees, and if two points are found to be odd in degrees, then connect them together. If you have an even number of degrees and an odd amount of edges, add a self-loop so that the total number of paths is even.
Next, from the starting point, looking for a European pull circuit, (again: Euler circuit is all the path is only go once, where the nodes may be repeated access), in the output, only need two a group, the first sequentially output, the second output in reverse order.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 100005int Out[n], Deg[n];queue <pair<int, int> >q[n];bool vis[n * 3];struct a{int A, B;} Ans[3 * n];int top;void Euler (int u) {int I, v;pair<int, Int>nw;while (!q[u].empty ()) {NW = Q[u].front (); Q[u].pop (); v = Nw.first;if (Vis[nw.second]) Continue;vis[nw.second] = 1;euler (v);//recursive end of the found edge is added to the stack, the end of the stack is near the end of the path, the top of the stack is near the beginning of the path ans[++ top].a = u;ans[top].b = V;}} int main () {//freopen ("T.txt", "R", stdin); int n, M, I, A, b;scanf ("%d%d", &n, &m); int number = 0;for (i = 1; I < ; = m; i++) {scanf ("%d%d", &a, &b), Q[a].push (Make_pair (b, ++number)), Q[b].push (Make_pair (A,Number));d eg[a]++; deg[b]++;//statistic degrees}int last = -1;for (i = 1; I <= n; i++) {if (Deg[i] & 1) {if (last = =-1) last = I;//last indicates that the first degree found is The odd node else//finds a point with an odd number of degrees, connects it to the last found point {q[last].push (Make_pair (i, ++number)), Q[i].push (Make_pair (last, number)); last = -1;deg[last]++; deg[i]++;}}} if (number & 1)//The total edge count is odd, plus a self-ring {Q[1].push (Make_pair (1, ++number));} Euler (1);//starting from the starting point, look for a Euler loop cout << top << endl;for (i = 1; I <= top; i++) {if (I & 1) printf ("%d%d\n", ans [I].a, ans[i].b), Else printf ("%d%d\n", ans[i].b, ANS[I].A);} return 0;}
#296 (Div.2) E. Data Center Drama