Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class V {
- public ArrayList<V> vIn;
- public ArrayList<V> vOut;
- public HashMap<V, V> deti;
- public int num;
- public int T1;
- public int l;
- public int cm;
- public int t;
- public String nm;
- public String a;
- public boolean cr;
- public boolean cy;
- public V(int num,String nm,String a) {
- this.num=num;this.t=num;
- this.nm=nm;this.a=a;
- vIn=new ArrayList<V>();vOut=new ArrayList<V>();
- deti=new HashMap<V, V>();
- }
- }
- public class Cpm {
- public static int t;
- public static int num;
- public static Scanner in;
- public static ArrayList<V> g;
- public static HashMap<String, Integer> pair;
- public static boolean fl1;
- public static boolean fl2;
- public static boolean fl3;
- public static String stroka1;
- public static String stroka2;
- public static int numer=0;
- public static int magic_number=48;
- public static void setCy(V v) {
- ArrayList<V> m=v.vOut;
- v.cy=true;
- for (int i=0; i<=m.size()-1; i++) if (! m.get(i).cy) setCy(m.get(i));
- }
- public static ArrayList<V> build() {
- int max=0;ArrayList<ArrayList<V>> parts=new ArrayList<ArrayList<V>>();
- for (int i=0; i<=num-2; i++) parts.add(new ArrayList<V>());
- for (int i=0; i<=g.size()-1; i++) parts.get(g.get(i).cm-1).add(g.get(i));
- for (int i=0; i<=num-2; i++) {
- ArrayList<V> help=parts.get(i);
- if (parts.size()>1 && (help.size()>1 || help.size()==1 && help.get(0).vIn.contains(help.get(0)))) setCy(help.get(0));
- else if (help.size()>1) for (int j=0; j < help.size(); j++) help.get(j).cy=true;
- else if (help.get(0).vIn.contains(help.get(0))) help.get(0).cy=true;
- }
- for (int i=0; i <= g.size()-1; i+=1)
- 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);}
- ArrayList<V> vs=new ArrayList<V>();
- 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;
- for (int i=0; i<=g.size()-1; i++)if ((! g.get(i).cy) && (max == g.get(i).t))vs.add(g.get(i));
- for (int i=0; i<=vs.size()-1; i++)setCr(vs.get(i));
- return g;
- }
- public static void setCr(V v) {
- v.cr=true;
- for (V value : v.deti.values()) if (! value.cy) setCr(value);
- }
- public static Stack<V> vvt(V v, Stack<V> s) {
- v.T1=v.l=t++;
- s.push(v);
- for (int i=0; i<=v.vOut.size()-1; i++) {
- if (v.vOut.get(i).T1 == 0) s=vvt(v.vOut.get(i), s);
- if ((v.vOut.get(i).cm == 0) && (v.l > v.vOut.get(i).l)) v.l=v.vOut.get(i).l;
- }
- if (v.T1 == v.l) {
- V w=new V(-1,"","");
- for (;w!=v;) {w=s.pop(); w.cm=num; }
- num++;
- }
- return s;
- }
- public static ArrayList<V> tj() {
- int n=g.size();
- t=num=1;
- for (int i=0;i<=n-1;i++)g.get(i).cm=g.get(i).T1=0;
- Stack<V> s=new Stack<V>();
- for (int i=0;i<=n-1;i++)if(g.get(i).T1 == 0)s=vvt(g.get(i), s);
- return g;
- }
- public static void visitV(V v, int t) {
- v.T1=t++;t++;
- ArrayList<V> m=v.vIn;
- for (int i=0; i<=m.size()-1; i++) {
- V w=m.get(i);
- if (! w.cy)
- if (w.t < w.num+v.t) {
- w.t=w.num+v.t;
- if (w.deti.size() != 0) {w.deti.clear();w.deti=new HashMap<V, V>();}
- w.deti.put(v, v);
- } else if (w.t == w.num+v.t)w.deti.put(v, v);
- if (w.T1 == 0) visitV(w, t);
- }
- }
- public static void main(String[] arguments) {
- in=new Scanner(System.in);
- int n; t=0; num=0;
- g=new ArrayList<V>();
- pair=new HashMap<String, Integer>();
- V v=null;
- do
- {
- stroka1=in.next();
- fl1=false;
- if (stroka1.charAt(stroka1.length()-1) == ';') {stroka1=stroka1.substring(0, stroka1.length()-1);fl1=true;}
- if (stroka1.equals("<"))continue;
- else {
- n=0;stroka2 ="";fl2=false;
- if (stroka1.charAt(stroka1.length()-1) == ')') {
- int j=0;
- fl3=false;
- for (int k=0; k <= stroka1.length()-2; k++)
- if (fl3) n+=Math.pow(10, stroka1.length()-(2+k))* (stroka1.charAt(k)-magic_number);
- else if (stroka1.charAt(k) == '(') {j=k;fl3=true;}
- stroka2=stroka1.substring(0, j);fl2=true;
- }
- V w;
- if(fl2){w=new V(n,stroka2,stroka1);pair.put(stroka2, numer);g.add(numer++,w);
- } else w=g.get(pair.get(stroka1));
- if (v == null)v=w;
- else {
- if (! v.vOut.contains(w))v.vOut.add(w);if(! w.vIn.contains(v))w.vIn.add(v);
- v=w;
- }
- }
- if(fl1)v=null;
- } while (in.hasNext());
- g=tj();g=build();
- System.out.println("digraph {");
- for (int i=0; i <= g.size()-1; i=i+1) {
- v=g.get(i);
- String s=" "+
- v.nm+
- " [label = \""+
- v.a+
- "\"";
- if(v.cr)s+=", color = red]";
- else if (v.cy)s+=", color = blue]";
- else s+="]";System.out.println(s);
- }
- for (int i=0;i<=g.size()-1;i+=1)
- {
- for (int j=0;j<=g.get(i).vOut.size()-1;j++)
- {
- v=g.get(i).vOut.get(j);
- String s=" "+
- g.get(i).nm+
- " -> "+
- v.nm;
- if (g.get(i).cr && v.cr && g.get(i).deti.containsKey(v))s+=" [color = red]";
- if (g.get(i).cy && v.cy)s+=" [color = blue] ";
- System.out.print(s);
- System.out.println();
- }
- }
- System.out.println("}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement