Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.03 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pal;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.StreamTokenizer;
  14. import java.io.StringReader;
  15. import java.util.HashMap;
  16. import java.util.Iterator;
  17. import java.util.Map;
  18. import java.util.StringTokenizer;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21.  
  22. /**
  23.  *
  24.  * @author slavo
  25.  */
  26. public class Main {
  27.  
  28.     static int index, stackPointer = -1, sccStackPointer = -1;
  29.     static int v, e, scc = 0, result = 0;
  30.    
  31.     static boolean stackNodes[], lakeVisited[];
  32.     static int stack[], sccStack[], nodeLowLink[], nodeIndex[], nodeColors[], lakeCanals[], lakeRating[];
  33.    
  34.     static String node;
  35.     static String graph[], condensedGraph[];
  36.     static StringBuilder builder = new StringBuilder();
  37.    
  38.    
  39.  
  40.    
  41.     /**
  42.      * @param args the command line arguments
  43.      */
  44.     public static void main(String[] args) {
  45.         loadGraph("");
  46.        
  47.         stackInit(v);
  48.         sccStack = new int[v];
  49.  
  50.         tarjan();
  51.  
  52.         condensate();
  53.                
  54.         traverse();
  55.        
  56.         System.out.println(result);
  57.     }
  58.    
  59.     protected static void traverse(){
  60.         int node, tmp;
  61.  
  62.         for(node = 0; node < scc; node++){
  63.            
  64.             if(lakeVisited[node]){
  65.                 tmp = lakeRating[node];
  66.             }else{
  67.                 tmp = dfs(node, 0);
  68.             }
  69.            
  70.             lakeVisited[node] = true;
  71.             lakeRating[node] = tmp;
  72.            
  73.             if(tmp > result) result = tmp;    
  74.         }
  75.     }
  76.    
  77.     protected static int dfs(int lake, int canals){
  78.         char string[];
  79.         int j, w, tmp, max;
  80.         boolean broken;        
  81.        
  82.  
  83.         canals = canals + lakeCanals[lake];
  84.         max = canals;
  85.        
  86.         if(condensedGraph[lake] != null){
  87.             string = condensedGraph[lake].toCharArray();
  88.  
  89.             j = 0;
  90.             while(j < string.length){
  91.  
  92.                 if(string[j] == '*'){
  93.                     broken = true;
  94.                     j++;
  95.                 }else{
  96.                     broken = false;
  97.                 }
  98.  
  99.                 w = 0;
  100.                 while(j < string.length && string[j] != ' '){
  101.                     w = (w*10) + ((int) string[j] - 48);
  102.                     j++;
  103.                 }
  104.  
  105.                 if(broken){
  106.                     if(lakeVisited[w]){
  107.                         tmp = canals + lakeRating[w] + 1;
  108.                     }else{
  109.                         tmp = dfs(w, (canals + 1));
  110.                     }
  111.                 }else{
  112.                     if(lakeVisited[w]){
  113.                         tmp = canals + lakeRating[w];
  114.                     }else{
  115.                         tmp = dfs(w, canals);
  116.                     }
  117.                 }
  118.                
  119.                 if(tmp > max) max = tmp;
  120.                 j++;
  121.             }
  122.         }
  123.  
  124.         lakeVisited[lake] = true;
  125.         lakeRating[lake] = max;
  126.         return max;
  127.     }
  128.    
  129.     protected static void condensate(){
  130.         int i,j,w;
  131.         boolean broken;
  132.         char string[];
  133.        
  134.         lakeVisited = new boolean[scc];
  135.         lakeRating = new int[scc];
  136.        
  137.         condensedGraph = new String[scc];
  138.          
  139.         for(i = 0; i < v; i++){
  140.             if(graph[i] != null){
  141.                 string = graph[i].toCharArray();
  142.  
  143.                 j = 0;
  144.                 while(j < string.length){
  145.                     if(string[j] == '*'){
  146.                         broken = true;
  147.                         j++;
  148.                     }else{
  149.                         broken = false;
  150.                     }
  151.  
  152.                     w = 0;
  153.                     while(j < string.length && string[j] != ' '){
  154.                         w = (w*10) + ((int) string[j] - 48);
  155.                         j++;
  156.                     }
  157.  
  158.                     if(nodeColors[i] != nodeColors[w]){
  159.                         node = condensedGraph[nodeColors[i]-1];
  160.  
  161.                         builder.setLength(0);        
  162.                        
  163.                         if(node != null){
  164.                             builder.append(node);
  165.                             builder.append(" ");
  166.                         }
  167.                        
  168.                         if(broken){
  169.                             builder.append("*");
  170.                         }
  171.  
  172.                         builder.append(nodeColors[w]-1);
  173.  
  174.                         condensedGraph[nodeColors[i]-1] = builder.toString();
  175.                     }
  176.  
  177.                     j++;
  178.                 }
  179.             }
  180.         }
  181.        
  182.     }
  183.    
  184.     protected static void findSCC(int v){
  185.         int w,i,j;
  186.         boolean broken = false;
  187.         char string[];
  188.        
  189.         nodeIndex[v] = nodeLowLink[v] = ++index;
  190.         stackPush(v);        
  191.        
  192.         if(graph[v] != null){
  193.             string = graph[v].toCharArray();
  194.             i = 0;
  195.             while(i < string.length){
  196.  
  197.                 if(string[i] == '*'){
  198.                     i++;
  199.                 }
  200.  
  201.                 w = 0;
  202.                 while(i < string.length && string[i] != ' '){
  203.                     w = (w*10) + ((int) string[i] - 48);
  204.                     i++;
  205.                 }
  206.                
  207.                 if(nodeIndex[w] == 0){
  208.                     findSCC(w);
  209.                     nodeLowLink[v] = (nodeLowLink[v] < nodeLowLink[w]) ? nodeLowLink[v] : nodeLowLink[w];                    
  210.                 }else if(inStack(w)){
  211.                     nodeLowLink[v] = (nodeLowLink[v] < nodeIndex[w]) ? nodeLowLink[v] : nodeIndex[w];
  212.                 }
  213.  
  214.                 i++;
  215.             }
  216.         }
  217.  
  218.         if(nodeIndex[v] == nodeLowLink[v]){
  219.             scc++ ;
  220.                    
  221.  
  222.             do {
  223.                 w = stackPop();
  224.                 sccStack[++sccStackPointer] = w;
  225.                 nodeColors[w] = scc;
  226.             } while (w != v);
  227.            
  228.             do{
  229.                
  230.                 v = sccStack[sccStackPointer];
  231.                 if(graph[v] != null){
  232.                     string = graph[v].toCharArray();
  233.                     i = 0;
  234.                    
  235.                     while(i < string.length){
  236.                         if(string[i] == '*'){
  237.                             broken = true;
  238.                             i++;
  239.                         }else{
  240.                             broken = false;
  241.                         }
  242.  
  243.                         w = 0;
  244.                         while(i < string.length && string[i] != ' '){
  245.                             w = (w*10) + ((int) string[i] - 48);
  246.                             i++;
  247.                         }
  248.  
  249.                         if(broken && nodeColors[w] == scc){
  250.                             lakeCanals[scc-1]++;
  251.                         }
  252.  
  253.                         i++;
  254.                     }
  255.                 }
  256.                
  257.                
  258.                
  259.                
  260.                 sccStackPointer--;
  261.             }while(sccStackPointer >= 0);
  262.            
  263.            
  264.         }
  265.     }
  266.    
  267.    
  268.     protected static void tarjan(){
  269.         int i;
  270.        
  271.         scc = 0;
  272.         index = 0; // unique node number > 0
  273.        
  274.         nodeLowLink = new int[v];
  275.         nodeIndex = new int[v];
  276.         nodeColors = new int[v];
  277.         lakeCanals = new int[v+1];
  278.        
  279.         for(i = 0; i < v; i++){
  280.             if(nodeIndex[i] == 0){
  281.                 findSCC(i);                    
  282.             }
  283.         }
  284.     }
  285.    
  286.     protected static void stackInit(int n){
  287.         stack = new int[n];
  288.         stackNodes = new boolean[n];
  289.         stackPointer = -1;
  290.        
  291.     }
  292.    
  293.     protected static void stackPush(int x){
  294.         stack[++stackPointer] = x;
  295.         stackNodes[x] = true;
  296.     }
  297.    
  298.     protected static int stackPop(){
  299.         int x = stack[stackPointer--];
  300.         stackNodes[x] = false;
  301.        
  302.         return x;
  303.     }
  304.    
  305.     protected static boolean inStack(int x){
  306.         return stackNodes[x];
  307.     }
  308.    
  309.     protected static void printStack(){
  310.         int i;
  311.        
  312.         System.out.println("Stack:");
  313.         System.out.println("-----");
  314.        
  315.         for(i = 0; i <= stackPointer; i++){    
  316.             System.out.println(stack[i]);
  317.         }
  318.        
  319.         System.out.println("-----");
  320.     }
  321.    
  322.    
  323.     protected static void loadGraph(String file){
  324.         String parsed[], node, sign, dst;
  325.        
  326.         char string[];
  327.         int i = 0;
  328.         int src;
  329.        
  330.         BufferedReader br;
  331.         try {
  332.             if(file.equals("")){
  333.                 br = new BufferedReader(new InputStreamReader(System.in));
  334.             }else{
  335.                 br = new BufferedReader(new FileReader(file));
  336.             }
  337.            
  338.             parsed = br.readLine().split(" ");
  339.            
  340.             v = Integer.parseInt(parsed[0]);
  341.             e = Integer.parseInt(parsed[1]);
  342.            
  343.             graph = new String[v];
  344.            
  345.             long startTime = System.nanoTime();    
  346.  
  347.             for(i = 0; i < e; i++){
  348.                 StringTokenizer st = new StringTokenizer(br.readLine());
  349.                
  350.                 src = Integer.parseInt(st.nextToken());
  351.                 dst = st.nextToken();
  352.                 sign = st.nextToken();
  353.                
  354.                 node = graph[src];
  355.                
  356.                 builder.setLength(0);        
  357.                
  358.                 if(node != null){
  359.                     builder.append(node);
  360.                     builder.append(" ");
  361.                 }
  362.                 if(Integer.parseInt(sign) == 1){
  363.                     builder.append("*");
  364.                 }
  365.  
  366.                 builder.append(dst);
  367.  
  368.                 graph[src] = builder.toString();
  369.             }  
  370.            
  371.             long estimatedTime = System.nanoTime() - startTime;
  372.  
  373.         } catch (IOException ex) {
  374.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  375.         }
  376.     }
  377.    
  378.     protected static void printGraph() {
  379.         int i;
  380.        
  381.         for(i = 0; i < graph.length ; i++){
  382.             System.out.println(i + " : " + graph[i]);
  383.         }
  384.     }
  385.  
  386.     protected static void printCondensedGraph() {
  387.         int i;
  388.        
  389.         for(i = 0; i < condensedGraph.length ; i++){
  390.             System.out.println(i + " : " + condensedGraph[i]);
  391.         }
  392.     }
  393.    
  394.     protected static void printColors() {
  395.         int i;
  396.        
  397.         for(i = 0; i < nodeColors.length ; i++){
  398.             System.out.println(i + " color: " + nodeColors[i]);
  399.         }
  400.     }
  401.    
  402.     protected static void printArray(int array[]) {
  403.         int i;
  404.        
  405.         for(i = 0; i < array.length ; i++){
  406.             System.out.println(i + ": " + array[i]);
  407.         }
  408.     }
  409.    
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement