CCF Sixth Computer Professional certification the fourth item receiving STL application of dynamic storage and Fleury algorithm

Source: Internet
Author: User

Problem description in order to increase the company's revenue, F Company opened a new logistics business. Due to the good reputation of f company in the industry, the logistics business has been welcomed by consumers, logistics business immediately throughout the city every street. However, Company F now only arranges for xiaoming to be responsible for all street services.
Although the task is heavy, but Xiaoming has enough confidence, he got the map of the city, ready to study the best plan. There are n intersections in the city, and M-streets are connected between these intersections, and the first and last of each street is connected to an intersection. Streets do not intersect other streets in other locations, except for the ends of the streets. At least one street is connected to each intersection, and some intersections may be connected to only one or two streets.
Xiao Ming wants to design a plan, starting from the intersection numbered 1, every time must go along the street to the other end of the street intersection, and then from the new junction to go down a junction, until all the streets have passed the exact time. Input format the first line of input contains two integers n, m, indicating the number of intersections and number of streets, intersections from 1 to n marking.
Next m line, each line two integers a, b, and labeled a intersection and labeled B intersection between a street, street is two-way, xiaoming can from either end to the other end. There is a maximum of one street between the two intersections. Output format If Xiaoming can go through each street exactly once, then the output line contains m+1 integers p1, p2, p3, ..., pm+1, which indicates the order of the intersections that Xiaoming passed, separated by a space between two integers. If there are a variety of scenarios to satisfy the conditions, then the output dictionary order of the least one scheme, that is, first of all to ensure the minimum P1, p1 the smallest premise to ensure P2 minimum, and so on.
If the scenario does not exist so that xiaoming passes through each street exactly once, output an integer-1. Sample Input 4 5
1 2
1 3
1 4
2 4
3 4 Sample Output 1 2 4 1 3 4 example shows the map of the city and the path of xiaoming as shown.
Sample Input 4 6
0 S
1 3
1 4
2 4
3 4
2 3 Sample Output-1 example shows a map of the city as shown, there is no path that satisfies the criteria.
The evaluation use case scale and the 30% evaluation use case satisfies: 1≤n≤10, n-1≤m≤20.
The first 50% evaluation cases meet: 1≤n≤100, n-1≤m≤10000.
All evaluation cases meet: 1≤n≤10000,n-1≤m≤100000.
RT can easily think of Oraton Road (Circuit) Solution method: Fleury algorithm. The algorithm can guarantee the minimum output of the answer by the dictionary order, but one important problem is that the common adjacency matrix stores such a large amount of data and must be hyper-memory. At this point many C + + (including me) beginners are stuck here: found that the adjacency matrix can not be stored, so that the algorithm is wrong to think about the other algorithms. However, as long as you think about it, you will find that: as long as the unused space in the adjacency matrix is released, we can greatly compress the stored adjacency matrix. In this case, some of the dynamic storage containers in the STL are very useful. You can use a container such as map set. I'm using set. And here's My Code:

#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stack>
#define CLR (x) memset (x,0,sizeof (x))
using namespace Std;
Multiset<int> a[10001];
Stack<int> Stacki;
int b[100002];
Int PL;
void Dfs (int x);
void Flueny (int ss);

int main ()
{
int n,m,k,l;
scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++)
{
scanf ("%d%d", &k,&l);
A[k].insert (l);
A[l].insert (k);
}
int num=0;
for (int i=1;i<=n;i++)
if (A[i].size ()%2==1)
num++;
if (num==0 | | (num==2 && a[1].size ()%2==1))
{
Flueny (1);
for (int i=b[0];i>=1;i--)
printf ("%d", b[i]);
printf ("\ n");
}
Else
{printf (" -1\n");

}
return 0;
}
void Flueny (int ss)
{
Stacki.push (ss);
b[0]=0;
while (!stacki.empty ())
{
if (A[stacki.top ()].empty ())
{
B[++b[0]]=stacki.top ();
Stacki.pop ();
}
Else
{
Pl=stacki.top ();
Stacki.pop ();
DFS (PL);
}
}
return;
}
void Dfs (int x)
{
Stacki.push (x);
if (!a[x].empty ())
{
Pl=*a[x].begin ();
a[x].erase (PL);
A[pl].erase (x);
DFS (PL);
}
return;
}

CCF Sixth Computer Professional certification the fourth item receiving STL application of dynamic storage and Fleury algorithm

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.