結果

問題 No.762 PDCAパス
ユーザー Pump0129Pump0129
提出日時 2018-12-12 13:34:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 3,722 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 79,208 KB
実行使用メモリ 73,136 KB
最終ジャッジ日時 2023-10-25 08:24:01
合計ジャッジ時間 13,949 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
52,516 KB
testcase_01 AC 55 ms
53,444 KB
testcase_02 AC 55 ms
53,448 KB
testcase_03 AC 55 ms
53,444 KB
testcase_04 AC 57 ms
53,448 KB
testcase_05 AC 55 ms
53,444 KB
testcase_06 AC 56 ms
53,460 KB
testcase_07 AC 56 ms
53,456 KB
testcase_08 AC 56 ms
53,448 KB
testcase_09 AC 55 ms
53,456 KB
testcase_10 AC 55 ms
53,452 KB
testcase_11 AC 56 ms
52,440 KB
testcase_12 AC 56 ms
53,456 KB
testcase_13 AC 56 ms
53,464 KB
testcase_14 AC 56 ms
52,460 KB
testcase_15 AC 55 ms
53,452 KB
testcase_16 AC 56 ms
53,456 KB
testcase_17 AC 56 ms
52,468 KB
testcase_18 AC 57 ms
53,456 KB
testcase_19 AC 56 ms
51,412 KB
testcase_20 AC 56 ms
52,384 KB
testcase_21 AC 56 ms
53,456 KB
testcase_22 AC 325 ms
61,344 KB
testcase_23 AC 316 ms
61,200 KB
testcase_24 AC 474 ms
73,136 KB
testcase_25 AC 505 ms
73,000 KB
testcase_26 AC 338 ms
61,476 KB
testcase_27 AC 319 ms
61,208 KB
testcase_28 AC 442 ms
67,572 KB
testcase_29 AC 437 ms
67,460 KB
testcase_30 AC 417 ms
67,592 KB
testcase_31 AC 410 ms
67,084 KB
testcase_32 AC 429 ms
67,528 KB
testcase_33 AC 409 ms
65,360 KB
testcase_34 AC 406 ms
65,420 KB
testcase_35 AC 401 ms
65,564 KB
testcase_36 AC 371 ms
63,368 KB
testcase_37 AC 373 ms
63,516 KB
testcase_38 AC 370 ms
63,348 KB
testcase_39 AC 369 ms
63,452 KB
testcase_40 AC 360 ms
63,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package net.ipipip0129.yukicoder.no762;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        // 入力が多いときはこれを使う。(Scanner はなじみ深いが遅い)
        // reader.readLine() これで一行を取得する。
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String[] split_str = reader.readLine().split(" ");
        int leaf_num = Integer.parseInt(split_str[0]);
        int pass_num = Integer.parseInt(split_str[1]);

        PDCALeaf[] pdcaLeaves = new PDCALeaf[leaf_num];

        String pdca_str = reader.readLine();

        // 頂点の構築
        for (int i = 0; i < leaf_num; i++) {
            switch (pdca_str.charAt(i)) {
                case 'P':
                    pdcaLeaves[i] = new PDCALeaf(PDCA.Plan, -1);
                    break;
                case 'D':
                    pdcaLeaves[i] = new PDCALeaf(PDCA.Do, -1);
                    break;
                case 'C':
                    pdcaLeaves[i] = new PDCALeaf(PDCA.Check, -1);
                    break;
                case 'A':
                    pdcaLeaves[i] = new PDCALeaf(PDCA.Act, 1);
                    break;
            }
        }

        // パスの構築
        for (int i = 0; i < pass_num; i++) {
            split_str = reader.readLine().split(" ");
            int a_num = Integer.parseInt(split_str[0]) - 1;
            int b_num = Integer.parseInt(split_str[1]) - 1;
            PDCA a_type = pdcaLeaves[a_num].getType();
            PDCA b_type = pdcaLeaves[b_num].getType();

            if ((a_type == PDCA.Act && b_type == PDCA.Check) ||
                    (a_type == PDCA.Check && b_type == PDCA.Do) ||
                    (a_type == PDCA.Do && b_type == PDCA.Plan)) {
                pdcaLeaves[b_num].addLeaf(a_num);
            } else if ((a_type == PDCA.Check && b_type == PDCA.Act) ||
                    (a_type == PDCA.Do && b_type == PDCA.Check) ||
                    (a_type == PDCA.Plan && b_type == PDCA.Do)) {
                pdcaLeaves[a_num].addLeaf(b_num);
            }
        }

        long ans = 0;

        // パスの集計
        for (int i = 0; i < leaf_num; i++) {
            if (pdcaLeaves[i].getType() == PDCA.Plan) {
                ans += getWeight(pdcaLeaves, i);
            }
        }

        System.out.println(ans % 1000000007);

        reader.close();
    }

    static long getWeight(PDCALeaf[] leaves, int leaf) {
        if (0 <= leaves[leaf].getWeight()) {
            return leaves[leaf].getWeight();
        } else {
            int weight = 0;
            for (int l : leaves[leaf].getLeaf_list()) {
                weight += getWeight(leaves, l);
            }

            leaves[leaf].setWeight(weight);
            return weight;
        }
    }
}

class PDCALeaf {

    PDCA type_pdca;
    long weight;
    List<Integer> leaf_list;

    PDCALeaf(PDCA type, int weight) {
        this.type_pdca = type;
        this.weight = weight;
        this.leaf_list = new ArrayList<>();
    }

    void addLeaf(int leaf_num) {
        leaf_list.add(leaf_num);
    }

    PDCA getType() {
        return type_pdca;
    }

    List<Integer> getLeaf_list() {
        return leaf_list;
    }

    long getWeight() {
        return weight;
    }

    public void setWeight(long weight) {
        this.weight = weight;
    }
}

enum PDCA {
    Plan,
    Do,
    Check,
    Act
}
0