vadimk772336

Untitled

Mar 14th, 2022 (edited)
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct my_pair
  4. {
  5.     int a;
  6.     int b;
  7. };
  8.  
  9. struct vertex
  10. {
  11.     bool isterminal;
  12.     int adj_list[26];
  13.     bool is_exist[26];
  14. };
  15.  
  16. struct cartesian_vertex
  17. {
  18.     int u;
  19.     int v;
  20.     bool isterminal;
  21.     bool isvisited;
  22.     struct my_pair adj_list[26];
  23.     bool is_exist[26];
  24. };
  25.  
  26. void addEdge(int i, int j, char symbol, struct vertex* DFA)
  27. {
  28.     int symbol_idx = int(symbol) - 97;
  29.     DFA[i].adj_list[symbol_idx] = j;
  30.     DFA[i].is_exist[symbol_idx] = true;
  31. }
  32.  
  33. void Fill_DFA(int n, int k, int l, struct vertex* DFA)
  34. {
  35.     int u, v;
  36.     char c;
  37.     int terminals[k];
  38.  
  39.     for (int i = 0; i < k; ++i)
  40.         std::cin >> terminals[i];
  41.  
  42.     for (int i = 0; i < k; ++i)
  43.         DFA[terminals[i]].isterminal = true;
  44.  
  45.     for (int i = 0; i < n * l; ++i)
  46.     {
  47.         std::cin >> u >> c >> v;
  48.         addEdge(u, v, c, DFA);
  49.     }
  50.  
  51.     return;
  52. }
  53.  
  54. void clean_DFA(struct vertex* DFA, int n)
  55. {
  56.     for (int i = 0; i < n; ++i)
  57.     {
  58.         DFA[i].isterminal = false;
  59.         for (int j = 0; j < 26; ++j)
  60.         {
  61.             DFA[i].adj_list[j] = -1;
  62.             DFA[i].is_exist[j] = false;
  63.         }
  64.     }
  65. }
  66.  
  67. void clean_DFA_two(struct cartesian_vertex* DFA, int n)
  68. {
  69.     for (int i = 0; i < n; ++i)
  70.     {
  71.         DFA[i].isterminal = false;
  72.         DFA[i].isvisited = false;
  73.         for (int j = 0; j < 26; ++j)
  74.             DFA[i].is_exist[j] = false;
  75.     }
  76. }
  77.  
  78. void cartesian_product(struct cartesian_vertex* dec_graph, struct vertex* DFA_one,
  79.     struct vertex* DFA_two, int n_one, int n_two)
  80. {
  81.  
  82.     int N = n_one * n_two;
  83.     cartesian_vertex buff;
  84.     struct my_pair dec_ver;
  85.  
  86.     clean_DFA_two(dec_graph, N);
  87.  
  88.     for (int i = 0; i < n_one; ++i)
  89.     {
  90.         for (int j = 0; j < n_two; ++j)
  91.         {
  92.             int idx = i * n_two + j;
  93.  
  94.             dec_graph[idx].u = i;
  95.             dec_graph[idx].v = j;
  96.             dec_graph[idx].isterminal = (DFA_one[i].isterminal != DFA_two[j].isterminal);
  97.  
  98.             for (int k = 0; k < 26; ++k)
  99.             {
  100.                 if (DFA_one[i].is_exist[k] && DFA_two[j].is_exist[k])
  101.                 {
  102.                     dec_ver.a = DFA_one[i].adj_list[k];
  103.                     dec_ver.b = DFA_two[j].adj_list[k];
  104.                     dec_graph[idx].adj_list[k] = dec_ver;
  105.                     dec_graph[idx].is_exist[k] = true;
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void DFS(int v_idx, struct cartesian_vertex* graph, bool& is_equiv, int n_two)
  113. {
  114.  
  115.     int a, b;
  116.     if (is_equiv)
  117.     {
  118.         if (graph[v_idx].isterminal)
  119.         {
  120.             is_equiv = false;
  121.             return;
  122.         }
  123.  
  124.         graph[v_idx].isvisited = true;
  125.         for (int i = 0; i < 26; ++i)
  126.         {
  127.             if (graph[v_idx].is_exist[i])
  128.             {
  129.                 a = graph[v_idx].adj_list[i].a;
  130.                 b = graph[v_idx].adj_list[i].b;
  131.                 int idx = a * n_two + b;
  132.                 if (not graph[idx].isvisited)
  133.                     DFS(idx, graph, is_equiv, n_two);
  134.             }
  135.         }
  136.     }
  137.     return;
  138. }
  139.  
  140. int main()
  141. {
  142.  
  143.     int n_one, k_one, l_one;
  144.     int n_two, k_two, l_two;
  145.  
  146.     std::cin >> n_one >> k_one >> l_one;
  147.     vertex DFA_one[n_one];
  148.     clean_DFA(DFA_one, n_one);
  149.     Fill_DFA(n_one, k_one, l_one, DFA_one);
  150.  
  151.     std::cin >> n_two >> k_two >> l_two;
  152.     vertex DFA_two[n_two];
  153.     clean_DFA(DFA_two, n_two);
  154.     Fill_DFA(n_two, k_two, l_two, DFA_two);
  155.  
  156.     int N = n_one * n_two;
  157.     struct cartesian_vertex dec_graph[N];
  158.     cartesian_product(dec_graph, DFA_one, DFA_two, n_one, n_two);
  159.  
  160.     bool is_equiv = true;
  161.     DFS(0, dec_graph, is_equiv, n_two);
  162.  
  163.     if (is_equiv)
  164.         std::cout << "EQUIVALENT";
  165.     else
  166.         std::cout << "NOT EQUIVALENT";
  167.  
  168.     return 0;
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment