結果

問題 No.2638 Initial fare
ユーザー YugimnYugimn
提出日時 2024-02-24 22:00:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 3,643 ms
コンパイル使用メモリ 256,300 KB
実行使用メモリ 57,588 KB
最終ジャッジ日時 2024-02-24 22:01:07
合計ジャッジ時間 8,842 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 229 ms
28,072 KB
testcase_05 AC 257 ms
29,316 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 226 ms
29,028 KB
testcase_08 AC 189 ms
27,616 KB
testcase_09 AC 205 ms
29,696 KB
testcase_10 AC 203 ms
29,928 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 235 ms
28,648 KB
testcase_13 AC 170 ms
26,368 KB
testcase_14 AC 202 ms
29,552 KB
testcase_15 AC 204 ms
29,948 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 205 ms
28,912 KB
testcase_18 AC 191 ms
28,396 KB
testcase_19 AC 238 ms
32,320 KB
testcase_20 AC 221 ms
30,352 KB
testcase_21 AC 230 ms
33,892 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 273 ms
57,588 KB
testcase_24 AC 284 ms
53,288 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 297 ms
45,020 KB
testcase_27 AC 283 ms
54,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//mosyasu

#include<bits/stdc++.h>
using namespace std;

int main(){
	int n; cin >> n;
	vector ikeru(n, vector<int>(0));
	for (int i=0; i<n-1; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		ikeru[u].push_back(v);
		ikeru[v].push_back(u);
	}

	long long ans = 0;
	vector dp(n, vector<long long>(4));
	vector<int> mx(n, 0);

	auto dfs = [&](auto self, int i, int p) -> void {
		dp[i][0] = 1;
		mx[i] = 1;
		for (int j: ikeru[i]){
			if (j == p) continue;
			self(self, j, i);
			for (int x=0; x<mx[i]; x++){
				for (int y=0; y<mx[j]; y++){
					if (x+y+1 > 3) break;
					ans += dp[i][x] * dp[j][y];
				}
			}
			for (int x=0; x<min(3,mx[j]); x++){
				dp[i][x+1] += dp[j][x];
			}
			mx[i] = min(4, max(mx[i], mx[j]+1));
		}
	};

	dfs(dfs, 0, -1);
	cout << ans << '\n';
}
0