-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatcoder_dp_P.cpp
More file actions
executable file
·90 lines (75 loc) · 1.96 KB
/
atcoder_dp_P.cpp
File metadata and controls
executable file
·90 lines (75 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// #pragma GCC optimize "trapv"
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll ,ll>>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>
#define MAP map<ll ,ll>
#define UMAP unordered_map<ll , ll>
#define endl "\n"
#define SET set<ll>
#define SETC set<char>
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define fastcout ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define testcases int tt = 1 ; cin >> tt ; for(int i = 1 ; i <= tt ; ++i)
#define codejam cout << "Case #"<<i <<": ";
#define FOR(i,a,n) for(int i = a ; i <= n ; ++i)
#define FORback(i,a,n) for(int i = n ; i >= a ; --i)
#define time cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
#define ll long long int
const ll INF = 1e13+7;
const ll mod = 1e9+7;
vvll graph;
vvll dp;
ll doSome(int curr , int color , int parent){
ll ans= 0 ;
if(dp[curr][color] != -1){
return dp[curr][color];
}
ll temp = 1;
ll temp2 = 1;
for(auto x : graph[curr]){
if(x != parent){
// let's say it's black
temp *= doSome(x , 1 , curr);
temp %= mod;
if(color){
temp2 *= doSome(x , 0 , curr);
temp2 %= mod;
}
}
}
if(color){
ans += temp2;
ans %= mod;
}
ans += temp;
return dp[curr][color] = ans%mod;
}
void solv(){
ll n ;
cin >> n ;
graph.assign(n+2 , vll());
FOR(i , 1 ,n-1){
ll a , b ;
cin >> a >> b ;
graph[a].push_back(b);
graph[b].push_back(a);
}
ll ans = 0;
dp.assign(n+2 , vll(2 , -1));
// here i have taken color 1 as white and 0 as black
ans += doSome(1 , 1 , -1);
cout << ans << endl;
}
int main(){
fast ;
// testcases{
// codejam ;
solv();
// }
}