Advertisement
Guest User

Cpm

a guest
Sep 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Cpm {
  5.     private static ArrayList<Work> W = new ArrayList<>();
  6.     private static ArrayList<String> dict = new ArrayList<>();
  7.  
  8.     private static void DFS(int n){
  9.         W.get(n).color = 1;
  10.         for (int i : W.get(n).E) {
  11.             if(W.get(i).color == 0){
  12.                 DFS(i);
  13.             } else if(W.get(i).color == 1){
  14.                 if (!W.get(i).wrong) {
  15.                     delCycles(i);
  16.                 }
  17.             }
  18.         }
  19.         W.get(n).color = 2;
  20.     }
  21.  
  22.     private static void delCycles(int n){
  23.         W.get(n).wrong = true;
  24.         for (int i : W.get(n).E) {
  25.             if (!W.get(i).wrong) {
  26.                 delCycles(i);
  27.             }
  28.         }
  29.     }
  30.  
  31.     private static void calcSums(int n){
  32.         W.get(n).color = 1;
  33.         for (int i : W.get(n).E) {
  34.             if(!W.get(i).wrong && W.get(i).color == 2){
  35.                 calcSums(i);
  36.             }
  37.         }
  38.         W.get(n).color = 0;
  39.         int max = -1;
  40.         for (int i : W.get(n).E) {
  41.             if(!W.get(i).wrong){
  42.                 if(W.get(i).sum > max) {
  43.                     max = W.get(i).sum;
  44.                 }
  45.             }
  46.         }
  47.         W.get(n).sum += max;
  48.     }
  49.  
  50.     private static void findWay(int n){
  51.         W.get(n).color = 3;
  52.         int max = -1;
  53.         ArrayList<Integer> ind = new ArrayList<>();
  54.         for (int i : W.get(n).E) {
  55.             if(!W.get(i).wrong){
  56.                 if(W.get(i).sum == max) ind.add(i);
  57.                 if(W.get(i).sum > max) {
  58.                     max = W.get(i).sum;
  59.                     ind.clear();
  60.                     ind.add(i);
  61.                 }
  62.             }
  63.         }
  64.         if(!ind.isEmpty()){
  65.             for (Integer i : ind) {
  66.                 W.get(n).clrs.add(i);
  67.                 findWay(i);
  68.             }
  69.         }
  70.     }
  71.  
  72.     public static void main(String[] args){
  73.         Scanner in = new Scanner(System.in);
  74.         StringBuilder input = new StringBuilder();
  75.         while(in.hasNext()){
  76.             input.append(in.nextLine());
  77.         }
  78.         for (String s : input.toString().replace(" ", "").split(";")) {
  79.             String[] w = s.split("<");
  80.             for (int i = 0; i < w.length; i++){
  81.                 String name = w[i];
  82.                 if (name.contains("(")){
  83.                     int time = Integer.parseInt(name.split("\\(")[1].replace(")", ""));
  84.                     name = name.split("\\(")[0];
  85.                     dict.add(name);
  86.                     W.add(new Work(time));
  87.                 }
  88.                 if (i != 0){
  89.                     int from = dict.indexOf(w[i-1].split("\\(")[0]), to = dict.indexOf(w[i].split("\\(")[0]);
  90.                     if(!W.get(from).E.contains(to)){
  91.                         W.get(from).E.add(to);
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         for  (int i = 0; i < W.size(); i++){
  97.             if(W.get(i).color == 0){
  98.                 DFS(i);
  99.             }
  100.         }
  101.         for  (int i = 0; i < W.size(); i++) {
  102.             if (!W.get(i).wrong && W.get(i).color == 2) {
  103.                 calcSums(i);
  104.             }
  105.         }
  106.         int max = -1;
  107.         ArrayList<Integer> ind = new ArrayList<>();
  108.         for (int i = 0; i < W.size(); i++) {
  109.             if(!W.get(i).wrong){
  110.                 if(W.get(i).sum == max) ind.add(i);
  111.                 if(W.get(i).sum > max) {
  112.                     max = W.get(i).sum;
  113.                     ind.clear();
  114.                     ind.add(i);
  115.                 }
  116.             }
  117.         }
  118.         for (Integer i : ind) {
  119.             findWay(i);
  120.         }
  121.         System.out.println("digraph {");
  122.         for(int i = 0; i < W.size(); i++){
  123.             System.out.print("  " + dict.get(i) + " [label = \"" + dict.get(i) + "(" + W.get(i).time + ")\"");
  124.             if(W.get(i).wrong) System.out.println(", color = blue]");
  125.             else if(W.get(i).color == 3) System.out.println(", color = red]");
  126.             else System.out.println("]");
  127.         }
  128.         for(int i = 0; i < W.size(); i++){
  129.             for(int j : W.get(i).E){
  130.                 System.out.print("  " + dict.get(i) + " -> " + dict.get(j));
  131.                 if(W.get(i).wrong) System.out.println(" [color = blue]");
  132.                 else if(W.get(i).clrs.contains(j)) System.out.println(" [color = red]");
  133.                 else System.out.println();
  134.             }
  135.         }
  136.         System.out.println("}");
  137.     }
  138.  
  139.     private static class Work{
  140.         int time, sum, color;
  141.         ArrayList<Integer> E;
  142.         ArrayList<Integer> clrs;
  143.         boolean wrong;
  144.  
  145.         private Work(int t){
  146.             wrong = false;
  147.             time = t;
  148.             sum = time;
  149.             color = 0;
  150.             E = new ArrayList<>();
  151.             clrs = new ArrayList<>();
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement