-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathP-Independent Set.cpp
More file actions
48 lines (38 loc) · 876 Bytes
/
P-Independent Set.cpp
File metadata and controls
48 lines (38 loc) · 876 Bytes
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
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
#define pii pair<long long , long long >
using namespace std;
vector<vector<int>> adj_list;
pii ways (int source , int parent = -1)
{
int d=1,w=1;
for(auto i:adj_list[source])
{
if(i != parent)
{
pii temp = ways(i,source);
d = (d * temp.second)%MOD;
w = (w * ((temp.first + temp.second)%MOD) )%MOD;
}
}
return {d,w};
}
int32_t main()
{
int n;
cin>>n;
adj_list.resize(n);
for(int i=0;i<n-1;++i)
{
int x,y;
cin>>x>>y;
adj_list[x-1].push_back(y-1);
adj_list[y-1].push_back(x-1);
}
pii ans = ways(0, -1);
cout<< ((ans.first + ans.second)%MOD) <<endl;
return 0;
}