Submission #1294278


Source Code Expand

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		new Main().solve();
	}
	
	int N;
	Long[] f; //iを根として塗れる数
	Long[] g; //iが白で根として塗れる数
	List<Edge>[] edge;
	int mod=(int)Math.pow(10, 9)+7;
	boolean used[];
	
	void solve(){
		Scanner sc=new Scanner(System.in);
		N=sc.nextInt();
		f=new Long[N];
		g=new Long[N];
		edge=new List[N];
		used=new boolean[N];
		for(int i=0;i<N;i++)edge[i]=new ArrayList<Edge>();
		
		for(int i=0;i<N-1;i++){
			int a=sc.nextInt()-1;
			int b=sc.nextInt()-1;
			addEdge(a,b);
			addEdge(b,a);
		}
		
		dfs(0);
		System.out.println(f[0]);
	}
	void dfs(int s){
		used[s]=true;
		long gg=1;
		long ff=1;
		for(Edge e:edge[s]){
			if(used[e.to])continue;
			dfs(e.to);
			gg*=f[e.to]%mod;
			ff*=g[e.to]%mod;
			gg%=mod;
			ff%=mod;
		}
		ff+=(gg%mod);
		ff%=mod;
		g[s]=gg%mod;
		f[s]=ff%mod;
	}
	void addEdge(int from,int to){
		edge[from].add(new Edge(to,from));
	}
	
	class Edge{
		int to;
		int from;
		Edge(int to,int from){
			this.to=to;
			this.from=from;
		}
	}
}

Submission Info

Submission Time
Task D - 塗り絵
User kwkm0429
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 1194 Byte
Status AC
Exec Time 646 ms
Memory 105676 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt
Case Name Status Exec Time Memory
000.txt AC 93 ms 21716 KB
001.txt AC 93 ms 21716 KB
002.txt AC 567 ms 74556 KB
003.txt AC 646 ms 102812 KB
004.txt AC 529 ms 70304 KB
005.txt AC 626 ms 105068 KB
006.txt AC 609 ms 92740 KB
007.txt AC 616 ms 105088 KB
008.txt AC 549 ms 71744 KB
009.txt AC 637 ms 104672 KB
010.txt AC 542 ms 73156 KB
011.txt AC 633 ms 105292 KB
012.txt AC 519 ms 66712 KB
013.txt AC 627 ms 103068 KB
014.txt AC 458 ms 66652 KB
015.txt AC 630 ms 105220 KB
016.txt AC 519 ms 71196 KB
017.txt AC 616 ms 101976 KB
018.txt AC 334 ms 45020 KB
019.txt AC 624 ms 103556 KB
020.txt AC 273 ms 45348 KB
021.txt AC 630 ms 105676 KB