結果

問題 No.1103 Directed Length Sum
ユーザー akun0716akun0716
提出日時 2020-07-06 20:58:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 751 ms / 3,000 ms
コード長 1,844 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 136,572 KB
最終ジャッジ日時 2023-10-24 23:07:42
合計ジャッジ時間 8,800 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,220 KB
testcase_01 AC 10 ms
28,220 KB
testcase_02 AC 342 ms
136,572 KB
testcase_03 AC 182 ms
50,388 KB
testcase_04 AC 402 ms
49,708 KB
testcase_05 AC 751 ms
60,828 KB
testcase_06 AC 250 ms
43,968 KB
testcase_07 AC 44 ms
32,616 KB
testcase_08 AC 67 ms
35,520 KB
testcase_09 AC 30 ms
31,748 KB
testcase_10 AC 101 ms
35,176 KB
testcase_11 AC 449 ms
50,708 KB
testcase_12 AC 254 ms
42,164 KB
testcase_13 AC 104 ms
35,320 KB
testcase_14 AC 26 ms
31,380 KB
testcase_15 AC 188 ms
40,460 KB
testcase_16 AC 507 ms
52,724 KB
testcase_17 AC 542 ms
54,716 KB
testcase_18 AC 105 ms
35,148 KB
testcase_19 AC 469 ms
51,112 KB
testcase_20 AC 35 ms
32,060 KB
testcase_21 AC 61 ms
35,280 KB
testcase_22 AC 374 ms
45,992 KB
testcase_23 AC 206 ms
40,828 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:78:12: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   78 |         dfs(root, -1);
      |         ~~~^~~~~~~~~~
main.cpp:74:13: note: 'root' was declared here
   74 |         int root;
      |             ^~~~

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

int N;
int ans[1000010];
VI G[1000010];
int t[1000010];//size of subtree

void dfs(int now, int pa) {

	int res = 0;
	fore(to, G[now]) {
		dfs(to, now);
		res += t[to] + ans[to];
		res %= mod;
	}
	ans[now] = res;
}
void dfs1(int now, int pa) {
	fore(to, G[now]) {
		if (to == pa)continue;
		dfs1(to, now);
	}
	t[now]++;
	if (pa != -1)t[pa] += t[now];
	return;
}


signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	vector<bool>F(N, false);
	REP(i, N - 1) {
		int a, b; cin >> a >> b;
		a--; b--;
		G[a].push_back(b);
		F[b] = 1;
	}
	int root;
	REP(i, N)if (!F[i])root = i;
	
	dfs1(root, -1);
	dfs(root, -1);
	int sum = 0;
	REP(i, N) {
		sum += ans[i];
		sum %= mod;
	}
	cout << sum << endl;

	return 0;
}

0