結果

問題 No.1340 おーじ君をさがせ
ユーザー tenten
提出日時 2023-06-07 09:22:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 3,439 bytes
コンパイル時間 5,506 ms
コンパイル使用メモリ 83,944 KB
実行使用メモリ 43,468 KB
最終ジャッジ日時 2024-12-29 15:09:25
合計ジャッジ時間 12,568 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

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();
        long t = sc.nextLong();
        BitSet[][] graph = new BitSet[60][n];
        for (int i = 0; i < n; i++) {
            graph[0][i] = new BitSet();
        }
        for (int i = 0; i < m; i++) {
            graph[0][sc.nextInt()].add(sc.nextInt());
        }
        for (int i = 1; i < 60; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = new BitSet();
            }
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    if (graph[i - 1][j].get(k)) {
                        graph[i][j].or(graph[i - 1][k]);
                    }
                }
            }
        }
        BitSet ans = new BitSet();
        ans.add(0);
        for (int i = 59; i >= 0; i--) {
            if (t < (1L << i)) {
                continue;
            }
            t -= (1L << i);
            BitSet next = new BitSet();
            for (int j = 0; j < n; j++) {
                if (ans.get(j)) {
                    next.or(graph[i][j]);
                }
            }
            ans = next;
        }
        System.out.println(ans.getCount());
    }
    
    static class BitSet {
        long[] values = new long[2];
        
        public void add(int x) {
            values[x / 60] |= (1L << (x % 60));
        }
        
        public boolean get(int x) {
            return (values[x / 60] & (1L << (x % 60))) > 0;
        }
        
        public void or(BitSet x) {
            for (int i = 0; i < 2; i++) {
                values[i] |= x.values[i];
            }
        }
        
        public int getCount() {
            long pop = 0;
            for (long x : values) {
                pop += getPop(x);
            }
            return (int)pop;
        }
        
        private long getPop(long x) {
            long pop = 0;
            while (x > 0) {
                pop += x % 2;
                x >>= 1;
            }
            return pop;
        }
    }
}
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