Problem 2 travel plans (travelling .cpp) --- 2019.10.6

LTH tql, lzpclxf tql Barley

Problem 2 travel plans (travelling.cpp)
[title] Description
small Z intend to take advantage of the summer, opened his travel plans. But the difference is that with other students, a small brigade Z
rows do not care which reached net red spots which played cards. Small Z is more concerned about the scenery along the road, and
small Z that, despite repeated arrive at the same place, but when the road to if not the same, is not a good
flavor.
Small Z prepared a map, gives N small Z favorite city on the map are numbered
1 ... N, and M strip road connecting the two cities, number 1 ... M. Small Z M intends to go all over the road and
only once, but he found such a route may not exist. He intends, when he went to a city
after the city discovered the road from the departure city he has gone through, he will fly to another city,
then continue his journey.
Now small Z want to know, at best route plan, he had to take at least how many times the aircraft.
[Input format
number of the first set of test data behavior T (1≤T≤10).
The number of cities and roads N number of the first acts of each test data M.
Next M lines of two integers x and y, x represents a connected two-way cities and urban y of
road.
[Format] output
for each test case, the output of the first line contains an integer K, Z represents at least a little to take a number of
times aircraft.
The next line K + 1, the output of the i-th row of the small Z i paragraph stroke. When the i-th paragraph after stroke x number of channel
paths, the first output x, and then outputs the integers x, respectively, through the route number of the road. if
By way forward Article i, i output, otherwise the output -i.
If plural sets of programs, a group of any output.
Sample input] [
. 1
. 3. 3
. 1 2
. 1. 3
2. 3
[output] Sample
0
. 3. 1. 3 -2

 

 

 (Copy down, buckle down, right)

 

Each block communicates clearly separate. For a communication block (except a single point), if the number of singular points of degree k,

At least then max (k / 2,1) paths. This is because an undirected graph, the two degrees of odd

Even up point, two odd points can be eliminated. So if you have a non-Euler (all point to a degree view of the whole

Is an even number), just add k / 2 edges on it . Consequently, we just become a communication block Euler, a search

Get over the path, and then we add the human side deleted, and the rest is the communication block "stroke" the. Here

What must become Euler Euler rather than through road, in fact, The Euler Euler paths and circuits are possible, too

The answer is the same. The main algorithm is the problem, if that becomes a Euler path, we will get from the rest of the

Two odd point of view, is terminated. As to why this result plus k / 2 edges swept out deleted Dorcas side is the answer

Case, and the answer = max (k / 2,1) it? This is because a figure, we started to go from an odd point, also stop

It must be in the odd point, and then again passes out of an article will not be repeated similarly to the path from the front of the drawing (the odd start point

Odd ending point) and then we will be these paths connecting the start to the end of the next path (Dorcas side), so and because

Start and end point of each path are odd point, an odd edge deleting two points. So eventually put them together into the number of edges needed to Euler tour is k / 2.

 

 

1  // Unicom block Euler ,, +
 2  // note is odd or even number of paths
 3  // see if may be formed Euler
 4  // slightly modified in FIG Euler template 
. 5 #include <the iostream>
 . 6 #include <cstdio>
 . 7 #include <Vector>
 . 8 #include <CString>
 . 9  the using  namespace STD;
 10  const  int N = 100005 ;
 . 11 Vector < int > ANS [N];
 12 is  int T, n-, m, = TOT . 1 , head [N], VIS [N], in [N], CNT;
 13 is  struct Node {
14     int to,nex,flag,id;
15 } a[N>>2];
16 inline int read() {//快读
17     int x=0,f=1;
18     char ch=getchar();
19     while(ch<'0'||ch>'9') {
20         if(ch=='-')f=-1;
21         ch=getchar();
22     }
23     while(ch>='0'&&ch<='9')
24         x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
25     return x*f;
26 }
27 void add(int x,int y,int id) {//加边
28     a[++tot].to=y;
29     a[tot].nex=head[x];
30     a[tot].flag=0;
31     a[tot].id=id;
32     head[x]=tot;
33 }
34 void dfs(int x) {
35     vis[x]=1;
36     for(int i=head[x]; i; i=a[i].nex) {
37         int y=a[i].to,emm=a[i].flag;
38         if(!emm) {
39             a[i].flag=a[i^1].flag=1;
40             dfs(y);
41             if(a[i].id)ans[cnt].push_back(-a[i].id);
42             else cnt++;
43         }
44     }
45 }
46 int main() {
47     freopen("travelling.in","r",stdin);
48     freopen("travelling.out","w",stdout);
49     T=read();
50     while(T--) {
51         for(int i = 1; i <= cnt; i ++) ans[i].clear();
52         tot=1,cnt=0;
53         memset(head,0,sizeof(head));
54         memset(vis,0,sizeof(vis));
55         memset(in,0,sizeof(in));//多组数据注意清空数组
56         n=read();
57         m=read();
58         for(int i=1,x,y; i<=m; i++) {
59             x=read();
60             y=read();
61             add(x,y,i);
62             add(y,x,-i);
63             in[x]++,in[y]++;
64         }
65         int now=0;
66         for(int i=1; i<=n; i++)
67             if(in[i]%2==1) {
68                 if(now) {
69                     add(i,now,0);
70                     add(now,i,0);
71                     now=0;
72                 } else now=i;
73             }
74         for(int i=1; i<=n; i++)
75             if(!vis[i]&&in[i]%2) {
76                 cnt++;
77                 dfs(i);
78                 cnt--;//回溯
79             }
80         for(int i=1; i<=n; i++)
81             if(!vis[i]&&in[i])
82                 cnt++,dfs(i);
83         //输出
84         printf("%d\n",cnt - 1);
85         for(int i=1; i<=cnt; i++) {
86             printf("%d ",ans[i].size());
87             for(int j=0; j<ans[i].size(); j++)
88                 printf("%d ",ans[i][j]);
89             printf("\n");
90         }
91         printf("\n");
92     }
93     fclose(stdin);
94     fclose(stdout);
95     return 0;
96 }
View Code

 

Guess you like

Origin www.cnblogs.com/ydclyq/p/11627627.html