結果
問題 | No.1103 Directed Length Sum |
ユーザー |
|
提出日時 | 2020-07-05 06:35:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 487 ms / 3,000 ms |
コード長 | 938 bytes |
コンパイル時間 | 949 ms |
コンパイル使用メモリ | 111,232 KB |
最終ジャッジ日時 | 2025-01-11 15:51:34 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:49:8: warning: ‘s’ may be used uninitialized [-Wmaybe-uninitialized] 49 | dfs(s); | ~~~^~~ main.cpp:45:9: note: ‘s’ was declared here 45 | int s; | ^
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <cstdio> #include <ctime> #include <assert.h> #include <chrono> #include <random> #include <numeric> #include <set> using namespace std; typedef long long int ll; using ull = unsigned long long; constexpr ll mod=1e9+7; int n; ll res=0; vector<int> g[1001000]; int d[1001000]; ll dp[1001000]; void dfs(int s){ d[s]=1; for(int t:g[s]){ dfs(t); d[s]+=d[t]; (dp[s]+=dp[t])%=mod; } dp[s]+=d[s]-1; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin >> n; vector<bool> v(n,true); for(int i=1;i<n;i++){ int a,b; cin >> a >> b; a--; b--; g[a].push_back(b); v[b]=false; } int s; for(int i=0;i<n;i++){ if(v[i])s=i; } dfs(s); for(int i=0;i<n;i++){ (res+=dp[i])%=mod; } printf("%lld\n",res); }