Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
4
Indexable
public class TheSettlersOfCatan {
    static final int ms = 30;

    static int T, n, e, maxx;
    static int[][] m = new int[ms][ms];
    static int[][] v = new int[ms][ms];
    
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
    
        T = sc.nextInt();
        for (int tc = 1; tc <= T; tc++) {
            n = sc.nextInt();
            e = sc.nextInt();
            reset();
            for (int i = 1; i <= e; i++) {
                int temp1 = sc.nextInt();
                int temp2 = sc.nextInt();
                m[temp1][temp2] = 1;
                m[temp2][temp1] = 1;
            }

            maxx = 0;
            for (int i = 0; i < n; i++) {
                BT(i, 0);
            }
            System.out.println(maxx);
        }
        sc.close();
    }

    public static void BT(int step, int sum) {
        if (sum > maxx) maxx = sum;
        for (int i = 0; i < n; i++) {
            if (m[step][i] == 1 && v[step][i] == 0) {
                v[step][i] = 1;
                v[i][step] = 1;
                BT(i, sum + 1);
                v[step][i] = 0;
                v[i][step] = 0;
            }
        }
    }
    
    public static void reset() {
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                v[i][j] = 0;
                m[i][j] = 0;
            }
        }
    }

    public static void resetv() {
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                v[i][j] = 0;
            }
        }
    }
}
Editor is loading...
Leave a Comment