Guest User

Untitled

a guest
Jan 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class ps6gen {
  5. public static Random random = new Random();
  6.  
  7. static int parent[][];
  8. static int par_size[];
  9. static Vector<Vector<Integer>> adj;
  10.  
  11. public static void update(int from){
  12.  
  13. for (int t = 0; t < adj.get(from).size();t++){
  14. boolean check = false;
  15. int to = adj.get(from).get(t);
  16. for (int y = 0; y < par_size[from];y++){
  17. if (!contain(to,parent[from][y])){
  18. parent[to][par_size[to]] = parent[from][y];
  19. par_size[to]++;
  20. check = true;
  21. }
  22. }
  23. if (!contain(to,from)){
  24. parent[to][par_size[to]] = from;
  25. par_size[to]++;
  26. check = true;
  27. }
  28. if (check == true)
  29. update(to);
  30. }
  31. }
  32.  
  33. public static boolean contains(int cur, int next){
  34. //System.out.println(cur+" "+next);
  35. if (cur == next)
  36. return true;
  37. for (int i = 0; i < par_size[cur]; i++){
  38. if (contains(parent[cur][i],next))
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. public static boolean contain(int cur, int next){
  45. for (int i = 0; i < par_size[cur]; i++){
  46. if (parent[cur][i] == next)
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. public static void main(String args[]){
  53. int Vcount = 6000;
  54. int Ecount = 30000;
  55. int Wlimit = 9;
  56. adj = new Vector<Vector<Integer>>();
  57. for (int a = 0; a < Vcount; a++)
  58. adj.add(new Vector<Integer>());
  59.  
  60. Vector<Integer> spanning = new Vector<Integer>();
  61. parent = new int[Vcount][];
  62. par_size = new int[Vcount];
  63. for (int h = 0; h < Vcount; h++){
  64. parent[h] = new int[Vcount];
  65. par_size[h] = 0;
  66. }
  67.  
  68. boolean visited[] = new boolean[Vcount];
  69. for (int y = 0; y < Vcount; y++)
  70. visited[y] = false;
  71. visited[0] = true;
  72. spanning.add(0);
  73. int add = 0;
  74. boolean full = false;
  75. for (int i = 0; i < Ecount; i++){
  76. /*
  77. System.out.println(i+"-----");
  78. for (int o = 0; o < Vcount; o++ ){
  79. System.out.println("----->"+o+", "+par_size[o]);
  80. for (int x = 0; x < par_size[o];x++)
  81. System.out.printf("%d ", parent[o][x]);
  82. System.out.println("");
  83. }
  84. System.out.println("-----");
  85. */
  86. int cur = spanning.get(random.nextInt(spanning.size()));
  87. int to = random.nextInt(Vcount);
  88. //System.out.printf("From: %d, To: ",cur);
  89. while ((cur == to)||contain(cur,to)||
  90. (adj.get(cur).contains(((Integer)to)))||
  91. (adj.get(to).contains(((Integer)cur)))){
  92. cur = spanning.get(random.nextInt(spanning.size()));
  93. to = random.nextInt(Vcount);
  94.  
  95. int ra = random.nextInt(10);
  96. if (ra < 3)
  97. to = random.nextInt(Vcount);
  98. else{
  99. if (!full){
  100. boolean found = false;
  101. for (int w = 0; w < Vcount; w++){
  102. if (!visited[w]){
  103. to = w;
  104. found = true;
  105. break;
  106. }
  107. }
  108. if (found == false)
  109. full = true;
  110. }
  111. else
  112. to = random.nextInt(Vcount);
  113. }
  114.  
  115.  
  116. }
  117. //System.out.println("Adding edge "+cur+" "+to+", "+par_size[cur]+" "+par_size[to]);
  118. //System.out.println(to);
  119. spanning.add(to);
  120. visited[to] = true;
  121. adj.get(cur).add(to);
  122.  
  123. if (!contain(cur,to)){
  124. parent[to][par_size[to]] = cur;
  125. par_size[to]++;
  126.  
  127. for (int t = 0; t < par_size[cur]; t++){
  128. if (!contain(to,parent[cur][t])){
  129. parent[to][par_size[to]] = parent[cur][t];
  130. par_size[to]++;
  131. }
  132. }
  133.  
  134. //System.out.println("Updating "+to);
  135. update(to);
  136. }
  137.  
  138. if (i == Ecount-1){
  139. for (int q = 0; q < Vcount; q++){
  140. if (visited[q] == false){
  141. i--;
  142. add++;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152. for (int m = 0; m < Vcount; m++){
  153. if (adj.get(m).size() == 0){
  154. adj.get(m).add(Vcount);
  155. add++;
  156. }
  157. }
  158.  
  159.  
  160.  
  161. System.out.println("1");
  162. System.out.println("");
  163. System.out.println((Vcount+1)+" "+(Ecount+add));
  164.  
  165. System.out.printf("%d",random.nextInt(Wlimit)+1);
  166. for (int m = 0; m < Vcount-1; m++)
  167. System.out.printf(" %d",random.nextInt(Wlimit)+1);
  168. System.out.println(" 0");
  169.  
  170. for (int m = 0; m < Vcount; m++){
  171. for (int b = 0; b < adj.get(m).size(); b++){
  172. System.out.println(m+" "+adj.get(m).get(b));
  173. }
  174. }
  175.  
  176. }
  177. }
Add Comment
Please, Sign In to add comment