結果
問題 |
No.1103 Directed Length Sum
|
ユーザー |
![]() |
提出日時 | 2020-07-16 13:35:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,263 ms / 3,000 ms |
コード長 | 1,613 bytes |
コンパイル時間 | 1,093 ms |
コンパイル使用メモリ | 101,816 KB |
実行使用メモリ | 175,156 KB |
最終ジャッジ日時 | 2024-11-24 15:05:10 |
合計ジャッジ時間 | 13,825 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<utility> #include<map> #include<set> #include<queue> #include<stack> #include<functional> #include<math.h> #include<random> #include <bitset> using namespace std; #define N (1000000000+7) //#define N 998244353 #define INF 1e16 typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> Q; const int inf = (int)1e9; ll gcd(ll a, ll b) { if (b > a) { ll tmp = b; b = a; a = tmp; } if (a%b == 0)return b; else return gcd(b, a%b); } ll path[1000010]; ll vertex[1000010]; vector<int>g[1000010]; ll num[1000010]; void rec(ll now,ll root){ if(path[now]!=-1 && vertex[now]!=-1){ return; } if(root!=-1 && g[now].size()==1){ path[now]=0; vertex[now]=1; return; } ll v = 0,p = 0; for(int i=0;i<g[now].size();i++){ int next = g[now][i]; if(root == next)continue; rec(next,now); v = (v+vertex[next])%N; p = (p+path[next]+vertex[next])%N; } vertex[now]=(v+1)%N; path[now]=p%N; return; } int main(void){ int n; cin>>n; for(int i=1;i<=n;i++){ path[i]=-1; vertex[i]=-1; } for(int i=0;i<n-1;i++){ int a,b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); num[b]++; } ll start = 0; for(int i=1;i<=n;i++){ if(num[i]==0)start=i; } rec(start,-1); //for(int i=1;i<=n;i++)cout<<path[i]<<" "<<vertex[i]<<endl; ll ans = 0; for(int i=1;i<=n;i++)ans = (ans+path[i])%N; cout<<ans<<endl; return 0; }