首页 > 代码库 > Codeforces Round #277 (Div. 2) d
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; }
Codeforces Round #277 (Div. 2) d
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。