-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_sort.cpp
More file actions
73 lines (64 loc) · 1.33 KB
/
Copy pathtree_sort.cpp
File metadata and controls
73 lines (64 loc) · 1.33 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
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<utility>
#include<memory>
#include<cmath>
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod=1e9+7;
const int INF=1<<30;
const int MAX_N=100001;
vector<int> edge[MAX_N];
ll memo[MAX_N][2];
int depth[MAX_N];
int s=0;
void dfs(int x){//xを根として木の深さを探索
depth[x]=s;
s++;
for(int i :edge[x]){
if(depth[i]==-1) dfs(i);
}
s--;
return;
}
ll draw(int x,int y){ //xをy(0->白、1->黒)に塗る通り数
if(memo[x][y]>0) return memo[x][y];
ll res=1;
for(int i :edge[x]){
if(depth[x]<depth[i]){
if(y) (res*=draw(i,0))%=mod;
else (res*=(draw(i,0)+draw(i,1)))%=mod;
}
}
//cout << x << "," << y << "," << res << endl;
return memo[x][y]=res;
}
int main(){
int N;
cin >> N ;
rep(i,N-1){
int a,b;
scanf("%d%d",&a,&b);
a--; b--;
edge[a].pb(b);
edge[b].pb(a);
}
fill(memo[0],memo[MAX_N-1],-1);
fill(depth,depth+N,-1);
dfs(0);
printf("%lld\n",(draw(0,0)+draw(0,1))%mod);
return 0;
}