Advertisement
ann_candle

Минимизация авт. Мили

Sep 22nd, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.38 KB | None | 0 0
  1. package main
  2. import (
  3.         "fmt"
  4.     "github.com/skorobogatov/input"
  5. )
  6. type Stack struct {
  7.     data [] int
  8.     top int
  9.     capacity int
  10. }
  11.  
  12. func (st Stack) isEmpty ()bool {
  13.     return (st.top == 0)
  14. }
  15. func (st *Stack) push (x int) {
  16.     if st.top == st.capacity {
  17.         st.capacity*=2
  18.         dub := make([] int, st.capacity)
  19.         copy(dub, st.data)
  20.         st.data = dub
  21.     }
  22.     st.data[st.top] = x
  23.     st.top++
  24. }
  25. func (st *Stack) pop () int {
  26.     st.top--
  27.     return st.data[st.top]
  28. }
  29. func dfs (graph []Cond, n int, first int, m int) ([]Cond, int)  {
  30.     var st Stack
  31.     st.data = make([] int, n)
  32.     st.top = 0
  33.     st.capacity = n
  34.     number := 0
  35.     (&st).push(first)
  36.     for !st.isEmpty() {
  37.         v := st.pop()
  38.         if graph[v].mark == -1 {
  39.             graph[v].mark = 0
  40.             graph[v].num = number
  41.             number++
  42.             (&st).push(v)
  43.             for i:=m-1; i>=0; i-- {
  44.                 u := graph[v].edges[i].dest
  45.                 if graph[u].mark == -1 {
  46.                     (&st).push(u)
  47.                 }
  48.             }
  49.         } else {
  50.             if graph[v].mark == 0 {
  51.                 graph[v].mark = 1
  52.             }
  53.         }
  54.     }
  55.     return graph, number
  56. }
  57.  
  58. type Edge struct {
  59.     dest int
  60.     label string
  61. }
  62.  
  63. type Cond struct {
  64.     i int
  65.     parent *Cond
  66.     depth int
  67.     edges []Edge
  68.     num int
  69.     mark int
  70. }
  71.  
  72. func ( t *Cond) makeSet () {
  73.     t.parent = t
  74.     t.depth = 0
  75. }
  76. func (t *Cond) find () *Cond {
  77.     if t.parent == t {
  78.         return t
  79.     } else {
  80.         t.parent  = t.parent.find()
  81.         return t.parent
  82.     }
  83. }
  84. func union (x, y *Cond) {
  85.     rx := x.find()
  86.     ry := y.find()
  87.     if rx.depth < ry.depth {
  88.         rx.parent = ry
  89.     } else {
  90.         ry.parent = rx
  91.         if (rx.depth == ry.depth)&&(rx != ry) {
  92.             rx.depth ++
  93.         }
  94.     }
  95. }
  96.  
  97. func split1 (graph []Cond, n, m int, delta [][]int, fi [][]string) (int, []*Cond) {
  98.     for i:=0; i<n; i++ {
  99.         (&graph[i]).makeSet()
  100.         graph[i].i = i
  101.     }
  102.     count := n
  103.     for i:=0; i<n; i++ {
  104.         for j:=0; j<n; j++ {
  105.                 if (&graph[i]).find() != (&graph[j]).find() {
  106.                     eq := true
  107.                     for k:=0; k<m; k++ {
  108.                         if fi[i][k] != fi[j][k] {
  109.                             eq = false
  110.                             break
  111.                         }
  112.                     }
  113.                     if eq {
  114.                         union (&graph[i], &graph[j])
  115.                         count --
  116.                     }
  117.                 }
  118.         }
  119.     }
  120.     pi := make ([] *Cond, n)
  121.     for i:=0; i<n; i++ {
  122.         pi[i] = (&graph[i]).find()
  123.     }
  124.     return count, pi
  125. }
  126.  
  127. func split (graph []Cond, m, n int, delta [][]int, pi []*Cond) (int, []*Cond) {
  128.     count := n
  129.     for i:=0; i<n; i++ {
  130.         graph[i].parent = &graph[i]
  131.         graph[i].depth = 0
  132.     }
  133.     for i:=0; i<n; i++ {
  134.         for j:=0; j<n; j++ {
  135.             if (pi[i] == pi[j])&&((&graph[i]).find() != (&graph[j]).find()) {
  136.                 eq := true
  137.                 for k:=0; (k<m)&&eq; k++ {
  138.                     if pi[ delta [i][k] ] != pi[ delta[j][k] ] {
  139.                         eq = false
  140.                     }
  141.                 }
  142.                 if eq {
  143.                     union(&graph[i], &graph[j])
  144.                     count--
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     for i:=0; i<n; i++ {
  150.         pi[i] = graph[i].find()
  151.     }
  152.     return count, pi
  153. }
  154.  
  155. func belong (arr []Cond, q Cond, n int) bool {
  156.     for i:=0; i<n; i++ {
  157.         if (arr[i].i == q.i)&&(arr[i].parent == q.parent)&&(arr[i].depth == q.depth) {
  158.             return true
  159.         }
  160.     }
  161.     return false
  162. }
  163.  
  164. func minimize (graph []Cond, delta [][]int, fi [][]string, n, m, first int) ([]Cond, [][]int, [][]string, int, int) {
  165.     count, pi := split1(graph, n, m, delta, fi)
  166.     var c int
  167.     for ; ; {
  168.         c, pi = split(graph, m, n, delta, pi)
  169.         if count == c {
  170.             break
  171.         }
  172.         count = c
  173.     }
  174.     var (
  175.         min = make([]Cond, count)
  176.         dmin = make ([][]int, count)
  177.         fmin = make ([][]string, count)
  178.         k int
  179.     )
  180.     for i:=0; i<count; i++ {
  181.         dmin[i] = make([]int, m)
  182.         fmin[i] = make([]string, m)
  183.     }
  184.     k = 0
  185.     for i:=0; i<n; i++ {
  186.         if (graph[i].i == pi[i].i)&&(graph[i].parent == pi[i].parent)&&(graph[i].depth == pi[i].depth) {
  187.             graph[i].i = k
  188.             k++
  189.         } else {
  190.             graph[i].i = pi[i].i
  191.             }
  192.     }
  193.     k=0
  194.     for i:=0; i<n; i++ {
  195.         q1 := *pi[i]
  196.         if !belong(min, q1, k) {
  197.             min[k] = q1
  198.             for j:=0; j<m; j++ {
  199.                 dmin[k][j] = pi[ delta[i][j] ].i
  200.                 fmin[k][j] = fi[i][j]
  201.             }
  202.             k++
  203.         }
  204.     }
  205.     first = pi[first].i
  206.     return min, dmin, fmin, count, first
  207. }
  208.            
  209.    
  210.  
  211. func main() {
  212.     var n, m, first, count int
  213.     input.Scanf("%d\n%d\n%d\n", &n, &m, &first)
  214.     var delta  = make([][]int, n)
  215.     for i:=0; i<n; i++{
  216.         delta[i] = make([]int, m)
  217.     }
  218.     var fi = make([][]string, n)
  219.     for i:=0; i<n; i++{
  220.         fi[i] = make([]string, m)
  221.     }
  222.     for i:=0; i<n; i++ {
  223.         for j:=0; j<m; j++ {
  224.             input.Scanf("%d ", &delta[i][j])
  225.         }
  226.         input.Scanf("\n")
  227.     }
  228.     for i:=0; i<n; i++ {
  229.         for j:=0; j<m; j++ {
  230.             input.Scanf("%s ", &fi[i][j])
  231.         }
  232.         input.Scanf("\n")
  233.     }
  234.     var graph = make([]Cond, n)
  235.     graph, delta, fi, n, first = minimize(graph, delta, fi, n, m, first)
  236.     for i:=0; i<n; i++ {
  237.         graph[i].edges = make([]Edge, m)
  238.         graph[i].num = -1
  239.         graph[i].mark = -1
  240.         for j:=0; j<m; j++ {
  241.             graph[i].edges[j] = Edge{ delta[i][j], fi[i][j] }
  242.         }
  243.     }
  244.     graph, count = dfs (graph, n, first, m)
  245.     delta = make([][]int, count)
  246.     for i:=0; i<count; i++{
  247.         delta[i] = make([]int, m)
  248.     }
  249.     fi = make([][]string, count)
  250.     for i:=0; i<count; i++{
  251.         fi[i] = make([]string, m)
  252.     }
  253.     for i:=0; i<n; i++ {
  254.         num1 := graph[i].num
  255.         if num1!=-1 {
  256.             for j:=0; j<m; j++ {
  257.                 num2 := graph[ graph[i].edges[j].dest ].num
  258.                 delta[num1][j] = num2
  259.                 fi[num1][j] = graph[i].edges[j].label
  260.             }
  261.         }
  262.     }
  263.     n = count
  264.     fmt.Printf("digraph {\n rankdir = LR\n dummy [label = \"\", shape = none]\n")
  265.     for i:=0; i<n; i++ {
  266.         fmt.Printf(" %d [shape = circle]\n", i)
  267.     }
  268.     fmt.Printf("dummy -> %d\n", 0)
  269.    
  270.     for i:=0; i<n; i++ {
  271.         for j:=0; j<m; j++ {
  272.             fmt.Printf ("%d -> %d [label = \"%c(%s)\"]\n", i, delta[i][j], byte ( int('a') + j), fi[i][j])
  273.         }
  274.     }
  275.     fmt.Printf("}")
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement