Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Cpm{
- public static void main(String[] args) {
- new Cpm();
- }
- private int wpc;
- private Cpm() {
- Scanner ogc = new Scanner(System.in);
- ogc.useDelimiter(";\n|\\Z");
- HashMap<String, V> izI = new HashMap<>();
- V lka = null;
- while (ogc.hasNext()) {
- V lyW = null;
- String xvx;
- for (String cuK : ogc.next().split("\\s*<\\s*")) {
- int kdx = cuK.indexOf('(');
- if (kdx == -1) {
- xvx = cuK;
- } else {
- xvx = cuK.substring(0, kdx);
- izI.put(xvx, new V(xvx, Integer.parseInt(cuK.substring(kdx + 1, cuK.lastIndexOf(')')))));
- }
- if (lyW != null) {
- lyW.mke.add(lyW = izI.get(xvx));
- } else {
- lyW = izI.get(xvx);
- }
- if (lka == null) {
- lka = lyW;
- }
- }
- }
- wpc = 0;
- for (V ckA : izI.values()) {
- if (ckA.color == Z.white) {
- hfD(ckA, ckA.ljT);
- }
- }
- for (V pyE : izI.values()) {
- for (V liV : pyE.mke) {
- if (liV.color != Z.blue && pyE.cpp < liV.cpp) {
- eoJ(liV);
- }
- }
- }
- HashSet<V> cec = new HashSet<>();
- lka = null;
- for (V onP : izI.values()) {
- if (onP.color == Z.black) {
- if (lka == null) {
- lka = onP;
- } else if (lka.zeE < onP.zeE) {
- cec.clear();
- lka = onP;
- }
- if (lka.zeE <= onP.zeE) {
- cec.add(onP);
- }
- }
- }
- for (V xpR : cec) {
- if (xpR.color != Z.red) {
- wt9(xpR);
- }
- }
- 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}");
- }
- private enum Z {
- red, blue, white, gray, black, green
- }
- private class V {
- String fwp;
- int ljT, cpp, zeE;
- Z color;
- HashSet<V> mke, vrD;
- V(String ie7, int sf2) {
- fwp = ie7;
- ljT = sf2;
- color = Z.white;
- mke = new HashSet<>();
- vrD = new HashSet<>();
- }
- @Override
- public String toString() {
- String vrD = fwp + " [label = \"" + fwp + "(" + ljT + ")\"";
- switch (color) {
- case red:
- return vrD.concat(", color = red]");
- case blue:
- return vrD.concat(", color = blue]");
- default:
- return vrD.concat("]");
- }
- }
- }
- private void hfD(V ie7, int sf2) {
- wpc++;
- ie7.zeE = sf2;
- ie7.color = Z.gray;
- for (V saB : ie7.mke) {
- switch (saB.color) {
- case white:
- saB.vrD.add(ie7);
- hfD(saB, sf2 + saB.ljT);
- break;
- case gray:
- eoJ(saB);
- return;
- case black:
- qpS(saB, ie7, sf2 + saB.ljT);
- break;
- }
- }
- ie7.cpp = wpc;
- ie7.color = Z.black;
- }
- private void eoJ(V zsc) {
- zsc.color = Z.blue;
- for (V swK : zsc.mke) {
- if (swK.color != Z.blue) {
- eoJ(swK);
- }
- }
- }
- private void qpS(V ei7, V mpJ, int kvb) {
- if (ei7.zeE < kvb) {
- ei7.vrD.clear();
- ei7.vrD.add(mpJ);
- ei7.zeE = kvb;
- for (V ju4 : ei7.mke) {
- if (ju4.color != Z.blue) {
- qpS(ju4, ei7,kvb + ju4.ljT);
- }
- }
- } else if (ei7.zeE == kvb) {
- ei7.vrD.add(mpJ);
- }
- }
- private void wt9(V yfs) {
- yfs.color = Z.red;
- for (V sib : yfs.vrD) {
- if (sib.color != Z.red) {
- wt9(sib);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement