Advertisement
osipyonok

Untitled

Jan 16th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  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 Main
  9. {
  10.     public static void main (String[] args) throws java.lang.Exception
  11.     {
  12.         Scanner sc = new Scanner(System.in);
  13.         System.out.print("fdsfs");
  14.         int a = sc.nextInt();
  15.         int s = sc.nextInt();
  16.         Set<Integer> S = new HashSet();
  17.         for(int i = 1 ; i <= s ; ++i)S.add(i);
  18.         int s0 = sc.nextInt();
  19.     //    --s0;
  20.         int sz_f = sc.nextInt();
  21.         Set<Integer> F = new HashSet();
  22.         for(int i = 0 ; i < sz_f ; ++i){
  23.             int t = sc.nextInt();
  24.        //     --t;
  25.             F.add(t);
  26.         }
  27.         Map<Pair<Integer , Character> , ArrayList<Integer>> f = new HashMap();
  28.         int k = sc.nextInt();
  29.         while(k-->0){
  30.             int u = sc.nextInt();
  31.             char ch = sc.next().charAt(0);
  32.             int v = sc.nextInt();
  33.         //    --u;
  34.          //   --v;
  35.             Pair<Integer , Character> fnd = new Pair<Integer , Character>(u , ch);
  36.             if(!f.containsKey(fnd)){
  37.                 //    System.out.println("fdsfsd");
  38.                 f.put(fnd, new ArrayList<Integer>());
  39.             }
  40.             ArrayList<Integer> tmp = f.get(fnd);
  41.             //tmp.add(v);
  42.             tmp.add(v);
  43.             f.put(fnd, tmp);
  44.         }
  45.         Automata<Integer> aut = new Automata<Integer>(a , S , s0 , F , f);
  46.         aut.deadlock();
  47.         aut.unattainable();
  48.     }
  49. }
  50.  
  51. class Automata<T > {
  52.     int A;
  53.     T s;
  54.     Set<T> S = new HashSet<T>();
  55.     Set<T> F = new HashSet<T>();
  56.     Map<Pair<T , Character> , ArrayList<T>> f = new HashMap();
  57.     Map<Pair<T , Character> , ArrayList<T>> inverse_f = new HashMap();
  58.   //  Map<Pair<T , T> , ArrayList<Character>> inverse_f = new HashMap();
  59.  
  60.     Automata(int _A , Set<T> _S , T _s , Set<T> _F , Map<Pair<T , Character> , ArrayList<T>> _f){
  61.         A = _A;
  62.         s = _s;
  63.         S = _S;
  64.         F = _F;
  65.         f = _f;
  66.         for(Map.Entry<Pair<T , Character> , ArrayList<T>> i : f.entrySet()){
  67.       //
  68.             for(T to : i.getValue()){
  69.                 ArrayList<T> tmp = new ArrayList<T>();
  70.                 if(inverse_f.containsKey(new Pair<T , Character>(to , i.getKey().second()))){
  71.                     tmp = inverse_f.get(new Pair<T , Character>(to , i.getKey().second()));
  72.                 }
  73.                 tmp.add(i.getKey().first());
  74.                 inverse_f.put(new Pair<T , Character>(to , i.getKey().second()) , tmp);
  75.             }
  76.         }
  77.     }
  78.  
  79.     public String toString(){
  80.         String res = "";
  81.         res += A;
  82.         res += '\n';
  83.         res += S.size() + "\n";
  84.         res += S.toString() + "\n";
  85.         res += s.toString() + "\n";
  86.         res += F.size() + "\n";
  87.         res += F.toString() + "\n";
  88.         res += f.toString();
  89.         return res;
  90.     }
  91.  
  92.     public void deadlock(){
  93.         Map<T , Boolean> used = new HashMap();
  94.         for(T u : F){
  95.             dfs2(used , u);
  96.         }
  97.         for(T v : S){
  98.             boolean ck = true;
  99.             if(!used.containsKey(v)){
  100.                 ck = false;
  101.             }else{
  102.                 if(used.get(v) == false){
  103.                     ck = false;
  104.                 }
  105.             }
  106.             if(ck == false){
  107.                 System.out.println(v.toString() + " - deadlock");
  108.             }
  109.         }
  110.     }
  111.  
  112.     public void unattainable(){
  113.         //boolean used[] = new boolean[S.size() + 1];
  114.         Map<T , Boolean> used = new HashMap();
  115.         dfs1(used , s);
  116.         for(T v : S){
  117.             boolean ck = true;
  118.             if(!used.containsKey(v)){
  119.                 ck = false;
  120.             }else{
  121.                 if(used.get(v) == false){
  122.                     ck = false;
  123.                 }
  124.             }
  125.             if(ck == false){
  126.                 System.out.println(v.toString() + " is unattainable");
  127.             }
  128.         }
  129.     }
  130.  
  131.     private void dfs1(Map<T , Boolean> used , T cur){
  132.         used.put(cur , true);
  133.         for(char c = 'a' ; c < 'a' + A ; ++c){
  134.             if(f.containsKey(new Pair<T, Character>(cur , c))){
  135.                 ArrayList<T> to = f.get(new Pair<T,Character>(cur , c));
  136.                 for(T v : to){
  137.                     if(!used.containsKey(v)){
  138.                         dfs1(used , v);
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     private  void dfs2(Map<T , Boolean> used , T cur){
  146.         if(used.containsKey(cur))return;
  147.         used.put(cur , true);
  148.         for(char c = 'a' ; c < 'a' + A ; ++c){
  149.             if(inverse_f.containsKey(new Pair<T, Character>(cur , c))){
  150.                 ArrayList<T> to = inverse_f.get(new Pair<T,Character>(cur , c));
  151.                 for(T v : to){
  152.                     if(!used.containsKey(v)){
  153.                         dfs2(used , v);
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.  
  161.  
  162. }
  163.  
  164. class Pair<T1 , T2> {
  165.         T1 x;
  166.         T2 y;
  167.         Pair(T1 _x , T2 _y){
  168.         x = _x;
  169.         y = _y;
  170.         }
  171.         T1 first(){
  172.         return x;
  173.         }
  174.         T2 second(){
  175.         return y;
  176.         }
  177.  
  178. public boolean equals(Object other){
  179.         if (other instanceof Pair) {
  180.         Pair otherPair = (Pair) other;
  181.         return
  182.         ((  this.x == otherPair.x ||
  183.         ( this.x != null && otherPair.x != null &&
  184.         this.x.equals(otherPair.x))) &&
  185.         (  this.y == otherPair.y ||
  186.         ( this.y != null && otherPair.y != null &&
  187.         this.y.equals(otherPair.y))) );
  188.         }
  189.         return false;
  190.         }
  191.  
  192. public int hashCode(){
  193.         int hash = 31;
  194.         int res = 1;
  195.         res = hash * res + x.hashCode();
  196.         res = hash * res + y.hashCode();
  197.         return res;
  198.         }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement