結果
| 問題 |
No.1103 Directed Length Sum
|
| コンテスト | |
| ユーザー |
shell_mug
|
| 提出日時 | 2020-07-03 22:33:56 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 681 ms / 3,000 ms |
| コード長 | 2,580 bytes |
| コンパイル時間 | 1,289 ms |
| コンパイル使用メモリ | 125,088 KB |
| 実行使用メモリ | 89,320 KB |
| 最終ジャッジ日時 | 2024-09-17 04:14:46 |
| 合計ジャッジ時間 | 7,816 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:92:16: warning: 'top' may be used uninitialized [-Wmaybe-uninitialized]
92 | rep (i, n) if (i != top) ans = (ans + num[i] * depth[i]) % MOD;
| ^~
main.cpp:59:9: note: 'top' was declared here
59 | int top;
| ^~~
ソースコード
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
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; }
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
int main (void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> parent(n, -1);
vector<P> e(n - 1);
vector<vector<int>> graph(n);
rep (i, n - 1) {
cin >> e[i].first >> e[i].second;
e[i].first--; e[i].second--;
graph[e[i].first].push_back(e[i].second);
parent[e[i].second] = e[i].first;
}
int top;
rep (i, n) {
if (parent[i] == -1) {
top = i;
break;
}
}
ll MOD = (ll)1e9 + 7;
vector<ll> num(n), depth(n);
vector<bool> used(n);
stack<int> st;
st.push(top);
while (!st.empty()) {
int a = st.top();
eprintf("%d ", a);
if (!used[a] && !graph[a].empty()) {
for (int i : graph[a]) {
st.push(i);
depth[i] = depth[a] + 1;
}
used[a] = true;
} else {
num[a]++;
for (int i : graph[a]) num[a] = (num[a] + num[i]) % MOD;
st.pop();
}
}
eprintf("\n");
ll ans = 0;
rep (i, n) eprintf("%lld%c", num[i], " \n"[i == i_len - 1]);
rep (i, n) eprintf("%lld%c", depth[i], " \n"[i == i_len - 1]);
rep (i, n) if (i != top) ans = (ans + num[i] * depth[i]) % MOD;
cout << ans << "\n";
return 0;
}
shell_mug