結果

問題 No.1103 Directed Length Sum
ユーザー shimarutshimarut
提出日時 2021-04-06 22:30:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,167 ms / 3,000 ms
コード長 3,327 bytes
コンパイル時間 3,802 ms
コンパイル使用メモリ 182,704 KB
実行使用メモリ 120,396 KB
最終ジャッジ日時 2023-09-02 14:01:09
合計ジャッジ時間 15,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
32,300 KB
testcase_01 AC 14 ms
32,108 KB
testcase_02 AC 712 ms
120,396 KB
testcase_03 AC 457 ms
46,460 KB
testcase_04 AC 657 ms
49,772 KB
testcase_05 AC 1,167 ms
57,716 KB
testcase_06 AC 408 ms
43,772 KB
testcase_07 AC 85 ms
34,804 KB
testcase_08 AC 134 ms
36,112 KB
testcase_09 AC 54 ms
33,976 KB
testcase_10 AC 193 ms
37,376 KB
testcase_11 AC 716 ms
50,696 KB
testcase_12 AC 407 ms
43,768 KB
testcase_13 AC 194 ms
37,452 KB
testcase_14 AC 44 ms
33,464 KB
testcase_15 AC 310 ms
42,072 KB
testcase_16 AC 819 ms
52,024 KB
testcase_17 AC 846 ms
52,880 KB
testcase_18 AC 191 ms
37,524 KB
testcase_19 AC 728 ms
50,916 KB
testcase_20 AC 62 ms
34,268 KB
testcase_21 AC 112 ms
35,460 KB
testcase_22 AC 592 ms
46,624 KB
testcase_23 AC 337 ms
42,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>
#include <sstream>
#include <regex>
#include <fstream>
#include <array>
#include <atcoder/all>

using namespace atcoder;
using mint = modint1000000007;
//using mint = modint998244353;
//using mint = modint;

using namespace std;
typedef long long ll;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vmint = vector<mint>;
using vvmint = vector<vmint>;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

	int root(int x)
	{
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}

	bool issame(int x, int y)
	{
		return root(x) == root(y);
	}

	bool merge(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b)
{
	if (b == 0)return a;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b)
{
	return a / gcd(a, b) * b;
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region グリッド内チェック
bool out(int x, int y, int h, int w)
{
	if (x < 0 || h <= x || y < 0 || w <= y)return true;
	else return false;
}
#pragma endregion
#pragma region Dijkstra
vl dijkstra(vector<vector<pair<int, ll>>> v, int s)
{
	ll INF = 1e18;
	int MAX = 1e6;
	vl res(MAX, INF);
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
	q.push({ 0,s });
	while (!q.empty())
	{
		int now;
		ll d;
		tie(d, now) = q.top();
		q.pop();
		if (!chmin(res[now], d))continue;
		for (auto p : v[now])
		{
			int next;
			ll c;
			tie(next, c) = p;
			if (res[next] <= res[now] + c)continue;
			q.push({ res[now] + c,next });
		}
	}
	return res;
}
#pragma endregion
#pragma endregion

mint dp[1001001];
ll st_size[1001001];
vvi v(1001001);

ll dfs1(int now)
{
	st_size[now] = 1;
	for (int next : v[now])
	{
		st_size[now] += dfs1(next);
	}
	return st_size[now];
}

mint dfs2(int now)
{
	for (int next : v[now])
	{
		dp[now] += dfs2(next) + st_size[next];
	}
	return dp[now];
}

int main()
{
	int n; cin >> n;
	rep(i, n)dp[i] = st_size[i] = 0;
	vi deg(n);
	rep(i, n - 1)
	{
		int a, b; cin >> a >> b;
		--a, --b;
		v[a].push_back(b);
		++deg[b];
	}
	int r = 0;
	rep(i, n)if (deg[i] == 0)r = i;
	dfs1(r);
	dfs2(r);
	mint res = 0;
	rep(i, n)res += dp[i];
	cout << res.val() << endl;
}
0