Submission #3772957


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
struct node {
    vector<int> next;
};
int N;
vector<node> graph;
vector<int> memo;
const int MOD = 1000000007;
int dfs(int i, bool c, int prev = -1) // c: true == black
{
    if (memo[c*N + i] > 0) return memo[c*N + i];
    Int ans = 1;
    for (int n : graph[i].next) {
        if (n != prev) {
            if (c) {
                ans *= dfs(n, false, i);
            } else {
                ans *= dfs(n, false, i) + dfs(n, true, i);
            }
            ans %= MOD;
        }
    }
    return memo[c*N + i] = ans;
}
int main()
{
    cin >> N;
    graph = vector<node>(N);
    memo = vector<int>(N*2);
    for (int i = 0; i < N-1; i++) {
        int a, b; cin >> a >> b;
        a--, b--;
        graph[a].next.push_back(b);
        graph[b].next.push_back(a);
    }
    cout << (dfs(0, false) + dfs(0, true)) % MOD << endl;
    return 0;
}

Submission Info

Submission Time
Task D - 塗り絵
User ha15
Language C++14 (GCC 5.4.1)
Score 100
Code Size 960 Byte
Status AC
Exec Time 108 ms
Memory 6528 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 22
Set Name Test Cases
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt
Case Name Status Exec Time Memory
000.txt AC 1 ms 256 KB
001.txt AC 1 ms 256 KB
002.txt AC 75 ms 5248 KB
003.txt AC 104 ms 6528 KB
004.txt AC 63 ms 4480 KB
005.txt AC 108 ms 6528 KB
006.txt AC 98 ms 6016 KB
007.txt AC 99 ms 6528 KB
008.txt AC 68 ms 4736 KB
009.txt AC 101 ms 6528 KB
010.txt AC 69 ms 4864 KB
011.txt AC 98 ms 6528 KB
012.txt AC 58 ms 3968 KB
013.txt AC 98 ms 6528 KB
014.txt AC 44 ms 3200 KB
015.txt AC 101 ms 6528 KB
016.txt AC 58 ms 4096 KB
017.txt AC 99 ms 6528 KB
018.txt AC 17 ms 1408 KB
019.txt AC 98 ms 6528 KB
020.txt AC 9 ms 896 KB
021.txt AC 99 ms 6528 KB