-3

この問題の制限時間を超えないようにするためのヒント:_ 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();
    }




}
4

1 に答える 1