結果

問題 No.763 Noelちゃんと木遊び
ユーザー iicafiaxus
提出日時 2018-12-11 00:40:42
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,484 bytes
コンパイル時間 2,962 ms
コンパイル使用メモリ 115,200 KB
実行使用メモリ 35,584 KB
最終ジャッジ日時 2025-03-22 10:32:30
合計ジャッジ時間 7,106 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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