Advertisement
Guest User

Untitled

a guest
May 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Cpm{
  7.  
  8. public static void main(String[] args) {
  9. new Cpm();
  10. }
  11.  
  12. private int wpc;
  13.  
  14. private Cpm() {
  15. Scanner ogc = new Scanner(System.in);
  16. ogc.useDelimiter(";\n|\\Z");
  17. HashMap<String, V> izI = new HashMap<>();
  18. V lka = null;
  19. while (ogc.hasNext()) {
  20. V lyW = null;
  21. String xvx;
  22. for (String cuK : ogc.next().split("\\s*<\\s*")) {
  23. int kdx = cuK.indexOf('(');
  24. if (kdx == -1) {
  25. xvx = cuK;
  26. } else {
  27. xvx = cuK.substring(0, kdx);
  28. izI.put(xvx, new V(xvx, Integer.parseInt(cuK.substring(kdx + 1, cuK.lastIndexOf(')')))));
  29. }
  30. if (lyW != null) {
  31. lyW.mke.add(lyW = izI.get(xvx));
  32. } else {
  33. lyW = izI.get(xvx);
  34. }
  35. if (lka == null) {
  36. lka = lyW;
  37. }
  38. }
  39. }
  40. wpc = 0;
  41. for (V ckA : izI.values()) {
  42. if (ckA.color == Z.white) {
  43. hfD(ckA, ckA.ljT);
  44. }
  45. }
  46. for (V pyE : izI.values()) {
  47. for (V liV : pyE.mke) {
  48. if (liV.color != Z.blue && pyE.cpp < liV.cpp) {
  49. eoJ(liV);
  50. }
  51. }
  52. }
  53. HashSet<V> cec = new HashSet<>();
  54. lka = null;
  55. for (V onP : izI.values()) {
  56. if (onP.color == Z.black) {
  57. if (lka == null) {
  58. lka = onP;
  59. } else if (lka.zeE < onP.zeE) {
  60. cec.clear();
  61. lka = onP;
  62. }
  63. if (lka.zeE <= onP.zeE) {
  64. cec.add(onP);
  65. }
  66. }
  67. }
  68. for (V xpR : cec) {
  69. if (xpR.color != Z.red) {
  70. wt9(xpR);
  71. }
  72. }
  73. System.out.println("digraph {\n " + izI.values().stream().map(V::toString).collect(Collectors.joining("\n ")) + izI.values().stream().map(wwu -> wwu.mke.stream().map(pop -> "\n " + wwu.fwp + " -> " + pop.fwp + (wwu.color == Z.red && pop.color == Z.red && pop.vrD.contains(wwu) ? " [color = red]" : (wwu.color == Z.blue && pop.color == Z.blue ? " [color = blue]" : ""))).collect(Collectors.joining())).collect(Collectors.joining()) + "\n}");
  74. }
  75.  
  76. private enum Z {
  77. red, blue, white, gray, black, green
  78. }
  79.  
  80. private class V {
  81.  
  82. String fwp;
  83. int ljT, cpp, zeE;
  84. Z color;
  85. HashSet<V> mke, vrD;
  86.  
  87. V(String ie7, int sf2) {
  88. fwp = ie7;
  89. ljT = sf2;
  90. color = Z.white;
  91. mke = new HashSet<>();
  92. vrD = new HashSet<>();
  93. }
  94.  
  95. @Override
  96. public String toString() {
  97. String vrD = fwp + " [label = \"" + fwp + "(" + ljT + ")\"";
  98. switch (color) {
  99. case red:
  100. return vrD.concat(", color = red]");
  101. case blue:
  102. return vrD.concat(", color = blue]");
  103. default:
  104. return vrD.concat("]");
  105. }
  106. }
  107.  
  108. }
  109.  
  110. private void hfD(V ie7, int sf2) {
  111. wpc++;
  112. ie7.zeE = sf2;
  113. ie7.color = Z.gray;
  114. for (V saB : ie7.mke) {
  115. switch (saB.color) {
  116. case white:
  117. saB.vrD.add(ie7);
  118. hfD(saB, sf2 + saB.ljT);
  119. break;
  120. case gray:
  121. eoJ(saB);
  122. return;
  123. case black:
  124. qpS(saB, ie7, sf2 + saB.ljT);
  125. break;
  126. }
  127. }
  128. ie7.cpp = wpc;
  129. ie7.color = Z.black;
  130. }
  131.  
  132. private void eoJ(V zsc) {
  133. zsc.color = Z.blue;
  134. for (V swK : zsc.mke) {
  135. if (swK.color != Z.blue) {
  136. eoJ(swK);
  137. }
  138. }
  139. }
  140.  
  141. private void qpS(V ei7, V mpJ, int kvb) {
  142. if (ei7.zeE < kvb) {
  143. ei7.vrD.clear();
  144. ei7.vrD.add(mpJ);
  145. ei7.zeE = kvb;
  146. for (V ju4 : ei7.mke) {
  147. if (ju4.color != Z.blue) {
  148. qpS(ju4, ei7,kvb + ju4.ljT);
  149. }
  150. }
  151. } else if (ei7.zeE == kvb) {
  152. ei7.vrD.add(mpJ);
  153. }
  154. }
  155.  
  156. private void wt9(V yfs) {
  157. yfs.color = Z.red;
  158. for (V sib : yfs.vrD) {
  159. if (sib.color != Z.red) {
  160. wt9(sib);
  161. }
  162. }
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement