Advertisement
osipyonok

Удалялка eps переходов

Jan 4th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2. // By Max Osipyonok
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.     static int a , s , s0 , sz_f;
  11.     static Set<Integer> F;
  12.  
  13.     public static void main (String[] args) throws java.lang.Exception
  14.     {
  15.         Scanner sc = new Scanner(System.in);
  16.         a = sc.nextInt();
  17.         s = sc.nextInt();
  18.         s0 = sc.nextInt();
  19.         --s0;
  20.         sz_f = sc.nextInt();
  21.         F = new HashSet<Integer>();
  22.         Queue<Integer> q = new PriorityQueue();
  23.         for(int i = 0 ; i < sz_f ; ++i){
  24.             int t = sc.nextInt();
  25.             --t;
  26.             F.add(t);
  27.             q.add(t);
  28.         }
  29.         //Set<Character>[][] w = new Set<Character>[s + 1][s + 1];
  30.         List<List<Set<Character>>> w = new ArrayList<List<Set<Character>>>();
  31.         for(int i = 0 ; i < s ; ++i){
  32.             List<Set<Character>> tmp = new ArrayList<Set<Character>>();
  33.             for(int j = 0 ; j < s ; ++j){
  34.                 Set<Character> tmp2 = new HashSet<Character>();
  35.                 tmp.add(tmp2);
  36.             }
  37.             w.add(tmp);
  38.         }
  39.         while(sc.hasNext()){
  40.             int u , v;
  41.             char ch;
  42.             u = sc.nextInt();
  43.             ch = sc.next().charAt(0);
  44.             v = sc.nextInt();
  45.             --u;
  46.             --v;
  47.             w.get(u).get(v).add(ch);
  48.         }
  49.         for(int i = 0 ; i < s ; ++i){
  50.             for(int j = 0 ; j < s ; ++j){
  51.                 if(w.get(i).get(j).size() == 0){
  52.                     w.get(i).get(j).add('A');
  53.                 }
  54.             }  
  55.         }
  56.         for(int k = 0 ; k < s ; ++k){
  57.             for(int i = 0 ; i < s ; ++i){
  58.                 for(int j = 0 ; j < s ; ++j){
  59.                     if(w.get(i).get(j).contains('A'))continue;
  60.                     if(w.get(i).get(k).contains('*') && w.get(k).get(j).contains('*')){
  61.                         w.get(i).get(j).add('*');
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         while(q.size() > 0){
  67.             int v = q.element();
  68.             q.remove();
  69.             for(int u = 0 ; u < s ; ++u){
  70.                 if(w.get(u).get(v).contains('*')){
  71.                     if(!F.contains(u)){
  72.                         F.add(u);
  73.                         q.add(u);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         for(int k = 0 ; k < s ; ++k){
  79.             for(int i = 0 ; i < s ; ++i){
  80.                 for(int j = 0 ; j < s ; ++j){
  81.                     if(w.get(i).get(k).contains('*')){
  82.                         for(char cur : w.get(k).get(j)){
  83.                             if(cur != 'A' && cur != '*'){
  84.                                 w.get(i).get(j).add(cur);
  85.                             }
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         for(int i = 0 ; i < s ; ++i){
  92.             for(int j = 0 ; j < s ; ++j){
  93.                 for(char cur : w.get(i).get(j)){
  94.                     if(cur != 'A' && cur != '*'){
  95.                         System.out.println((i + 1) + " " + cur + " " + (j + 1));
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         System.out.print("F = { ");
  101.         Iterator<Integer> it = F.iterator();
  102.         while(it.hasNext()){
  103.             int cur = it.next();
  104.             System.out.print(cur + 1);
  105.             if(it.hasNext()){
  106.                 System.out.print(" , ");
  107.             }
  108.         }
  109.         System.out.print(" }");
  110.     }
  111. }
  112. /*
  113. 2
  114. 4
  115. 1
  116. 2
  117. 1 4
  118. 1 a 2
  119. 1 * 4
  120. 1 b 3
  121. 2 b 2
  122. 2 a 4
  123. 3 a 2
  124. 3 * 1
  125. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement