結果

問題 No.1103 Directed Length Sum
コンテスト
ユーザー tenten
提出日時 2021-03-15 20:16:23
言語 Java
(openjdk 26.0.2.1 + ACL)
コンパイル:
javac -J-Duser.language=en -encoding UTF8 -cp /opt/aclib/ac_library.jar _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true -cp .:/opt/aclib/ac_library.jar _class_
結果
TLE  
実行時間 -
コード長 1,646 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,670 ms
コンパイル使用メモリ 88,792 KB
実行使用メモリ 400,432 KB
最終ジャッジ日時 2026-09-20 06:14:18
合計ジャッジ時間 32,933 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

public class Main {
    static ArrayList<ArrayList<Integer>> graphIn = new ArrayList<>();
    static ArrayList<ArrayList<Integer>> graphOut = new ArrayList<>();
    static int[] inCounts;
    static int[] outCounts;
    static final int MOD = 1000000007;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graphIn.add(new ArrayList<>());
		    graphOut.add(new ArrayList<>());
		}
		int[] ins = new int[n - 1];
		int[] outs = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    ins[i] = Integer.parseInt(line[0]) - 1;
		    outs[i] = Integer.parseInt(line[1]) - 1;
		    graphOut.get(ins[i]).add(outs[i]);
		    graphIn.get(outs[i]).add(ins[i]);
		}
		inCounts = new int[n];
		outCounts = new int[n];
		long ans = 0;
		for (int i = 0; i < n - 1; i++) {
		    ans += (long)getInCount(ins[i]) * getOutCount(outs[i]) % MOD;
		    ans %= MOD;
		}
		System.out.println(ans);
   }
   
   static int getInCount(int idx) {
       if (inCounts[idx] == 0) {
           inCounts[idx] = 1;
           for (int x : graphIn.get(idx)) {
               inCounts[idx] += getInCount(x);
           }
       }
       return inCounts[idx];
   }
   
   static int getOutCount(int idx) {
       if (outCounts[idx] == 0) {
           outCounts[idx] = 1;
           for (int x : graphOut.get(idx)) {
               outCounts[idx] += getOutCount(x);
           }
       }
       return outCounts[idx];
   }
}
0