Created
January 2, 2021 06:28
-
-
Save snowmerak/e6973016c4467e49603b8921273a890f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
import kotlin.math.abs | |
interface Page { | |
fun init(d: Double) | |
fun spread(a: MutableList<Page>?) | |
fun applyChanged(): Boolean | |
fun link(p: Page) | |
fun receive(score: Double) | |
fun spreadOut(l: MutableList<Page>) | |
fun getScore(): Double | |
} | |
class Case1Node: Page { | |
private val outList: MutableList<Page> = mutableListOf() | |
private var pageScore = 0.0 | |
private var changedScore = 0.0 | |
override fun init(d: Double) { | |
pageScore = d | |
} | |
override fun spread(a: MutableList<Page>?) { | |
val spreadScore = pageScore / outList.size | |
outList.forEach { | |
it.receive(spreadScore) | |
} | |
} | |
override fun applyChanged(): Boolean { | |
var rs = false | |
if (abs(changedScore - pageScore) < 0.00000001) { | |
rs = true | |
} | |
pageScore = changedScore | |
changedScore = 0.0 | |
return rs | |
} | |
override fun link(p: Page) { | |
outList.add(p) | |
} | |
override fun receive(score: Double) { | |
changedScore += score | |
} | |
override fun spreadOut(l: MutableList<Page>) { | |
TODO("do not need to make") | |
} | |
override fun getScore(): Double { | |
return pageScore | |
} | |
} | |
class Case2Node: Page { | |
private val outList: MutableList<Page> = mutableListOf() | |
private var pageScore = 0.0 | |
private var changedScore = 0.0 | |
override fun init(d: Double) { | |
pageScore = d | |
} | |
override fun spread(a: MutableList<Page>?) { | |
if (outList.size == 0) { | |
spreadOut(a!!) | |
return | |
} | |
val spreadScore = pageScore / outList.size | |
outList.forEach { | |
it.receive(spreadScore) | |
} | |
} | |
override fun applyChanged(): Boolean { | |
var rs = false | |
if (abs(changedScore - pageScore) < 0.00000001) { | |
rs = true | |
} | |
pageScore = changedScore | |
changedScore = 0.0 | |
return rs | |
} | |
override fun link(p: Page) { | |
outList.add(p) | |
} | |
override fun receive(score: Double) { | |
changedScore += score | |
} | |
override fun spreadOut(l: MutableList<Page>) { | |
val spreadScore = pageScore / l.size | |
l.forEach { | |
it.receive(spreadScore) | |
} | |
} | |
override fun getScore(): Double { | |
return pageScore | |
} | |
} | |
class PageList { | |
private val inner = mutableListOf<Page>() | |
fun add(p: Page) { | |
inner.add(p) | |
} | |
fun init() { | |
inner.forEach { | |
it.init(1.0/inner.size) | |
} | |
} | |
fun link(f: Int, t: Int) { | |
inner[f].link(inner[t]) | |
} | |
fun spread() { | |
while (true) { | |
var switch = true | |
inner.forEach { | |
it.spread(inner) | |
} | |
inner.forEach { | |
switch = it.applyChanged() | |
} | |
if (switch) { | |
break | |
} | |
} | |
} | |
fun getScores(): List<Pair<Int, Double>> { | |
return inner.indices.zip(inner.map { | |
it.getScore() | |
}).toList() | |
} | |
} | |
fun main() { | |
val fileName = readLine()!! | |
val file = File(fileName).bufferedReader() | |
val num = file.readLine().toInt() | |
val listOfCase1 = PageList() | |
val listOfCase2 = PageList() | |
for (i in 0 until num) { | |
listOfCase1.add(Case1Node()) | |
listOfCase2.add(Case2Node()) | |
} | |
file.forEachLine { | |
val (f, t) = it.trim().split("\\s+".toRegex()).map { x -> x.toInt() } | |
listOfCase1.link(f, t) | |
listOfCase2.link(f, t) | |
} | |
listOfCase1.init() | |
listOfCase2.init() | |
listOfCase1.spread() | |
listOfCase2.spread() | |
val case1File = File("out1.txt") | |
if (case1File.exists()) { | |
case1File.delete() | |
} | |
val case2File = File("out2.txt") | |
if (case2File.exists()) { | |
case2File.delete() | |
} | |
case1File.printWriter().use { | |
for ((i, v) in listOfCase1.getScores()) { | |
it.println("$i : $v") | |
} | |
} | |
case2File.printWriter().use { | |
for ((i, v) in listOfCase2.getScores()) { | |
it.println("$i : $v") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment