Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class V {
  4. public ArrayList<V> vIn;
  5. public ArrayList<V> vOut;
  6. public HashMap<V, V> deti;
  7. public int num;
  8. public int T1;
  9. public int l;
  10. public int cm;
  11. public int t;
  12. public String nm;
  13. public String a;
  14. public boolean cr;
  15. public boolean cy;
  16.  
  17. public V(int num,String nm,String a) {
  18. this.num=num;this.t=num;
  19. this.nm=nm;this.a=a;
  20. vIn=new ArrayList<V>();vOut=new ArrayList<V>();
  21. deti=new HashMap<V, V>();
  22. }
  23. }
  24.  
  25. public class Cpm {
  26. public static int t;
  27. public static int num;
  28. public static Scanner in;
  29. public static ArrayList<V> g;
  30. public static HashMap<String, Integer> pair;
  31. public static boolean fl1;
  32. public static boolean fl2;
  33. public static boolean fl3;
  34. public static String stroka1;
  35. public static String stroka2;
  36. public static int numer=0;
  37. public static int magic_number=48;
  38. public static void setCy(V v) {
  39. ArrayList<V> m=v.vOut;
  40. v.cy=true;
  41. for (int i=0; i<=m.size()-1; i++) if (! m.get(i).cy) setCy(m.get(i));
  42. }
  43. public static ArrayList<V> build() {
  44. int max=0;ArrayList<ArrayList<V>> parts=new ArrayList<ArrayList<V>>();
  45. for (int i=0; i<=num-2; i++) parts.add(new ArrayList<V>());
  46. for (int i=0; i<=g.size()-1; i++) parts.get(g.get(i).cm-1).add(g.get(i));
  47. for (int i=0; i<=num-2; i++) {
  48. ArrayList<V> help=parts.get(i);
  49. if (parts.size()>1 && (help.size()>1 || help.size()==1 && help.get(0).vIn.contains(help.get(0)))) setCy(help.get(0));
  50. else if (help.size()>1) for (int j=0; j < help.size(); j++) help.get(j).cy=true;
  51. else if (help.get(0).vIn.contains(help.get(0))) help.get(0).cy=true;
  52. }
  53. for (int i=0; i <= g.size()-1; i+=1)
  54. if (g.get(i).vOut.size() == 0) { for (int j=0; j < g.size(); j++) g.get(j).T1=0;visitV(g.get(i), 0);}
  55. ArrayList<V> vs=new ArrayList<V>();
  56. for (int i=0; i<=g.size()-1; i++)if ((! g.get(i).cy) && (max < g.get(i).t) && (g.get(i).vIn.size() == 0))max=g.get(i).t;
  57. for (int i=0; i<=g.size()-1; i++)if ((! g.get(i).cy) && (max == g.get(i).t))vs.add(g.get(i));
  58. for (int i=0; i<=vs.size()-1; i++)setCr(vs.get(i));
  59. return g;
  60. }
  61. public static void setCr(V v) {
  62. v.cr=true;
  63. for (V value : v.deti.values()) if (! value.cy) setCr(value);
  64. }
  65. public static Stack<V> vvt(V v, Stack<V> s) {
  66. v.T1=v.l=t++;
  67. s.push(v);
  68. for (int i=0; i<=v.vOut.size()-1; i++) {
  69. if (v.vOut.get(i).T1 == 0) s=vvt(v.vOut.get(i), s);
  70. if ((v.vOut.get(i).cm == 0) && (v.l > v.vOut.get(i).l)) v.l=v.vOut.get(i).l;
  71. }
  72. if (v.T1 == v.l) {
  73. V w=new V(-1,"","");
  74. for (;w!=v;) {w=s.pop(); w.cm=num; }
  75. num++;
  76. }
  77. return s;
  78. }
  79. public static ArrayList<V> tj() {
  80. int n=g.size();
  81. t=num=1;
  82. for (int i=0;i<=n-1;i++)g.get(i).cm=g.get(i).T1=0;
  83. Stack<V> s=new Stack<V>();
  84. for (int i=0;i<=n-1;i++)if(g.get(i).T1 == 0)s=vvt(g.get(i), s);
  85. return g;
  86. }
  87. public static void visitV(V v, int t) {
  88. v.T1=t++;t++;
  89. ArrayList<V> m=v.vIn;
  90. for (int i=0; i<=m.size()-1; i++) {
  91. V w=m.get(i);
  92. if (! w.cy)
  93. if (w.t < w.num+v.t) {
  94. w.t=w.num+v.t;
  95. if (w.deti.size() != 0) {w.deti.clear();w.deti=new HashMap<V, V>();}
  96. w.deti.put(v, v);
  97. } else if (w.t == w.num+v.t)w.deti.put(v, v);
  98. if (w.T1 == 0) visitV(w, t);
  99. }
  100. }
  101. public static void main(String[] arguments) {
  102. in=new Scanner(System.in);
  103. int n; t=0; num=0;
  104. g=new ArrayList<V>();
  105. pair=new HashMap<String, Integer>();
  106. V v=null;
  107. do
  108. {
  109. stroka1=in.next();
  110. fl1=false;
  111. if (stroka1.charAt(stroka1.length()-1) == ';') {stroka1=stroka1.substring(0, stroka1.length()-1);fl1=true;}
  112. if (stroka1.equals("<"))continue;
  113. else {
  114. n=0;stroka2 ="";fl2=false;
  115. if (stroka1.charAt(stroka1.length()-1) == ')') {
  116. int j=0;
  117. fl3=false;
  118. for (int k=0; k <= stroka1.length()-2; k++)
  119. if (fl3) n+=Math.pow(10, stroka1.length()-(2+k))* (stroka1.charAt(k)-magic_number);
  120. else if (stroka1.charAt(k) == '(') {j=k;fl3=true;}
  121. stroka2=stroka1.substring(0, j);fl2=true;
  122. }
  123. V w;
  124. if(fl2){w=new V(n,stroka2,stroka1);pair.put(stroka2, numer);g.add(numer++,w);
  125. } else w=g.get(pair.get(stroka1));
  126. if (v == null)v=w;
  127. else {
  128. if (! v.vOut.contains(w))v.vOut.add(w);if(! w.vIn.contains(v))w.vIn.add(v);
  129. v=w;
  130. }
  131. }
  132. if(fl1)v=null;
  133. } while (in.hasNext());
  134. g=tj();g=build();
  135. System.out.println("digraph {");
  136. for (int i=0; i <= g.size()-1; i=i+1) {
  137. v=g.get(i);
  138. String s=" "+
  139. v.nm+
  140. " [label = \""+
  141. v.a+
  142. "\"";
  143. if(v.cr)s+=", color = red]";
  144. else if (v.cy)s+=", color = blue]";
  145. else s+="]";System.out.println(s);
  146. }
  147. for (int i=0;i<=g.size()-1;i+=1)
  148. {
  149. for (int j=0;j<=g.get(i).vOut.size()-1;j++)
  150. {
  151. v=g.get(i).vOut.get(j);
  152. String s=" "+
  153. g.get(i).nm+
  154. " -> "+
  155. v.nm;
  156. if (g.get(i).cr && v.cr && g.get(i).deti.containsKey(v))s+=" [color = red]";
  157. if (g.get(i).cy && v.cy)s+=" [color = blue] ";
  158. System.out.print(s);
  159. System.out.println();
  160. }
  161. }
  162. System.out.println("}");
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement