結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
42,136 KB
testcase_01 AC 17 ms
42,136 KB
testcase_02 WA -
testcase_03 AC 181 ms
47,344 KB
testcase_04 AC 308 ms
51,496 KB
testcase_05 AC 523 ms
58,096 KB
testcase_06 AC 193 ms
48,328 KB
testcase_07 AC 51 ms
43,840 KB
testcase_08 AC 73 ms
44,632 KB
testcase_09 AC 36 ms
43,312 KB
testcase_10 AC 93 ms
45,424 KB
testcase_11 AC 327 ms
52,288 KB
testcase_12 AC 192 ms
48,328 KB
testcase_13 AC 94 ms
45,424 KB
testcase_14 AC 31 ms
43,048 KB
testcase_15 AC 150 ms
47,008 KB
testcase_16 AC 362 ms
53,608 KB
testcase_17 AC 383 ms
54,136 KB
testcase_18 AC 92 ms
45,424 KB
testcase_19 AC 340 ms
52,552 KB
testcase_20 AC 41 ms
43,312 KB
testcase_21 AC 64 ms
44,368 KB
testcase_22 AC 272 ms
50,704 KB
testcase_23 AC 167 ms
47,272 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 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;
	rep(i, n) if (!a[i]) root = i;
	dfs(root);
	ll ans = 0;
	rep(i, n) ans += cnt[i];
	cout << ans << '\n';
}
0