We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Got a couple test cases right.
But of course, it times out.
// Hackerrank 2022-06-15 task (GO) Kundu and Tree.txt// Made clever wrong non-brute force solutions 6 dayspackagemainimport("fmt""io/ioutil""os")funcmain(){// graph vertex red adjacency and black adjacency listsvarra,ba[][]int// stdin, parse, fill ra, babytes,err:=ioutil.ReadAll(os.Stdin)iferr!=nil{panic(err)}varaccuintvarinDigitsboolvarnintvaroneintvartwointfor_,b:=rangebytes{ifb>=48&&b<=57{inDigits=trueaccu=accu*10+int(b)-48}else{ifinDigits{one=twotwo=accuaccu=0inDigits=falseifn==0{n=two// slot[0] is never used. Index [1..n].ra=make([][]int,n+1)ba=make([][]int,n+1)continue}}switchb{casebyte('b'):ba[one]=append(ba[one],two)ba[two]=append(ba[two],one)breakcasebyte('r'):ra[one]=append(ra[one],two)ra[two]=append(ra[two],one)break}}}// Discovered-After-Red Listsdarl:=make([][]bool,n+1)// Do BFS from each vertexfors:=1;s<=n;s++{// discoveredd:=make([]bool,n+1)// discovered after reddar:=make([]bool,n+1)// fifof:=make([]int,0)// fifo after redfar:=make([]bool,0)// queue the search keyd[s]=truef=append(f,s)far=append(far,false)// run until dryforlen(f)>0{// dequeue vertexv:=f[0]f=f[1:]// dequeue after red?ar:=far[0]far=far[1:]for_,u:=rangeba[v]{if!d[u]{d[u]=truedar[u]=arf=append(f,u)far=append(far,ar)}}for_,u:=rangera[v]{if!d[u]{d[u]=truedar[u]=truef=append(f,u)far=append(far,true)}}}darl[s]=dar}// test the selftest// good, caught it: darl[3][7] = !darl[7][3]// as a selftest, prove the symmetry of darl// for i := 1; i <= n; i++ {// if darl[i][i] {// fmt.Println("darl[i][i]")// }// for j := i + 1; j <= n; j++ {// if darl[i][j] != darl[j][i] {// fmt.Println("asym")// }// }// }// Okay, I brute-forced it to here. Now what?N:=int64(0)fori:=1;i<=n;i++{forj:=i+1;j<=n;j++{ifdarl[i][j]{fork:=j+1;k<=n;k++{ifdarl[i][k]&&darl[j][k]{N++}}}}}fmt.Println(N)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Kundu and Tree
You are viewing a single comment's thread. Return to all comments →
In Go. Brute Force.
Got a couple test cases right. But of course, it times out.