Cod sursa(job #1841548)

Utilizator touristGennady Korotkevich tourist Data 5 ianuarie 2017 18:42:02
Problema Colorare3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
#define Nmax 100005
#define MOD 1000000007
#define pb push_back

using namespace std;

int dp[Nmax],f0[Nmax],f1[Nmax],n,k;
vector <int> L[Nmax];

class Parser {
 public:
  Parser(const char *path) {
    in.open(path);
    in.read(buffer, kSize);
    cursor = 0;
  }
  Parser& operator>>(int &x) {
    x = 0;
    while (buffer[cursor] < '0')
      advance();
    while (buffer[cursor] >= '0') {
      x = x * 10 + buffer[cursor] - '0';
      advance();
    }
    return *this;
  }
 private:
  void advance() {
    if (++cursor == kSize) {
      in.read(buffer, kSize);
      cursor = 0;
    }
  }
  static const int kSize = 500000;
  char buffer[kSize];
  int cursor;
  ifstream in;
};

inline void Dfs(int nod, int tata)
{
    int cnt=0;
    dp[nod]=1;
    for(auto it : L[nod])
        if(it!=tata)
        {
            Dfs(it,nod);
            dp[nod]=(1LL*dp[nod]*dp[it])%MOD;
            ++cnt;
        }

    if(tata) dp[nod]=(1LL*dp[nod]*f1[cnt])%MOD;
    else dp[nod]=(1LL*dp[nod]*f0[cnt])%MOD;
}

int main()
{
    int i,x,y;
    Parser cin("colorare3.in");
    ofstream cout("colorare3.out");
    cin>>n>>k;

    for(i=f0[0]=1;i<=n;++i) f0[i]=(1LL*f0[i-1]*(k-i+1))%MOD;
    for(i=f1[0]=1;i<=n;++i) f1[i]=(1LL*f1[i-1]*(k-i))%MOD;

    for(i=1;i<n;++i)
    {
        cin>>x>>y;
        L[x].pb(y); L[y].pb(x);
    }
    Dfs(1,0);
    cout<<dp[1]<<"\n";
    return 0;
}