[Data generator] uva10054 the neck.pdf

Source: Internet
Author: User

At the invitation of teacher Wu, I wrote a data generator.

Currently, this data generator can ensure that the generated data is valid and the efficiency is good. Zyy is just lazy when creating a common connected graph. It concatenates all vertices directly to ensure the graph connection. If you have a good idea, please do not hesitate to advise. zyy is appreciated ~~

 

The following code is used:

1 # include <cstdio> 2 # include <ctime> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <vector> 7 using namespace std; 8 # define maxn 40 9 # define maxm 100 10 11 int A [maxn + 10] [maxn + 10]; 12 INT d [maxn + 10]; 13 14 int N, m; 15 16 int getrand (int l, int R) 17 {18 int Len = r-L + 1; 19 int ret = rand () * rand () % Len + L; 20 return ret; 21} 22 23 inline void addedge (int u, int V, in T o) 24 {25 A [u] [v] + = O; A [v] [u] + = O; 26 d [u] + = O; d [v] + = O; 27 m + = O; 28} 29 30 void Gen () 31 {32 int I, J, K, T; 33 bool again; 34 memset (A, 0, sizeof (a); 35 memset (D, 0, sizeof (d); 36 for (I = 1; I <N; I ++) // ensure that the graph is connected to 37 {38 A [I] [I + 1] ++; A [I + 1] [I] ++; 39 D [I] ++; d [I + 1] ++; 40} 41 for (I = 1; I <= m-n + 1; I ++) // The remaining side is randomly connected to 42 {43 do {44 J = getrand (1, N); 45 k = getrand (1, N); 46 If (j = K) continue; 47 A [J] [k] ++; A [k] [J] ++; 4 8 d [J] ++; d [k] ++; 49 break; 50} while (true); 51} 52 // adjust the edge, make the degrees of all vertices an even number 53 for (I = 1; I <= N; I ++) 54 {55 if (d [I] % 2) // If the I-point is odd, find another 56 {57 for (j = I + 1; j <= N; j ++) point with an odd degree) 58 {59 If (d [J] % 2) 60 {61 if (a [I] [J]) // I and J have 62 {63 If (d [I]! = 1 & D [J]! = 1) // if there are more than one edge between I and j, delete one edge 64 {65 addedge (I, j,-1); 66 break; 67} 68 else 69 {70 continue; 71} 72} 73 else // if there is no edge between I and j, connect I and j 74 {75 addedge (I, j, 1); 76 break; 77} 78} 79} 80 If (j> N) 81 {82 For (k = I + 1; k <= N; k ++) 83 {84 If (d [k]) 85 break; 86} 87 T = K; // find a vertex t 88 for (k = 1; k <I; k ++) // find a vertex K with an even degree. If K is not connected to I or T, connect TK, ik 89 {90 if (! A [T] [k] &! A [k] [I]) 91 {92 addedge (T, k, 1); 93 addedge (I, k, 1); 94 break; 95} 96} 97 again = false; 98 If (k> = I) // you need to re-adjust 99 {100 for (t = 1; t <= N; t ++) // connection I is not connected to any vertex t101 {102 If (! A [T] [I] & I! = T) 103 {104 addedge (t, I, 1); 105 again = true; 106 break; 107} 108} 109 If (T> N) // if such a t cannot be found, delete an edge 110 {111 for (t = 1; t <= N; t ++) when the graph is connected) 112 {113 If (d [T]> 1) 114 {115 addedge (t, I,-1); 116 again = true; 117 break; 118} 119} 120} 121} 122 If (again) 123 I = 0; 124} 125} 126} 127 128 129 130 int main () 131 {132 // freopen ("data4.in", "W", stdout); 133 srand (Time (null); 134 int CAS = 10000; // getrand (1, 1); 135 int I, j; 136 printf ("% d \ n", CAS); 137 while (CAS --> 0) 138 {139 N = getrand (maxn/5*3, maxn); 140 m = getrand (maxm/5*3, maxm); 141 Gen (); 142 printf ("% d \ n", m); 143 for (I = 1; I <n; I ++) 144 for (j = I + 1; j <= N; j ++) 145 while (A [I] [J] --> 0) 146 printf ("% d \ n", I, j ); 147} 148 return 0; 149}
View code

 

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.