woonie

Hospital.java

Sep 23rd, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import java.util.*;
  2. class Hospital {
  3.     private int V; // number of vertices in the graph (number of rooms in the hospital)
  4.     private ArticPointDFS AdjList; // the graph (the hospital)
  5.     private Vector<Integer> RatingScore; // the weight of each vertex (rating score of each room)
  6.     class Vertex {    
  7.         int id, low, dfsnum, dfslevel, numChildren;
  8.         Vertex parent;
  9.         LinkedList<Integer> eList;  // list of edges incident to this vertex
  10.         // Create a vertex with given ID number
  11.         Vertex(int x) {
  12.             parent = null;
  13.             id = x;
  14.             dfsnum = -1;  // initially undiscovered
  15.             eList = new LinkedList<Integer>();  // create empty list for adjacency edges
  16.         }    
  17.     }
  18.     class Edge {      
  19.         Vertex from, to;   // two vertices incident to this edge      
  20.         // Create an edge with given vertices
  21.         Edge(Vertex a, Vertex b) {
  22.             from = a;  to = b;
  23.         }
  24.         // Return the string repesent'n of the edge in form of a pair of vertices
  25.         String getEdge() {
  26.             return "("+from.id+","+to.id+")";
  27.         }
  28.         // Check if two edges are the same by comparing two vertices in edges
  29.         boolean equal(Vertex a, Vertex b) {
  30.             return ((from.id == a.id) && (to.id == b.id));
  31.         }      
  32.     }
  33.  
  34.     public class ArticPointDFS {
  35.         private Vertex[] vArray; // an array which represents a graph
  36.         private int numVertices; // total number of vertices in a graph
  37.         private int dfsCounter = 1;  // will be used to assign dfsNum to each vertex
  38.         private ArrayList<Vertex> articPointList;
  39.         Vertex dfsRoot;
  40.         int rootChildren;
  41.         public ArticPointDFS(int n) {
  42.             numVertices = n;      
  43.             // Create an array to set up all vertices in a graph
  44.             vArray = new Vertex[numVertices];
  45.             for(int i=0; i < numVertices; i++) {
  46.                 vArray[i] = new Vertex(i);
  47.             }
  48.             articPointList = new ArrayList<Vertex>();
  49.         }
  50.         public void addEdge(int from, int to) {            
  51.             Vertex v1 = vArray[from];
  52.             Vertex v2 = vArray[to];
  53.             if (v1.eList.contains(v2)){
  54.                 return;
  55.             }
  56.             // Add the edge to the adjacent edge list of both vertices
  57.             v1.eList.add(to);
  58.             v2.eList.add(from);
  59.         }
  60.         public void doArticPointDFS(Vertex vertex) {
  61.             // Set DFS info of vertex 'vertex'      
  62.             vertex.dfsnum = dfsCounter++;
  63.             //System.out.println(vertex.id + " " + vertex.dfsnum);
  64.             vertex.low = vertex.dfsnum;
  65.             Iterator<Integer> neighbourIter = vertex.eList.iterator();
  66.             while(neighbourIter.hasNext()) {
  67.                 int idxNeighbour = neighbourIter.next();
  68.                 Vertex neighbour = vArray[idxNeighbour];
  69.                 if (neighbour.dfsnum == -1) {  // this neighbour is undiscovered
  70.                     neighbour.parent = vertex;
  71.                     neighbour.dfslevel = vertex.dfslevel + 1;
  72.                     vertex.numChildren++;                    
  73.                     doArticPointDFS(neighbour);  // recursively perform DFS at children nodes
  74.                     if (vertex.dfsnum == 1) {
  75.                         // Special Case for root
  76.                         // Root is an artic. point iff there are two or more children  
  77.                         if (vertex.numChildren >=2) {
  78.                             if (!articPointList.contains(vertex)) {
  79.                                 articPointList.add(vertex);                      
  80.                             }                                  
  81.                         }
  82.                     }
  83.                     else{
  84.                         if (neighbour.low >= vertex.dfsnum) {
  85.                             // v is artic. point seperating x. That is, children of v
  86.                             // cannot climb higher than v without passing through v.
  87.                             if (!articPointList.contains(vertex)) {
  88.                                 articPointList.add(vertex);                      
  89.                             }
  90.                         }
  91.                     }
  92.                     vertex.low = Math.min(vertex.low, neighbour.low);
  93.                 }
  94.                 //else if (neighbour.dfslevel < vertex.dfslevel - 1) {
  95.                 else if (neighbour != vertex.parent){
  96.                     // x is at a lower level than the level of v's parent.
  97.                     // equiv. (v,x) is a back edge
  98.                     vertex.low = Math.min(vertex.low, neighbour.dfsnum);                  
  99.                 }        
  100.             }    
  101.         }
  102.     }
  103.     int Query() {
  104.         int answer = -1;
  105.        
  106.         //Vertex startVertex = AdjList.vArray[0];  
  107.         //AdjList.doArticPointDFS(startVertex);  // Perform aritc. point search
  108.         if (AdjList.articPointList.isEmpty()) {
  109.             return answer;
  110.         }
  111.         else {
  112.             Iterator<Vertex> iter1 = AdjList.articPointList.iterator();
  113.             while (iter1.hasNext()) {
  114.                 Vertex v = iter1.next();
  115.                 if ((answer == -1) || ((answer != -1) && (answer > RatingScore.elementAt(v.id)))){
  116.                     answer = RatingScore.elementAt(v.id);;
  117.                 }
  118.             }
  119.         }
  120.         return answer;
  121.     }
  122.     void run() {
  123.         // do not alter this method
  124.         Scanner sc = new Scanner(System.in);
  125.         int TC = sc.nextInt(); // there will be several test cases
  126.         while (TC-- > 0) {
  127.             V = sc.nextInt();
  128.             // read rating scores, A (index 0), B (index 1), C (index 2), ..., until the V-th index
  129.               RatingScore = new Vector < Integer > ();
  130.               for (int i = 0; i < V; i++){
  131.                 RatingScore.add(sc.nextInt());
  132.               }
  133.             // clear the graph and read in a new graph as Adjacency List
  134.             AdjList = new ArticPointDFS(V);
  135.             for (int i = 0; i < V; i++) {
  136.                 int k = sc.nextInt(); //number of edges from node i
  137.                 while (k-- > 0) {
  138.                     int j = sc.nextInt(); //destination
  139.                     AdjList.addEdge(i,j);
  140.                 }
  141.             }
  142.             Vertex[] vertexArray = AdjList.vArray;
  143.  
  144.             for (int o = 0; o < vertexArray.length; o++){
  145.                 if (vertexArray[o].dfsnum == -1){
  146.                     AdjList.doArticPointDFS(vertexArray[o]);
  147.                 }
  148.             }
  149.             System.out.println(Query());
  150.         }
  151.        
  152.     }
  153.     public static void main(String[] args) {
  154.         // do not alter this method
  155.         Hospital ps3 = new Hospital();
  156.         ps3.run();
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment