#296 (Div.2) E. Data Center Drama

Source: Internet
Author: User
Tags in degrees

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 &lt ; = 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

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.