Last active
August 29, 2015 14:03
-
-
Save lala7573/c96ab655a02ac88e42a0 to your computer and use it in GitHub Desktop.
ver. Success : https://gist.github.com/lala7573/37cf28a00d93c4e3ff77
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
package greeter | |
import scala.collection.mutable | |
import scala.io.Source | |
import java.io.{ File, PrintStream } | |
/** | |
* test case마다 map초기화 && (str2 + elem) => (str2 = str2 + (elem)) 문제로 안됨.. | |
* description에 testcase를 둘다 통과한 소스의 링크를 걸어놓음 | |
*/ | |
object hello extends App { | |
var map: mutable.HashMap[String, Boolean] = mutable.HashMap.empty[String, Boolean] | |
def isBipartiteGraph(str: String): Boolean = { | |
val lines = str.split("\n") | |
check(lines) | |
} | |
def check(lines: Array[String]) : Boolean= { | |
var str2 = "" | |
for (i <- 0 to lines.length - 1) yield { | |
val names = lines(i).split(" ") | |
if ( map == null ) { | |
map = mutable.HashMap.empty[String, Boolean] | |
map += (names(0) -> false) | |
map += (names(1) -> true) | |
} | |
//0없음 | |
if (!map.contains(names(0))) { | |
//1 있음 | |
if (map.contains(names(1))) { | |
map += (names(0) -> !map(names(1))) | |
} //1 없음 | |
else { | |
str2.+(lines(i) + "\n") | |
} | |
//0있음 | |
} else { | |
// 1없음 | |
if (!map.contains(names(1))) { | |
map += (names(1) -> !map(names(0))) | |
} // 1있음 | |
else { | |
if (!map(names(0)) != map(names(1))) { | |
return false | |
} | |
} | |
} | |
} | |
if (str2 != "") check(str2.split("\n")) | |
else if (str2.split("\n").equals(lines)) { | |
map = mutable.HashMap.empty[String, Boolean] | |
check(str2.split("\n")) | |
} | |
else true | |
} | |
def setting(iter: Iterator[String])(pr: String => Unit) { | |
for (i <- 1 to iter.next().toInt) yield { | |
var seq: String = "" | |
for (j <- 1 to iter.next().toInt) yield { | |
seq = seq.+(iter.next().toString + "\n") | |
} | |
pr(s"Case #$i: ${if (isBipartiteGraph(seq)) "Yes" else "No"}") | |
} | |
} | |
override def main(args: Array[String]) { | |
val in = Source.fromFile(new File("src/A-small-practice-1.in")) | |
val out = new PrintStream(new File("src/test.out")) | |
try { | |
setting(in.getLines) { s: String => out.println(s) } | |
} finally { | |
out.flush; out.close | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment