結果

問題 No.763 Noelちゃんと木遊び
ユーザー iicafiaxusiicafiaxus
提出日時 2018-12-11 00:40:42
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 1,484 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 120,736 KB
実行使用メモリ 35,868 KB
最終ジャッジ日時 2023-09-03 21:32:32
合計ジャッジ時間 6,139 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
35,868 KB
testcase_01 AC 67 ms
11,280 KB
testcase_02 AC 176 ms
22,068 KB
testcase_03 AC 108 ms
18,696 KB
testcase_04 AC 76 ms
11,760 KB
testcase_05 AC 104 ms
18,148 KB
testcase_06 AC 210 ms
27,792 KB
testcase_07 AC 199 ms
25,636 KB
testcase_08 AC 111 ms
20,256 KB
testcase_09 AC 71 ms
11,284 KB
testcase_10 AC 26 ms
8,188 KB
testcase_11 AC 212 ms
28,412 KB
testcase_12 AC 189 ms
24,840 KB
testcase_13 AC 187 ms
22,140 KB
testcase_14 AC 170 ms
22,224 KB
testcase_15 AC 104 ms
18,480 KB
testcase_16 AC 17 ms
6,192 KB
testcase_17 AC 106 ms
17,880 KB
testcase_18 AC 211 ms
27,564 KB
testcase_19 AC 194 ms
26,824 KB
testcase_20 AC 194 ms
23,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

class Node{
	int id;
	Node parent;
	Node[] nodes;
	Node[] kids;
	int ans1, ans2; // ans1は自分が除去された時、ans2は自分が除去されない時
	this(int id){
		this.id = id;
	}
	override string toString(){
		return "#" ~ this.id.to!string ~ "(" ~ (this.parent? this.parent.id.to!string: "*") ~ ") (" ~ this.kids.map!(x => x.id.to!string).array.join(", ") ~ ") " ~ ans1.to!string ~ " " ~ ans2.to!string;
	}
	void setKids(){
		foreach(nd; this.nodes){
			if(this.parent && this.parent.id == nd.id) continue;
			if(nd.parent) continue;
			this.kids ~= nd;
			nd.parent = this;
			nd.setKids();
		}
	}
	void solve(){
		this.ans1 = 0, this.ans2 = 1;
		foreach(kid; this.kids){
			kid.solve;
			this.ans1 += max(kid.ans1, kid.ans2);
			this.ans2 += max(kid.ans1, kid.ans2 - 1);
		}
	}
}

void main(){
	int n = read.to!int;
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i);
	
	foreach(i; 0 .. n - 1){
		int u = read.to!int - 1, v = read.to!int - 1;
		nodes[u].nodes ~= nodes[v];
		nodes[v].nodes ~= nodes[u];
	}
	
	nodes[0].setKids;
	debug foreach(nd; nodes) nd.writeln;
	
	nodes[0].solve;
	debug foreach(nd; nodes) nd.writeln;
	
	max(nodes[0].ans1, nodes[0].ans2).writeln;
}
0