Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("fdsfs");
- int a = sc.nextInt();
- int s = sc.nextInt();
- Set<Integer> S = new HashSet();
- for(int i = 1 ; i <= s ; ++i)S.add(i);
- int s0 = sc.nextInt();
- // --s0;
- int sz_f = sc.nextInt();
- Set<Integer> F = new HashSet();
- for(int i = 0 ; i < sz_f ; ++i){
- int t = sc.nextInt();
- // --t;
- F.add(t);
- }
- Map<Pair<Integer , Character> , ArrayList<Integer>> f = new HashMap();
- int k = sc.nextInt();
- while(k-->0){
- int u = sc.nextInt();
- char ch = sc.next().charAt(0);
- int v = sc.nextInt();
- // --u;
- // --v;
- Pair<Integer , Character> fnd = new Pair<Integer , Character>(u , ch);
- if(!f.containsKey(fnd)){
- // System.out.println("fdsfsd");
- f.put(fnd, new ArrayList<Integer>());
- }
- ArrayList<Integer> tmp = f.get(fnd);
- //tmp.add(v);
- tmp.add(v);
- f.put(fnd, tmp);
- }
- Automata<Integer> aut = new Automata<Integer>(a , S , s0 , F , f);
- aut.deadlock();
- aut.unattainable();
- }
- }
- class Automata<T > {
- int A;
- T s;
- Set<T> S = new HashSet<T>();
- Set<T> F = new HashSet<T>();
- Map<Pair<T , Character> , ArrayList<T>> f = new HashMap();
- Map<Pair<T , Character> , ArrayList<T>> inverse_f = new HashMap();
- // Map<Pair<T , T> , ArrayList<Character>> inverse_f = new HashMap();
- Automata(int _A , Set<T> _S , T _s , Set<T> _F , Map<Pair<T , Character> , ArrayList<T>> _f){
- A = _A;
- s = _s;
- S = _S;
- F = _F;
- f = _f;
- for(Map.Entry<Pair<T , Character> , ArrayList<T>> i : f.entrySet()){
- //
- for(T to : i.getValue()){
- ArrayList<T> tmp = new ArrayList<T>();
- if(inverse_f.containsKey(new Pair<T , Character>(to , i.getKey().second()))){
- tmp = inverse_f.get(new Pair<T , Character>(to , i.getKey().second()));
- }
- tmp.add(i.getKey().first());
- inverse_f.put(new Pair<T , Character>(to , i.getKey().second()) , tmp);
- }
- }
- }
- public String toString(){
- String res = "";
- res += A;
- res += '\n';
- res += S.size() + "\n";
- res += S.toString() + "\n";
- res += s.toString() + "\n";
- res += F.size() + "\n";
- res += F.toString() + "\n";
- res += f.toString();
- return res;
- }
- public void deadlock(){
- Map<T , Boolean> used = new HashMap();
- for(T u : F){
- dfs2(used , u);
- }
- for(T v : S){
- boolean ck = true;
- if(!used.containsKey(v)){
- ck = false;
- }else{
- if(used.get(v) == false){
- ck = false;
- }
- }
- if(ck == false){
- System.out.println(v.toString() + " - deadlock");
- }
- }
- }
- public void unattainable(){
- //boolean used[] = new boolean[S.size() + 1];
- Map<T , Boolean> used = new HashMap();
- dfs1(used , s);
- for(T v : S){
- boolean ck = true;
- if(!used.containsKey(v)){
- ck = false;
- }else{
- if(used.get(v) == false){
- ck = false;
- }
- }
- if(ck == false){
- System.out.println(v.toString() + " is unattainable");
- }
- }
- }
- private void dfs1(Map<T , Boolean> used , T cur){
- used.put(cur , true);
- for(char c = 'a' ; c < 'a' + A ; ++c){
- if(f.containsKey(new Pair<T, Character>(cur , c))){
- ArrayList<T> to = f.get(new Pair<T,Character>(cur , c));
- for(T v : to){
- if(!used.containsKey(v)){
- dfs1(used , v);
- }
- }
- }
- }
- }
- private void dfs2(Map<T , Boolean> used , T cur){
- if(used.containsKey(cur))return;
- used.put(cur , true);
- for(char c = 'a' ; c < 'a' + A ; ++c){
- if(inverse_f.containsKey(new Pair<T, Character>(cur , c))){
- ArrayList<T> to = inverse_f.get(new Pair<T,Character>(cur , c));
- for(T v : to){
- if(!used.containsKey(v)){
- dfs2(used , v);
- }
- }
- }
- }
- }
- }
- class Pair<T1 , T2> {
- T1 x;
- T2 y;
- Pair(T1 _x , T2 _y){
- x = _x;
- y = _y;
- }
- T1 first(){
- return x;
- }
- T2 second(){
- return y;
- }
- public boolean equals(Object other){
- if (other instanceof Pair) {
- Pair otherPair = (Pair) other;
- return
- (( this.x == otherPair.x ||
- ( this.x != null && otherPair.x != null &&
- this.x.equals(otherPair.x))) &&
- ( this.y == otherPair.y ||
- ( this.y != null && otherPair.y != null &&
- this.y.equals(otherPair.y))) );
- }
- return false;
- }
- public int hashCode(){
- int hash = 31;
- int res = 1;
- res = hash * res + x.hashCode();
- res = hash * res + y.hashCode();
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement