結果

問題 No.1103 Directed Length Sum
ユーザー cn_449cn_449
提出日時 2020-07-03 23:05:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,856 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 122,084 KB
実行使用メモリ 151,816 KB
最終ジャッジ日時 2023-10-17 06:07:08
合計ジャッジ時間 7,902 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
42,136 KB
testcase_01 AC 19 ms
42,136 KB
testcase_02 WA -
testcase_03 AC 174 ms
51,396 KB
testcase_04 AC 338 ms
52,816 KB
testcase_05 AC 603 ms
60,736 KB
testcase_06 AC 214 ms
49,120 KB
testcase_07 AC 50 ms
43,840 KB
testcase_08 AC 74 ms
44,896 KB
testcase_09 AC 38 ms
43,312 KB
testcase_10 AC 107 ms
45,688 KB
testcase_11 AC 373 ms
53,872 KB
testcase_12 AC 226 ms
49,384 KB
testcase_13 AC 110 ms
45,952 KB
testcase_14 AC 32 ms
43,048 KB
testcase_15 AC 172 ms
47,800 KB
testcase_16 AC 423 ms
55,192 KB
testcase_17 AC 443 ms
55,720 KB
testcase_18 AC 105 ms
45,688 KB
testcase_19 AC 386 ms
54,136 KB
testcase_20 AC 44 ms
43,576 KB
testcase_21 AC 70 ms
44,632 KB
testcase_22 AC 313 ms
52,024 KB
testcase_23 AC 182 ms
48,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 int long long
#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;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n;
	cin >> n;
	graph.resize(n); cnt.resize(n); sz.resize(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';
}
0