Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pal;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.StreamTokenizer;
- import java.io.StringReader;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author slavo
- */
- public class Main {
- static int index, stackPointer = -1, sccStackPointer = -1;
- static int v, e, scc = 0, result = 0;
- static boolean stackNodes[], lakeVisited[];
- static int stack[], sccStack[], nodeLowLink[], nodeIndex[], nodeColors[], lakeCanals[], lakeRating[];
- static String node;
- static String graph[], condensedGraph[];
- static StringBuilder builder = new StringBuilder();
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- loadGraph("");
- stackInit(v);
- sccStack = new int[v];
- tarjan();
- condensate();
- traverse();
- System.out.println(result);
- }
- protected static void traverse(){
- int node, tmp;
- for(node = 0; node < scc; node++){
- if(lakeVisited[node]){
- tmp = lakeRating[node];
- }else{
- tmp = dfs(node, 0);
- }
- lakeVisited[node] = true;
- lakeRating[node] = tmp;
- if(tmp > result) result = tmp;
- }
- }
- protected static int dfs(int lake, int canals){
- char string[];
- int j, w, tmp, max;
- boolean broken;
- canals = canals + lakeCanals[lake];
- max = canals;
- if(condensedGraph[lake] != null){
- string = condensedGraph[lake].toCharArray();
- j = 0;
- while(j < string.length){
- if(string[j] == '*'){
- broken = true;
- j++;
- }else{
- broken = false;
- }
- w = 0;
- while(j < string.length && string[j] != ' '){
- w = (w*10) + ((int) string[j] - 48);
- j++;
- }
- if(broken){
- if(lakeVisited[w]){
- tmp = canals + lakeRating[w] + 1;
- }else{
- tmp = dfs(w, (canals + 1));
- }
- }else{
- if(lakeVisited[w]){
- tmp = canals + lakeRating[w];
- }else{
- tmp = dfs(w, canals);
- }
- }
- if(tmp > max) max = tmp;
- j++;
- }
- }
- lakeVisited[lake] = true;
- lakeRating[lake] = max;
- return max;
- }
- protected static void condensate(){
- int i,j,w;
- boolean broken;
- char string[];
- lakeVisited = new boolean[scc];
- lakeRating = new int[scc];
- condensedGraph = new String[scc];
- for(i = 0; i < v; i++){
- if(graph[i] != null){
- string = graph[i].toCharArray();
- j = 0;
- while(j < string.length){
- if(string[j] == '*'){
- broken = true;
- j++;
- }else{
- broken = false;
- }
- w = 0;
- while(j < string.length && string[j] != ' '){
- w = (w*10) + ((int) string[j] - 48);
- j++;
- }
- if(nodeColors[i] != nodeColors[w]){
- node = condensedGraph[nodeColors[i]-1];
- builder.setLength(0);
- if(node != null){
- builder.append(node);
- builder.append(" ");
- }
- if(broken){
- builder.append("*");
- }
- builder.append(nodeColors[w]-1);
- condensedGraph[nodeColors[i]-1] = builder.toString();
- }
- j++;
- }
- }
- }
- }
- protected static void findSCC(int v){
- int w,i,j;
- boolean broken = false;
- char string[];
- nodeIndex[v] = nodeLowLink[v] = ++index;
- stackPush(v);
- if(graph[v] != null){
- string = graph[v].toCharArray();
- i = 0;
- while(i < string.length){
- if(string[i] == '*'){
- i++;
- }
- w = 0;
- while(i < string.length && string[i] != ' '){
- w = (w*10) + ((int) string[i] - 48);
- i++;
- }
- if(nodeIndex[w] == 0){
- findSCC(w);
- nodeLowLink[v] = (nodeLowLink[v] < nodeLowLink[w]) ? nodeLowLink[v] : nodeLowLink[w];
- }else if(inStack(w)){
- nodeLowLink[v] = (nodeLowLink[v] < nodeIndex[w]) ? nodeLowLink[v] : nodeIndex[w];
- }
- i++;
- }
- }
- if(nodeIndex[v] == nodeLowLink[v]){
- scc++ ;
- do {
- w = stackPop();
- sccStack[++sccStackPointer] = w;
- nodeColors[w] = scc;
- } while (w != v);
- do{
- v = sccStack[sccStackPointer];
- if(graph[v] != null){
- string = graph[v].toCharArray();
- i = 0;
- while(i < string.length){
- if(string[i] == '*'){
- broken = true;
- i++;
- }else{
- broken = false;
- }
- w = 0;
- while(i < string.length && string[i] != ' '){
- w = (w*10) + ((int) string[i] - 48);
- i++;
- }
- if(broken && nodeColors[w] == scc){
- lakeCanals[scc-1]++;
- }
- i++;
- }
- }
- sccStackPointer--;
- }while(sccStackPointer >= 0);
- }
- }
- protected static void tarjan(){
- int i;
- scc = 0;
- index = 0; // unique node number > 0
- nodeLowLink = new int[v];
- nodeIndex = new int[v];
- nodeColors = new int[v];
- lakeCanals = new int[v+1];
- for(i = 0; i < v; i++){
- if(nodeIndex[i] == 0){
- findSCC(i);
- }
- }
- }
- protected static void stackInit(int n){
- stack = new int[n];
- stackNodes = new boolean[n];
- stackPointer = -1;
- }
- protected static void stackPush(int x){
- stack[++stackPointer] = x;
- stackNodes[x] = true;
- }
- protected static int stackPop(){
- int x = stack[stackPointer--];
- stackNodes[x] = false;
- return x;
- }
- protected static boolean inStack(int x){
- return stackNodes[x];
- }
- protected static void printStack(){
- int i;
- System.out.println("Stack:");
- System.out.println("-----");
- for(i = 0; i <= stackPointer; i++){
- System.out.println(stack[i]);
- }
- System.out.println("-----");
- }
- protected static void loadGraph(String file){
- String parsed[], node, sign, dst;
- char string[];
- int i = 0;
- int src;
- BufferedReader br;
- try {
- if(file.equals("")){
- br = new BufferedReader(new InputStreamReader(System.in));
- }else{
- br = new BufferedReader(new FileReader(file));
- }
- parsed = br.readLine().split(" ");
- v = Integer.parseInt(parsed[0]);
- e = Integer.parseInt(parsed[1]);
- graph = new String[v];
- long startTime = System.nanoTime();
- for(i = 0; i < e; i++){
- StringTokenizer st = new StringTokenizer(br.readLine());
- src = Integer.parseInt(st.nextToken());
- dst = st.nextToken();
- sign = st.nextToken();
- node = graph[src];
- builder.setLength(0);
- if(node != null){
- builder.append(node);
- builder.append(" ");
- }
- if(Integer.parseInt(sign) == 1){
- builder.append("*");
- }
- builder.append(dst);
- graph[src] = builder.toString();
- }
- long estimatedTime = System.nanoTime() - startTime;
- } catch (IOException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- protected static void printGraph() {
- int i;
- for(i = 0; i < graph.length ; i++){
- System.out.println(i + " : " + graph[i]);
- }
- }
- protected static void printCondensedGraph() {
- int i;
- for(i = 0; i < condensedGraph.length ; i++){
- System.out.println(i + " : " + condensedGraph[i]);
- }
- }
- protected static void printColors() {
- int i;
- for(i = 0; i < nodeColors.length ; i++){
- System.out.println(i + " color: " + nodeColors[i]);
- }
- }
- protected static void printArray(int array[]) {
- int i;
- for(i = 0; i < array.length ; i++){
- System.out.println(i + ": " + array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement