結果
問題 | No.2638 Initial fare |
ユーザー |
|
提出日時 | 2024-02-19 21:37:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 201 ms / 2,000 ms |
コード長 | 814 bytes |
コンパイル時間 | 2,229 ms |
コンパイル使用メモリ | 197,424 KB |
最終ジャッジ日時 | 2025-02-19 16:44:09 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#include <bits/stdc++.h>using namespace std;int main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);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.at(u).push_back(v);Graph.at(v).push_back(u);}auto dp = [&](auto dp,int pos,int back) -> vector<long long> {long long ret = 0;long long one = 0,two = 0;for(auto to : Graph.at(pos)){if(to == back) continue;auto ko = dp(dp,to,pos);long long x = ko.at(0),y = ko.at(1),z = ko.at(2);ret += x+y+z+ko.back();ret += one*x+two*x+one*y;one++; two += y;}return {1,one,two,ret};};vector<long long> result = dp(dp,0,-1);cout << result.back() << endl;}