結果
問題 | No.1103 Directed Length Sum |
ユーザー |
![]() |
提出日時 | 2020-07-03 23:03:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,784 bytes |
コンパイル時間 | 1,240 ms |
コンパイル使用メモリ | 122,232 KB |
実行使用メモリ | 151,620 KB |
最終ジャッジ日時 | 2024-09-17 04:43:50 |
合計ジャッジ時間 | 7,996 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 21 WA * 1 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>#include <cmath>#include <string>#include <queue>#include <stack>#include <set>#include <map>#include <iomanip>#include <utility>#include <tuple>#include <functional>#include <bitset>#include <cassert>#include <complex>#include <stdio.h>#include <time.h>#include <numeric>#include <random>#include <unordered_map>#include <unordered_set>#define all(a) a.begin(),a.end()#define rep(i, n) for (ll i = 0; i < (n); i++)#define pb push_back#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")using namespace std;typedef long long ll;typedef unsigned long long ull;typedef long double ld;typedef pair<ll, ll> P;typedef complex<ld> com;constexpr int inf = 1000000010;constexpr ll INF = 1000000000000000010;constexpr ld eps = 1e-12;constexpr ld pi = 3.141592653589793238;template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }vector<vector<int>> graph(1000010, vector<int>());vector<ll> cnt(1000010), sz(1000010, 1);void dfs(int n) {for (int i : graph[n]) {dfs(i);cnt[n] += cnt[i];sz[n] += sz[i];}cnt[n] += sz[n] - 1;}int main() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(20);int n;cin >> n;vector<bool> a(n);rep(i, n - 1) {int u, v;cin >> u >> v;u--; v--;graph[u].pb(v);a[v] = true;}int root = -1; int s = 0;rep(i, n) {if (!a[i]) root = i, s++;}assert(s == 1);dfs(root);assert(0 <= root && root < n);ll ans = 0;rep(i, n) {ans += cnt[i];assert(sz[i] >= 1);assert(cnt[i] >= 0);}cout << ans << '\n';}