Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- type Vertex struct {
- edges []int
- color int
- used int
- vertexes int
- edge int
- }
- func filling (g []Vertex, vertex int) {
- for i:=0; i < vertex; i++{
- g[i].used=-1
- g[i].vertexes=0
- g[i].edge=0
- g[i].color=-1
- }
- }
- func filling_edge(edge int, g []Vertex) {
- first_vortex := 0; second_vertex:= 0
- for i:=0; i < edge; i++{
- fmt.Scan(&first_vortex,&second_vertex)
- g[first_vortex].edges = append(g[first_vortex].edges,second_vertex)
- if first_vortex == second_vertex {
- continue
- }else {
- g[second_vertex].edges = append(g[second_vertex].edges,first_vortex)
- }
- }
- }
- func display(vertex, color int, g []Vertex){
- fmt.Print("graph {\n")
- for i :=0 ; i < vertex; i++{
- fmt.Printf("\t%d",i)
- if g[i].used == color{fmt.Print(" [color = red]")}
- fmt.Print("\n")
- }
- for i :=0 ; i < vertex; i++{
- flag := g[i].used == color
- for j := 0; j < len(g[i].edges); j++{
- if g[i].edges[j] >= i{
- fmt.Printf("\t%d -- %d",i,g[i].edges[j])
- if flag{fmt.Print(" [color = red]")}
- fmt.Print("\n")
- }
- }
- }
- fmt.Print("}")
- }
- func main(){
- var dfs func(v , anc int, color int)
- var vertex, edge, color int
- fmt.Scan(&vertex,&edge)
- graph := make([]Vertex,vertex)
- filling(graph, vertex)
- filling_edge(edge, graph)
- dfs = func(v, anc int, color int) {
- graph[v].used = color
- graph[color].vertexes++
- for _,i := range graph[v].edges{
- if graph[i].used == color{
- graph[color].edge++
- }
- if graph[i].used == -1{
- graph[color].edge++;dfs(i,v,color)
- }
- }
- }
- color = 0
- for i:=0;i<vertex; i++ {
- if graph[i].used == -1{
- dfs(i,-1, color)
- color++
- }
- }
- max_verts := 0; max_edges := 0
- for i:=0; i < vertex; i++{
- if max_verts < graph[i].vertexes || (max_verts == graph[i].vertexes && max_edges < graph[i].edge) {
- color = i
- max_verts = graph[i].vertexes
- max_edges = graph[i].edge
- }
- for j:=0; j< vertex;j++{
- break
- }
- }
- display(vertex, color,graph)
- }
Add Comment
Please, Sign In to add comment