結果
問題 | No.2638 Initial fare |
ユーザー |
|
提出日時 | 2024-02-19 21:55:23 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 201 ms / 2,000 ms |
コード長 | 958 bytes |
コンパイル時間 | 873 ms |
コンパイル使用メモリ | 92,520 KB |
実行使用メモリ | 25,940 KB |
最終ジャッジ日時 | 2024-09-29 01:54:37 |
合計ジャッジ時間 | 5,346 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#include <iostream>#include <vector>using namespace std;using ll = long long;int main () {int N; cin >> N;vector<vector<int>> graph(N);for (int i = 0; i < N-1; i++) {int u, v; cin >> u >> v;u--, v--;graph[u].push_back(v);graph[v].push_back(u);}// 近隣を見ることで出来そうvector<vector<int>> count(N, vector<int>(4));for (int i = 0; i < N; i++) {count[i][0] = 1;count[i][1] = (int) graph[i].size();}for (int j = 2; j <= 3; j++) {for (int i = 0; i < N; i++) {for (auto& to : graph[i]) {if (j == 2) count[i][j] += count[to][j-1] - count[i][j-2];else count[i][j] += count[to][j-1] - count[i][j-2] + count[to][j-3];}}}ll ans = 0;for (int i = 0; i < N; i++) ans += count[i][1] + count[i][2] + count[i][3];ans /= 2;cout << ans << "\n";}