結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー AsahiAsahi
提出日時 2024-02-16 23:01:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,007 ms / 2,500 ms
コード長 8,339 bytes
コンパイル時間 5,755 ms
コンパイル使用メモリ 83,432 KB
実行使用メモリ 66,116 KB
最終ジャッジ日時 2024-02-16 23:01:45
合計ジャッジ時間 22,002 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 508 ms
64,928 KB
testcase_01 AC 838 ms
64,032 KB
testcase_02 AC 986 ms
65,928 KB
testcase_03 AC 137 ms
58,060 KB
testcase_04 AC 135 ms
58,064 KB
testcase_05 AC 138 ms
57,804 KB
testcase_06 AC 648 ms
65,752 KB
testcase_07 AC 917 ms
65,984 KB
testcase_08 AC 792 ms
65,992 KB
testcase_09 AC 786 ms
65,936 KB
testcase_10 AC 930 ms
65,936 KB
testcase_11 AC 998 ms
66,116 KB
testcase_12 AC 872 ms
65,944 KB
testcase_13 AC 1,007 ms
65,984 KB
testcase_14 AC 955 ms
66,032 KB
testcase_15 AC 767 ms
64,416 KB
testcase_16 AC 798 ms
64,384 KB
testcase_17 AC 748 ms
64,412 KB
testcase_18 AC 636 ms
66,004 KB
testcase_19 AC 753 ms
65,344 KB
testcase_20 AC 726 ms
65,304 KB
testcase_21 AC 725 ms
65,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*; 
import java.io.*;
import java.math.*;
import java.util.stream.*;
import java.util.function.*;

class Main implements Runnable {

    public void solve() {    
        int n = in.nextInt() , m = in.nextInt();
        int [] a = new int[m] , b = new int[m];
        for(int i = 0 ; i < m ; i ++) {
            a[i] = in.nextInt() - 1;
            b[i] = in.nextInt() - 1;
        }
        int [] c = in.nextInt(n);
        long [] w = in.nextLong(10);
        int q = in.nextInt();
        int [] u = new int[q] , v = new int[q];
        for(int i = 0 ; i < q ; i ++) {
            u[i] = in.nextInt() - 1;
            v[i] = in.nextInt() - 1;
        }
        long [] res = new long[q];
        Arrays.fill(res , lnf);
        for(int i = 0 ; i < (1 << 10) ; i ++) {
            int [] valid = new int[10];
            long cost = 0 ;
            for(int j = 0 ; j < 10 ; j ++) {
                int mask = (i >> j) & 1 ;
                if(mask == 1) {
                    valid[j] = 1 ;
                    cost += w[j];
                }
            }
            UnionFind uf = new UnionFind(n);
            for(int j = 0 ; j < m ; j ++) {
                if(valid[c[a[j]] - 1] == 1 && valid[c[b[j]] - 1] == 1) uf.unite(a[j] , b[j]);
            }
            for(int j = 0 ; j < q ; j ++) {
                if(uf.same(u[j] , v[j])) res[j] = Math.min(res[j] , cost);
            }
        }
        for(int i = 0 ; i < q ; i ++) {
            out.println(res[i] == lnf ? -1 : res[i]);
        }
    }

    class UnionFind {

        private int [] rank , parents , size ;
        private int group_count ;
    
        UnionFind(int n) {
            this.rank = new int[n];
            this.parents = new int[n];
            this.size = new int[n];
            this.group_count = n ; 
            Arrays.fill(size , 1);
            for(int i = 0 ; i < n ; i ++ ) parents[i] = i ;
        }
    
        public int groupcount() { return this.group_count ; }
    
        public int membercount(int x) { return size[root(x)] ; }
    
        public boolean same(int x , int y) { return root(x) == root(y) ; }
    
        public int root(int x) {
            if(x == parents[x]) return x ;
            else parents[x] = root(parents[x]);
            return parents[x];
        }
    
        public void unite(int l , int r) {
    
            int left  = root(l);
            int right = root(r);
            int parent = -1 , child = -1 ;
            if(left == right) return ;
            parent = rank[left] > rank[right] ? left : right;
            child = rank[left] > rank[right] ? right : left ;
            --group_count;
            parents[child] = parent ;
            if(rank[parent] == rank[child]) rank[parent] ++ ;
            size[parent] += size[child] ; 
        }
    
        @SuppressWarnings("unchecked")
        public List<Integer> [] groupList() {
            List<Integer> [] ret = new ArrayList[group_count];
            int count = 0 ;
            for(int i = 0 ; i < group_count ; i ++) ret[i] = new ArrayList<>();
            Map<Integer,List<Integer>> mp = new HashMap<>();
            for(int i = 0 ; i < parents.length ; i ++ ) {
                int par = root(i);
                if(!mp.containsKey(par)) mp.put(par , new ArrayList<>());
                mp.get(par).add(i);
            }
            for(List<Integer> temp : mp.values()) ret[count++] = temp ;
            return ret ;
        }
    
    }

    
    /******/



    /******/
    
    public PrintWriter out = new PrintWriter(System.out);
    public In in = new In() ;
    public static final int  inf = (1  << 30);
    public static final long lnf = (1L << 60);
    public static final String yes = "Yes" , no  = "No" ;
    public static final int mod7 = 1000000007 , mod9 = 998244353;
    public static final int [] dy4 = {-1,0,1,0} , dx4 = {0,1,0,-1};
    public static final int [] dy8 = {-1,-1,-1,0,1,1,1,0} , dx8 = {-1,0,1,1,1,0,-1,-1};

    public static void main(String ... args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); }
    public void run() { solve(); out.flush(); }

}

class In {

    private final InputStream in = System.in;
    private final Scanner sc = new Scanner(System.in);
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() { 
        if (hasNextByte()) return buffer[ptr++]; else return -1;
    }

    private static boolean isPrintableChar(int c) { 
        return 33 <= c && c <= 126;
    }

    private  boolean hasNext() { 
        while(hasNextByte() && !isPrintableChar(buffer[ptr])) {
            ptr++; 
        }
        return hasNextByte();
    }
    
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() { 
        return Double.parseDouble(next());
    }

    public char nextChar() {
        return next().charAt(0);
    }

    public BigInteger nextBigInteger() {
        return sc.nextBigInteger();
    }

    public int [] nextInt(int n) {
        int [] array = new int[n];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextInt();
        }
        return array ;
    }

    public int [][] nextInt(int n , int m) {
        int [][] array = new int[n][m];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextInt(m);
        }
        return array ;
    }

    public long [] nextLong(int n) {
        long [] array = new long[n];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextLong();
        }
        return array ;
    }

    public long [][] nextLong(int n , int m) {
        long [][] array = new long[n][m];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextLong(m);
        }
        return array ;
    }

    public double [] nextDouble(int n) {
        double [] array = new double[n];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextDouble();
        }
        return array ;
    }
    
    public String [] next(int n) {
        String [] array = new String[n];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = next();
        }
        return array ;
    }

    public String [][] next(int n , int m) {
        String [][] array = new String[n][m];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = next(m);
        }
        return array ;
    }

    public char [] nextChar(int n) {
        char [] array = new char[n];
        String string = next() ;
        for(int i = 0 ; i < n ; i ++) {
            array[i] = string.charAt(i);
        }
        return array ;
    }

    public char [][] nextChar(int n , int m) {
        char [][] array = new char[n][m];
        for(int i = 0 ; i < n ; i ++) {
            array[i] = nextChar(m);
        }
        return array ;
    }

}
0