EWTD

5-structured

Sep 20th, 2021 (edited)
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.98 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6. type Vertex struct {
  7.     edges []int
  8.     color int
  9.     used int
  10.     vertexes int
  11.     edge int
  12. }
  13. func filling (g []Vertex, vertex int)  {
  14.     for i:=0; i < vertex; i++{
  15.         g[i].used=-1
  16.         g[i].vertexes=0
  17.         g[i].edge=0
  18.         g[i].color=-1
  19.     }
  20. }
  21.  
  22. func filling_edge(edge int, g []Vertex)  {
  23.     first_vortex := 0; second_vertex:= 0
  24.     for i:=0; i < edge; i++{
  25.         fmt.Scan(&first_vortex,&second_vertex)
  26.         g[first_vortex].edges = append(g[first_vortex].edges,second_vertex)
  27.         if first_vortex == second_vertex {
  28.             continue
  29.         }else {
  30.             g[second_vertex].edges = append(g[second_vertex].edges,first_vortex)
  31.         }
  32.     }
  33. }
  34.  
  35. func display(vertex, color int, g []Vertex){
  36.     fmt.Print("graph {\n")
  37.     for i :=0 ; i < vertex; i++{
  38.         fmt.Printf("\t%d",i)
  39.         if g[i].used == color{fmt.Print(" [color = red]")}
  40.         fmt.Print("\n")
  41.     }
  42.     for i :=0 ; i < vertex; i++{
  43.         flag := g[i].used == color
  44.         for j := 0; j < len(g[i].edges); j++{
  45.             if g[i].edges[j] >= i{
  46.                 fmt.Printf("\t%d -- %d",i,g[i].edges[j])
  47.                 if flag{fmt.Print(" [color = red]")}
  48.                 fmt.Print("\n")
  49.             }
  50.  
  51.         }
  52.     }
  53.     fmt.Print("}")
  54. }
  55.  
  56.  
  57. func main(){
  58.     var  dfs func(v , anc int, color int)
  59.     var vertex, edge, color int
  60.     fmt.Scan(&vertex,&edge)
  61.     graph := make([]Vertex,vertex)
  62.     filling(graph, vertex)
  63.     filling_edge(edge, graph)
  64.     dfs = func(v, anc int, color int) {
  65.         graph[v].used = color
  66.         graph[color].vertexes++
  67.         for _,i := range graph[v].edges{
  68.             if graph[i].used == color{
  69.                 graph[color].edge++
  70.             }
  71.             if graph[i].used == -1{
  72.                 graph[color].edge++;dfs(i,v,color)
  73.             }
  74.  
  75.         }
  76.     }
  77.  
  78.     color = 0
  79.     for i:=0;i<vertex; i++ {
  80.         if graph[i].used == -1{
  81.             dfs(i,-1, color)
  82.             color++
  83.         }
  84.     }
  85.     max_verts := 0; max_edges := 0
  86.     for i:=0; i < vertex; i++{
  87.         if max_verts < graph[i].vertexes || (max_verts == graph[i].vertexes && max_edges < graph[i].edge) {
  88.             color = i
  89.             max_verts = graph[i].vertexes
  90.             max_edges = graph[i].edge
  91.         }
  92.         for j:=0; j< vertex;j++{
  93.             break
  94.         }
  95.     }
  96.     display(vertex, color,graph)
  97. }
Add Comment
Please, Sign In to add comment