Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <fstream>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- vector<int> graph[maxn];
- const ll MOD = 1e9 + 7;
- int painted[maxn];
- ll dp[maxn][4];
- void dfs(int node, int prev) {
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neighbour = graph[node][i];
- if(neighbour != prev) {
- dfs(neighbour, node);
- }
- }
- for(int current_color = 1; current_color <= 3; current_color++) {
- if(painted[node] != -1 and painted[node] != current_color) {
- continue;
- }
- dp[node][current_color] = 1;
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neighbour = graph[node][i];
- if(neighbour != prev) {
- ll sum = 0;
- for(int next_color = 1; next_color <= 3; next_color++) {
- if(current_color != next_color) {
- sum += dp[neighbour][next_color];
- }
- }
- dp[node][current_color] *= sum;
- dp[node][current_color] %= MOD;
- }
- }
- }
- }
- int main(int argc, const char * argv[]) {
- ios_base::sync_with_stdio(false);
- ifstream cin("barnpainting.in");
- ofstream cout("barnpainting.out");
- int n, k;
- cin >> n >> k;
- for(int i = 0; i < n - 1; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- memset(painted, -1, sizeof painted);
- for(int i = 0; i < k; i++) {
- int a, b;
- cin >> a >> b;
- a--;
- painted[a] = b;
- }
- dfs(0, -1);
- cout << (dp[0][1] + dp[0][2] + dp[0][3]) % MOD << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement