LG2731 Riding Fence Riding the Fences

Source: Internet
Author: User

Tag: is the map optimize the = = Definition list Get complex

Test instructions

John is a man as lazy as the other peasants. He hates riding, so he never passes a fence twice. You have to make up a program, read the description of the fence network, and figure out a path to fix the fence so that every fence happens once. John can start horseback riding from any vertex (that is, the intersection of two fences), ending at any one vertex.

Each fence connects two vertices, and vertices are labeled 1 to 500 (although some farms do not have 500 vertices). Any number of (>=1) fences can be connected at one vertex. There may be multiple fences between the two vertices. All fences are connected (i.e. you can reach all other fences from any fence).

Your program must output the path of the ride (indicated by the vertex number that passes through the road). If we think of the output path as a number of 500, then when there are multiple sets of solutions, the smallest of the output 500 binary notation (that is, the output of the first bit smaller, if there are more than one set of solutions, the output of the second smaller, and so on).

Input data is guaranteed to have at least one solution.

\ (n \leq 500,m \leq 1024\)

Analysis

Euler circuit template problem.
Good blog

For the hierholzers algorithm, the assumption is that the graph G exists in the Euler circuit, that is, the direction of the graph at any point of the same degree. From any starting point V to traverse, until the point V to arrive again, that is, to find a ring, which will ensure that the point V can be reached, because the traverse to any point u, because of the same degree and in the degree of the same, so you must have an edge, so must be able to reach v. This ring is defined as C, if there is a point x in the ring C, its out side is not in the ring, then continue with this point x begins to traverse the look for the ring C ', the ring C, C ' Join is also a large ring, so reciprocating, until all the edges in Figure g have been added to the ring.

Code
#include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <string> #include <vector> #include <list> #include <deque># include<stack> #include <queue> #include <map> #include <set> #include <bitset> #include <algorithm> #include <complex> #pragma GCC optimize ("O0") using namespace Std;template<class t> inline    T Read (t&x) {T data=0;    int w=1;    Char Ch=getchar ();        while (!isdigit (CH)) {if (ch== '-') w=-1;    Ch=getchar ();    } while (IsDigit (CH)) data=10*data+ch-' 0 ', Ch=getchar (); return x=data*w;} typedef long LONG ll;const int inf=0x7fffffff;const int maxn=1100;multiset <int> to[maxn];int deg[maxn];int road[ma    xn],top;void dfs (int x) {//cerr<< "dfsing" <<x<<endl;        for (auto I=to[x].begin (); I!=to[x].end (); I=to[x].begin ()) {int y=*i;        To[x].erase (i); To[y].erase(To[y].find (x));    Edit 1 dfs (y); } road[++top]=x;}    int main () {//Freopen (". In", "R", stdin);//Freopen (". Out", "w", stdout);    int m;    Read (m);        for (int i=1;i<=m;++i) {static int A/b;        Read (a); Read (b);        ++DEG[A],++DEG[B];        To[a].insert (b);    To[b].insert (a);    } int s=-1,e=-1;            for (int i=1;i<=500;++i) if (deg[i]&1) {if (s==-1) s=i;            else if (e==-1) e=i;                else {puts ("-1");            return 0;    }} if (S==-1) S=1;    DFS (s);    for (; top;--top) printf ("%d\n", Road[top]);//fclose (stdin);//fclose (stdout); return 0;}

LG2731 riding Fence Riding the Fences

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.