結果

問題 No.519 アイドルユニット
ユーザー tenten
提出日時 2021-05-17 15:13:42
言語 Java
(openjdk 23)
結果
AC  
実行時間 633 ms / 1,000 ms
コード長 1,984 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 78,604 KB
実行使用メモリ 66,000 KB
最終ジャッジ日時 2024-10-06 14:50:51
合計ジャッジ時間 10,450 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<HashSet<Integer>> base = new ArrayList<>();
    static HashMap<Integer, Integer> dp = new HashMap<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            base.add(new HashSet<>());
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int x = sc.nextInt();
                if (i < j) {
                    dp.put((1 << i) + (1 << j), x);
                    base.get(i).add((1 << i) + (1 << j));
                }
            }
        }
        System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int mask) {
        if (!dp.containsKey(mask)) {
            int max = 0;
            int idx = getIdx(mask);
            for (int x : base.get(idx)) {
                if ((mask & x) != x) {
                    continue;
                }
                max = Math.max(max, dfw(x) + dfw(mask ^ x));
            }
            dp.put(mask, max);
        }
        return dp.get(mask);
    }
    
    static int getIdx(int mask) {
        int idx = 0;
        while (mask > 0) {
            if (mask % 2 == 1) {
                break;
            }
            idx++;
            mask >>= 1;
        }
        return idx;
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0