Friday, 20 February 2015

Codeforces Round #277 (Div. 2) Valid Sets

Problem: Valid Sets

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

vector<int>adj[2005];
long long arr[2005], dp[2005];
int n, d, now;
bool vis[2005];

long long func(int u)
{
    vis[u]=1;
    long long &ret=dp[u];
    if(ret!=-1) return ret;
    ret=1;
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(!vis[v])
            if(arr[now]<=arr[v] && arr[now]+d>=arr[v])
                if(!(arr[now]==arr[v] && now<v))
                    ret=(ret*(func(v)+1))%MOD;
    }
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, x, y;
    cin>>d>>n;
    for(int i=1;i<=n;i++) cin>>arr[i];
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        CLR(vis);
        SET(dp);
        now=i;
        ans=(ans+func(i))%MOD;
    }
    cout<<ans;
    return 0;
}

No comments:

Post a Comment