結果

問題 No.439 チワワのなる木
ユーザー iicafiaxusiicafiaxus
提出日時 2016-10-28 23:47:45
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 1,833 bytes
コンパイル時間 4,003 ms
コンパイル使用メモリ 167,468 KB
実行使用メモリ 65,140 KB
最終ジャッジ日時 2023-09-02 22:36:43
合計ジャッジ時間 12,144 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 1 ms
4,368 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,368 KB
testcase_15 AC 6 ms
4,372 KB
testcase_16 AC 8 ms
4,372 KB
testcase_17 AC 7 ms
4,372 KB
testcase_18 AC 436 ms
48,944 KB
testcase_19 AC 494 ms
46,888 KB
testcase_20 AC 560 ms
60,080 KB
testcase_21 AC 227 ms
22,364 KB
testcase_22 AC 123 ms
18,692 KB
testcase_23 AC 573 ms
65,140 KB
testcase_24 AC 238 ms
29,716 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(77): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

ulong globalw;

class Node{
	int id;
	char name;
	Node[] adjnodes;
	this(int id, char c){
		this.id = id;
		this.name = c;
		}
	ulong value(){
		debug static int x;
		debug x += 1;
		debug if(x % 100 == 0) writeln("calcValue ", x);
		ulong v;
		if(this.name == 'w'){
			foreach(nx; adjnodes){
				v += nx.ccount(this) * (globalw - 1 - nx.wcount(this));
				}
			return v;
			}
		else return 0;
		}
	
	private ulong[int] _ccount;
	private ulong[int] _wcount;
	ulong xc;
	ulong ccount(Node nd){
		if(nd.id in _ccount) return _ccount[nd.id];
		else{
			debug static int x;
			debug x += 1;
			debug if(x % 100 == 0) writeln("ccount ", x);
			if(this.name == 'c') xc = 1; else xc = 0;
			foreach(nx; adjnodes){
				if(nx.id != nd.id) xc += nx.ccount(this);
				}
			_ccount[nd.id] = xc;
			return xc;
			}
		}
	ulong wcount(Node nd){
		if(nd.id in _wcount) return _wcount[nd.id];
		else{
			debug static int x;
			debug x += 1;
			debug if(x % 100 == 0) writeln("wcount ", x);
			if(this.name == 'w') xc = 1; else xc = 0;
			foreach(nx; adjnodes){
				if(nx.id != nd.id) xc += nx.wcount(this);
				}
			_wcount[nd.id] = xc;
			return xc;
			}
		}
	
	}

class Edge{
	this(Node nd1, Node nd2){
		nd1.adjnodes ~= nd2;
		nd2.adjnodes ~= nd1;
		}
	}


void main(){
	int n = readln.chomp.to!int;
	string s = readln.chomp;
	
	Node[] nodes;
	foreach(int i, c; s){
		nodes ~= new Node(i, c);
		}
	
	Edge[] edges;
	for(int i = 1; i <= n - 1; i ++){
		int[] tmp = readln.chomp.split.map!(to!int).map!(x => x - 1).array;
		edges ~= new Edge(nodes[tmp[0]] , nodes[tmp[1]]);
		}
	
	foreach(nd; nodes) if(nd.name == 'w') globalw += 1;
	
	BigInt ans;
	foreach(nd; nodes) ans += nd.value;
	
	ans.writeln;
	}
0