【Algorithm】 着色

来源:互联网 发布:安卓访问服务器数据库 编辑:程序博客网 时间:2024/06/05 01:18
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std;#define MAXN 100001#define P 1000000007 struct Node{    int id;    Node* next;}; int N;int rst;int root;long long V[MAXN][2];struct Node n1[MAXN], n2[MAXN];struct Node Head[MAXN]; void Calc(int cur, int pre){    struct Node *c = Head[cur].next;     while (c != NULL)    {        if(c->id != pre)        {            Calc(c->id, cur);            V[cur][1] = ((V[cur][1]) * (V[c->id][0] + V[c->id][1])) % P;            V[cur][0] = (V[cur][0] * V[c->id][1]) % P;        }        c = c->next;    }   } int main(int argc, char** argv){    int test_case;    int T;    int i;    int a, b;    bool flag = 0;    //freopen("BlackWhiteTreeInput.txt", "r", stdin);    cin >> T;    for (test_case = 1; test_case <= T; ++test_case)    {        cin >> N;                 for (i = 1; i <= N; i++)        {            Head[i].next = NULL;                V[i][0] = 1;            V[i][1] = 1;        }        flag = 0;         for (i = 1; i < N; i++)        {            cin >> a >> b;             n1[i].id = a;            n1[i].next = Head[b].next;            Head[b].next = &n1[i];             n2[i].id = b;            n2[i].next = Head[a].next;            Head[a].next = &n2[i];        }         Calc(1,-1);        cout << "#" << test_case << " " << (V[1][0] + V[1][1])%P << endl;    }    return 0;}