結果

問題 No.2638 Initial fare
ユーザー shobonvipshobonvip
提出日時 2024-02-11 16:59:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 761 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 211,076 KB
実行使用メモリ 57,588 KB
最終ジャッジ日時 2024-02-11 16:59:43
合計ジャッジ時間 8,136 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 234 ms
28,072 KB
testcase_05 AC 234 ms
29,316 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 212 ms
29,028 KB
testcase_08 AC 182 ms
27,616 KB
testcase_09 AC 197 ms
29,696 KB
testcase_10 AC 199 ms
29,928 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 227 ms
28,648 KB
testcase_13 AC 171 ms
26,368 KB
testcase_14 AC 202 ms
29,552 KB
testcase_15 AC 202 ms
29,948 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 208 ms
28,912 KB
testcase_18 AC 198 ms
28,396 KB
testcase_19 AC 233 ms
32,320 KB
testcase_20 AC 219 ms
30,352 KB
testcase_21 AC 225 ms
33,892 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 291 ms
57,588 KB
testcase_24 AC 281 ms
53,288 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 263 ms
45,020 KB
testcase_27 AC 297 ms
54,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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