buglife spoj Time limit exceeded java -
any hint avoid getting time limit exceeded problem :_ http://www.spoj.com/problems/buglife/
how optimize code
is there faster method reading input
these methods reading input :_ http://ideone.com/gtsiac
my solution :_
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) { (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++]; (int = 0; < 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; (i = 1; <= test; i++) { first = nextint(); second = nextint(); (k = 0; k < first; k++) graph[k] = new arraylist(); (j = 0; j < second; j++) { 1 = nextint(); 2 = nextint(); graph[one - 1].add(two - 1); graph[two - 1].add(one - 1); } out.println("scenario #" + + ":"); if (bfs( 2 * i) == false) { out.println("suspicious bugs found!"); } else { out.println("no suspicious bugs found!"); } } out.flush(); } }
you can not solve using bfs, problem bipartite graph
just @ wikipedia
i solved problem more year ago. my profile
Comments
Post a Comment