Codeforces Round #277 (Div. 2) d

/**
 * @brief Codeforces Round #277 (Div. 2) d
 * @file d.cpp
 * @author 面码
 * @created 2014/11/17 14:53
 * @edited  2014/11/17 14:53
 * @type dfs dp
 * 
 *
 */
#include <cstdio>
#include <vector>

#define MOD 1000000007
#define MAXN 2014

using namespace std;

typedef long long int ll;

int d, n;
int u, v;

int val[MAXN];
vector<int> tree[MAXN];

/**
 * @brief dfs and try to add new node
 * @param[in] root start pos;
 * @param[in] base  edge start pos
 * @param[in] curr  edge end pos
 * @note 
 *
 */
int dfs(int root, int base, int curr){
    int ans, to;
    /*make sure root with min val, and min idx if there is a node with the same val in case of conflict*/

    if(base)
    if((val[curr] - val[root] > d) || val[curr] <  val[root] || ( val[curr] == val[root] && curr < root))   
            return 0;

    ans = 1;
    for(int i = 0; i < tree[curr].size(); i++){
        to = tree[curr][i];
        if(to == base)
            continue;
        ans = (ll)ans * (1 + dfs(root, curr, to))%MOD;
    }
    return ans;
}

int main(void){
#ifdef DEBUG
    freopen("./in",  "r", stdin);
    freopen("./out", "w", stdout);
#endif
    scanf("%d%d", &d, &n);
    for(int i = 1; i <= n; i++)
            scanf("%d", &val[i]);
    for(int i = 1; i < n;  i++){
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
            tree[v].push_back(u);
    }

    int ans  = 0;
    
    for(int i = 1; i <= n; i++)
        ans = (ans + dfs(i, 0, i))%MOD;
    
    printf("%d\n", ans);
    return 0;
}
时间: 2024-08-10 17:02:35

Codeforces Round #277 (Div. 2) d的相关文章

【codeforces】Codeforces Round #277 (Div. 2) 解读

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 9:14:02 7 File Name :B.cpp 8 *************************************************/ 9 10 #include

Codeforces Round #277 (Div. 2) c

/**  * @brief Codeforces Round #277 (Div. 2) c  * @file c.c  * @author 面码  * @created 2014/11/14 13:39  * @edited  2014/11/14 13:39  * @type greedy  *  */ #include <stdio.h> #define MAXN 100010 #define MAXC 26 #define max(a, b)  ((a) > (b) ? (a) 

Codeforces Round #277 (Div. 2) a

/**  * @brief Codeforces Round #277 (Div. 2) a  * @author xiyan  * @created 2014/11/13 11:23  * @edited  2014/11/13 11:24  * @type math   *   *  */ #include <stdio.h> long long int a; int main() {     scanf("%I64d", &a);     printf(&qu

Codeforces Round #277 (Div. 2) b

/**  * @brief Codeforces Round #277 (Div. 2) b  * @author 面码  * @created 2014/11/13 14:01  * @edited  2014/11/13 14:01  * @type greedy  * @TODO less space and time cost with bitmap   *   *  */ #include <stdio.h> #define MAXN 110 //int a[MAXN][MAXN];

codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)

题目链接:www.codeforces.com/problemset/problem/486/A题意:求表达式f(n)的值.(f(n)的表述见题目)C++代码: #include <iostream> using namespace std; long long f(long long n) { if (n % 2 == 0) return n / 2; else return n / 2 - n; } int main() { long long n; cin >> n; cou

Codeforces Round #277 (Div. 2)A. Calculating Function 水

A. Calculating Function For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 1015). Output Print f

Codeforces Round #277 (Div. 2) D. Valid Sets DP

D. Valid Sets As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it. We call a set S of tree nodes valid if

Codeforces Round #277 (Div. 2)

整理上次写的题目: A: For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 10^15). 题目简洁.可以看出规律...分下奇偶就可以了.