Cod sursa(job #1484362)

Utilizator akaprosAna Kapros akapros Data 10 septembrie 2015 22:17:03
Problema Colorare3 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define mod 1000000007
#define maxN 100002
using namespace std;
int n, i, j, k, vis[maxN];
vector < int > V[maxN];
long long sol;
void write();
void read()
{
    int x, y;
    freopen("colorare3.in", "r", stdin);
    scanf("%d %d", &n, &k);
    for (i = 1; i <= n - 1; ++ i)
    {
        scanf("%d %d", &x, &y);
        V[x].push_back(y);
        V[y].push_back(x);
    }
}
void dfs(int x)
{
    int node, i;
    long long y;
    vis[x] = 1;
    y = k * 1LL;
    for (i = 0; i < V[x].size(); ++ i)
        if (!vis[node = V[x][i]])
    {
        sol = (sol * y * 1LL) % mod;
        if (y <= 0)
        {
            sol = 0;
            write();
        }
        dfs(node);
        -- y;
    }
    else
        -- y;
}
void solve()
{
    sol = 1;
    dfs(1);
}
void write()
{
    freopen("colorare3.out", "w", stdout);
    printf("%d", sol);
    exit(0);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}