結果

問題 No.762 PDCAパス
ユーザー YamaKasa
提出日時 2019-02-12 17:47:46
言語 Java
(openjdk 23)
結果
AC  
実行時間 925 ms / 2,000 ms
コード長 1,812 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 77,756 KB
実行使用メモリ 64,848 KB
最終ジャッジ日時 2024-09-13 09:42:50
合計ジャッジ時間 23,600 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		String S = sc.next();
		int[] u = new int[M];
		int[] v = new int[M];
		for(int i = 0; i < M; i++) {
			u[i] = sc.nextInt() - 1;
			v[i] = sc.nextInt() - 1;
		}
		sc.close();
		long mod = (long)Math.pow(10, 9) + 7;
		int cp = 0;
		int cd = 0;
		int cc = 0;
		int ca = 0;
		int[] node = new int[N];
		for(int i = 0; i < N; i++) {
			char c = S.charAt(i);
			if(c == 'P') {
				node[i] = cp;
				cp++;
			}else if(c == 'D') {
				node[i] = cd;
				cd++;
			}else if(c == 'C') {
				node[i] = cc;
				cc++;
			}else if(c == 'A') {
				node[i] = ca;
				ca++;
			}
		}
		if(cp == 0 || cd == 0 || cc == 0 || ca == 0) {
			System.out.println(0);
			System.exit(0);
		}
		long[] C = new long[cc];
		for(int i = 0; i < M; i++) {
			char cu = S.charAt(u[i]);
			char cv = S.charAt(v[i]);
			if(cu == 'C' && cv == 'A') {
				C[node[u[i]]]++;
			}else if(cu == 'A' && cv == 'C'){
				C[node[v[i]]]++;
			}
		}
		long[] D = new long[cd];
		for(int i = 0; i < M; i++) {
			char cu = S.charAt(u[i]);
			char cv = S.charAt(v[i]);
			if(cu == 'D' && cv == 'C') {
				D[node[u[i]]] += C[node[v[i]]];
				D[node[u[i]]] %= mod;
			}else if(cu == 'C' && cv == 'D'){
				D[node[v[i]]] += C[node[u[i]]];
				D[node[v[i]]] %= mod;
			}
		}
		long[] P = new long[cp];
		for(int i = 0; i < M; i++) {
			char cu = S.charAt(u[i]);
			char cv = S.charAt(v[i]);
			if(cu == 'P' && cv == 'D') {
				P[node[u[i]]] += D[node[v[i]]];
				P[node[u[i]]] %= mod;
			}else if(cu == 'D' && cv == 'P'){
				P[node[v[i]]] += D[node[u[i]]];
				P[node[v[i]]] %= mod;
			}
		}
		long ans = 0;
		for(long i : P) {
			ans += i;
			ans %= mod;
		}
		System.out.println(ans);
	}
}
0