結果

問題 No.2319 Friends+
ユーザー tentententen
提出日時 2023-05-30 13:03:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 975 ms / 3,000 ms
コード長 3,296 bytes
コンパイル時間 2,650 ms
コンパイル使用メモリ 85,788 KB
実行使用メモリ 223,864 KB
最終ジャッジ日時 2023-08-28 00:32:55
合計ジャッジ時間 39,578 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
49,464 KB
testcase_01 AC 40 ms
49,344 KB
testcase_02 AC 802 ms
213,040 KB
testcase_03 AC 727 ms
210,684 KB
testcase_04 AC 790 ms
212,832 KB
testcase_05 AC 741 ms
211,528 KB
testcase_06 AC 752 ms
210,932 KB
testcase_07 AC 962 ms
215,460 KB
testcase_08 AC 966 ms
215,472 KB
testcase_09 AC 975 ms
217,520 KB
testcase_10 AC 971 ms
215,416 KB
testcase_11 AC 965 ms
215,304 KB
testcase_12 AC 82 ms
51,540 KB
testcase_13 AC 83 ms
51,600 KB
testcase_14 AC 72 ms
49,824 KB
testcase_15 AC 73 ms
49,912 KB
testcase_16 AC 81 ms
51,364 KB
testcase_17 AC 43 ms
49,456 KB
testcase_18 AC 746 ms
206,008 KB
testcase_19 AC 815 ms
212,768 KB
testcase_20 AC 719 ms
212,156 KB
testcase_21 AC 729 ms
211,852 KB
testcase_22 AC 728 ms
209,528 KB
testcase_23 AC 777 ms
212,020 KB
testcase_24 AC 757 ms
212,268 KB
testcase_25 AC 766 ms
211,944 KB
testcase_26 AC 786 ms
215,156 KB
testcase_27 AC 750 ms
212,348 KB
testcase_28 AC 721 ms
212,280 KB
testcase_29 AC 715 ms
212,272 KB
testcase_30 AC 718 ms
212,164 KB
testcase_31 AC 716 ms
211,776 KB
testcase_32 AC 708 ms
211,828 KB
testcase_33 AC 712 ms
212,888 KB
testcase_34 AC 714 ms
212,176 KB
testcase_35 AC 696 ms
211,632 KB
testcase_36 AC 824 ms
223,864 KB
testcase_37 AC 664 ms
211,244 KB
testcase_38 AC 743 ms
212,884 KB
testcase_39 AC 744 ms
213,192 KB
testcase_40 AC 729 ms
211,936 KB
testcase_41 AC 758 ms
212,672 KB
testcase_42 AC 706 ms
211,828 KB
testcase_43 AC 714 ms
212,396 KB
testcase_44 AC 698 ms
212,832 KB
testcase_45 AC 682 ms
213,384 KB
testcase_46 AC 640 ms
212,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        BitSet.size = n;
        BitSet[] locations = new BitSet[n];
        BitSet[] friends = new BitSet[n];
        for (int i = 0; i < n; i++) {
            locations[i] = new BitSet();
            friends[i] = new BitSet();
        }
        int[] place = new int[n];
        for (int i = 0; i < n; i++) {
            place[i] = sc.nextInt() - 1;
            locations[place[i]].add(i);
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            friends[a].add(b);
            friends[b].add(a);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            if (place[a] != place[b] && locations[place[b]].and(friends[a])) {
                sb.append("Yes\n");
                locations[place[a]].draw(a);
                place[a] = place[b];
                locations[place[a]].add(a);
            } else {
                sb.append("No\n");
            }
        }
        System.out.print(sb);
    }
    
    static class BitSet {
        static int size;
        int count;
        long[] values;
        
        public BitSet() {
            count = (size + 60 - 1) / 60;
            values = new long[count];
        }
        
        public void add(int idx) {
            values[idx / 60] |= (1L << (idx % 60));
        }
        
        public void draw(int idx) {
            values[idx / 60] ^= (1L << (idx % 60));
        }
        
        public boolean and(BitSet another) {
            for (int i = 0; i < count; i++) {
                if ((values[i] & another.values[i]) > 0) {
                    return true;
                }
            }
            return false;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0