uestc 1847,dfs

Neurotic Network

Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 47 Tried: 340

Submit

Status

Best Solution

Back

Description

 

In this problem, a neural net is represented by a rooted tree with weighted edges. The neural net processes information by a cascade of signals that begins at the leaf nodes: Each node in the tree computes an output value based on its upstream neighbors, and passes this value on to its downstream neighbor. The output value computed by a node is the sum of the output of each of its upstream neighbors multiplied by the weight of the edge from the upstream neighbor to the node itself. A node with no upstream neighbors (leaf nodes) always has 1 as output. All neural nets in this problem have exactly one fi nal output node (the root node).
Sometimes, a neural net can go haywire and become what is more commonly known as a neurotic network. Consider this your chance to launch a second career in psychiatry. The scenario is that someone just came in with a neurotic network in their head. What this means is that if the output of their neural net is an even number, the person will freak out and will set fi re to a kitten. Therefore, it is of vital importance that you can know ahead of time whether or not a given person is safe. If it is safe, print their neural output modulo 1,000,000,007. If you wouldn't trust the person to be around kittens who're not wrapped in fi re retardant, print the string "FREAK OUT" (without the quotes).

 

Input

 

The fi rst line of input is T, the number of test cases. For each of the T cases, the first line will be the integer N, the number of nodes in the tree. The next line contains N -1 integers a1,a2,…,aN-1 where ai is the downstream neighbour of the node with ID i.Then follows a line with N - 1 integers w1,w2,…,wN-1 where wi is the weight of the neural connection going out from the node with ID i. Note that the node with ID 0 will always be the output node.

0 < T <= 50
0 < N <= 10000
0 < wi <= 10
0 <= ai < N
The graph is guaranteed to be a tree.
This is an I/O-heavy problem. For Java programmers, this means that you should
use BufferedReader for input reading (not Scanner).

 

Output

 

Output "FREAK OUT" (without the quotes) if the final value of the neural net's output node is even. Otherwise, output the final value of the output node, modulo 1,000,000,007.

 

Sample Input

 

4
1
4
0 0 2
7 7 1
4
0 0 2
6 7 2
11
0 1 2 3 4 5 6 7 8 9
9 9 9 9 9 9 9 9 9 3

 

Sample Output

 

1
FREAK OUT
FREAK OUT
162261460

奇数*奇数=奇数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 11111
#define MOD 1000000007

//int h[MAXN], cc;
//
//struct edge{
//    int u, v, nxt;
//    LL w;
//}e[MAXN<<1];

vector<int> g[MAXN], c[MAXN], rg[MAXN], rc[MAXN];

//bool vis[MAXN];
LL ans[MAXN];

bool dfs(int u){
//    vis[u]=true;
    if(0==g[u].size()){
        ans[u]=1;
        return true;
    }

    bool f=false;
    for(int i=0; i<g[u].size(); i++){
        int v=g[u][i];
        if(dfs(v) && (c[u][i]&1)) f=!f;
        ans[u] =(ans[u] + ((LL)c[u][i]*ans[v]))%MOD;
    }
    return f;
}

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int T;
    scanf(" %d", &T);
    while(T--){
        int i, n;
        scanf(" %d", &n);

        for(i=0; i<n; i++){
            g[i].clear();       rg[i].clear();
            c[i].clear();       rc[i].clear();
        }

        for(i=1; i<n; i++){
            int t;
            scanf(" %d", &t);
            rg[i].push_back(t);
        }

        for(i=1; i<n; i++){
            int t;
            scanf(" %d", &t);
            rc[i].push_back(t);
        }

        for(int u=1; u<n; u++){
            for(int j=0; j<rg[u].size(); j++){
                int v=rg[u][j];
                g[v].push_back(u);
                c[v].push_back(rc[u][j]);
            }
        }

        memset(ans, 0, sizeof(ans));

        if(!dfs(0)) printf("FREAK OUT\n");
        else printf("%lld\n", ans[0]);
    }
    return 0;
}

 

posted @ 2013-11-07 17:21  Ramanujan  阅读(238)  评论(0编辑  收藏  举报