为避免此问题超出时间限制的任何提示:_ http://www.spoj.com/problems/BUGLIFE/
我该如何优化此代码
是否有更快的方法来读取输入
这些是我阅读输入的方法:_ http://ideone.com/GtSIAC
我的解决方案:_
import java.io.*;
import java.util.*;
public class buglife {
static int first;
static ArrayList[] graph = new ArrayList[2000];
static int[] colour = new int[2000];
static int[] queue = new int[2000];
static int start;
static int index;
static int current;
public static boolean bfs(int f) {
for (int p = 0; p < first; p++) {
if (colour[p] != f && colour[p] != f + 1) {
int x;
start = 0;
index = 0;
queue[index++] = p;
colour[p] = f;
while (start < index) {
current = queue[start++];
for (int i = 0; i < graph[current].size(); i++) {
x = (Integer) graph[current].get(i);
if (colour[x] != f && colour[x] != f + 1) {
if (colour[current] == f) {
colour[x] = f + 1;
} else if (colour[current] == f + 1) {
colour[x] = f;
}
queue[index++] = x;
} else if (colour[current] == colour[x]) {
return false;
}
}
}
}
}
return true;
}
public static void main(String[] args) throws IOException, Exception {
int second;
int one, two;
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
int test = nextInt();
int j, k, i;
for (i = 1; i <= test; i++) {
first = nextInt();
second = nextInt();
for (k = 0; k < first; k++)
graph[k] = new ArrayList();
for (j = 0; j < second; j++) {
one = nextInt();
two = nextInt();
graph[one - 1].add(two - 1);
graph[two - 1].add(one - 1);
}
out.println("Scenario #" + i + ":");
if (bfs( 2 * i) == false) {
out.println("Suspicious bugs found!");
} else {
out.println("No suspicious bugs found!");
}
}
out.flush();
}
}