Advertisement
phanindhar1

Q3

Mar 24th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package hackathon;
  2. //Phanindhar Bodla
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Eulearian {
  8.     static int first[];
  9.     static int last[];
  10.     static boolean a[][];
  11.     static boolean flag[];
  12.  
  13.     public static void main(String[] args) throws NumberFormatException,
  14.             IOException {
  15.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.         int cases = Integer.parseInt(br.readLine());
  17.         int num;
  18.         String input[];
  19.         int firstAt;
  20.         int lastAt;
  21.         while (cases-- > 0) {
  22.             first = new int[100];
  23.             last = new int[100];
  24.             flag = new boolean[100];
  25.             a = new boolean[100][100];
  26.             num = Integer.parseInt(br.readLine());
  27.             for (int i = 0; i < num; i++) {
  28.                 input = br.readLine().split(" ");
  29.                 firstAt = Integer.parseInt(input[0]);
  30.                 lastAt = Integer.parseInt(input[1]);
  31.                
  32.                 first[firstAt]++;
  33.                
  34.                 last[lastAt]++;
  35.                 flag[firstAt] = true;
  36.                 flag[lastAt] = true;
  37.                 a[firstAt][lastAt] = true;
  38.             }
  39.             solve();
  40.         }
  41.     }
  42.  
  43.     private static void solve() {
  44.         int start = check();
  45.         int wc = 0;
  46.         int cnt = 0;
  47.         int i = 0;
  48.         boolean isPossible = false;
  49.         boolean visited[] = new boolean[100];
  50.         if (start != -1) {
  51.             while (i < 100) {
  52.                 if (flag[i])
  53.                     wc++;// total count of the nodes
  54.                 i++;
  55.             }
  56.             i = 0;
  57.             while (!flag[i] && i < 100) // pointing i to the first node of our graph
  58.                 i++;
  59.             visited[i] = true; // marking it as visited
  60.             connect(i, visited);// this is a method which passes from all the possible edges and mark them visited using Depth first search
  61.             i = 0;
  62.             while (i < 100) { // now we count the number of visited nodes in count cnt
  63.                 if (visited[i])
  64.                     cnt++;
  65.                 i++;
  66.             }
  67.             if (wc == cnt)
  68.                 System.out.println("yes"); // visited count  matches total count
  69.             else
  70.                 System.out.println("no"); // some unvisited nodes exist
  71.  
  72.             isPossible = true;
  73.  
  74.         }
  75.         if (!isPossible)
  76.             System.out.println("no");
  77.     }
  78.  
  79.     private static void connect(int ii, boolean[] visited) {
  80.         for (int i = 0; i < 100; i++) {
  81.             if (a[i][ii] || a[ii][i]) {
  82.                 if (!visited[i]) {
  83.                     visited[i] = true;
  84.                     connect(i, visited);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     private static int check() {
  91.         int start = -1;
  92.         int end = -1;
  93.         int diff;
  94.         for (int i = 0; i < 100; i++) {
  95.             diff = first[i] - last[i];// difference between no of occurrences at start and end point
  96.             if (diff == 1) { // shows the sign of a the starting point occurrence
  97.                 if (start == 1) // which means we already got a start point
  98.                     return -1; // multiple start point not possible in a trail
  99.                 start = 1;// here i is the stating point
  100.                 continue;
  101.             }
  102.             if (diff == -1) { // shows the sign of a the ending point occurrence
  103.                 if (end == 1) //   which means we already got a end point
  104.                     return -1; //  multiple end point not possible in a trail
  105.                 end = 1;// here i is the ending point
  106.                 continue;
  107.             }
  108.             if (diff > 0) { // diff can't be more than zero for non start and end points
  109.                 return -1; // not a trail
  110.             }
  111.         }
  112.         return 0; // could be a trail
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement