Submission #6327667
Source Code Expand
// {{{
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// }}}
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
static constexpr int mod = (int)1e9 + 7;
static constexpr int inf = 100100100;
static constexpr ll linf = 1e18;
static constexpr double eps = 1e-9;
static constexpr double pi = 3.14159265359;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n; i >= 0; --i)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define pb push_back
#define ist insert
#define fst first
#define snd second
// {{{
template <typename T>
struct modint {
static_assert(std::is_integral<T>::value, "T must be integral");
modint(T value = 0, T mod = (T)1e9 + 7) : value(value % mod), mod(mod) {}
modint operator +(const modint& rhs) const {
return modint(*this) += rhs;
}
modint operator -(const modint& rhs) const {
return modint(*this) -= rhs;
}
modint operator *(const modint& rhs) const {
return modint(*this) *= rhs;
}
modint operator /(const modint& rhs) const {
return modint(*this) /= rhs;
}
modint& operator +=(const modint& rhs) {
value += rhs.value;
if (value >= mod) {
value -= mod;
}
return *this;
}
modint& operator -=(const modint& rhs) {
if (value < rhs.value) {
value += mod;
}
value -= rhs.value;
return *this;
}
modint& operator *=(const modint& rhs) {
value = value * rhs.value % mod;
return *this;
}
modint& operator /=(const modint& rhs) {
modint tmp = rhs;
T exp = mod - 2;
while (exp) {
if (exp % 2) {
*this *= tmp;
}
tmp *= tmp;
exp /= 2;
}
return *this;
}
bool operator ==(const modint& rhs) const {
return value == rhs.value;
}
bool operator !=(const modint& rhs) const {
return !(*this == rhs);
}
bool operator >(const modint& rhs) const {
return value > rhs.value;
}
bool operator <(const modint& rhs) const {
return value < rhs.value;
}
bool operator >=(const modint& rhs) const {
return !(*this < rhs);
}
bool operator <=(const modint& rhs) const {
return !(*this > rhs);
}
friend ostream& operator <<(ostream &out, const modint& m) {
out << m.value;
return out;
}
friend istream& operator >>(istream &in, modint& m) {
in >> m.value;
m.value %= m.mod;
return in;
}
T value;
T mod;
};
// }}}
modint<ll> dfs(ll K, const vector<vector<ll>>& G, ll cur, ll par) {
ll k;
if (par == -1) {
k = K - 1;
} else {
k = K - 2;
}
if (K < G[cur].size()) {
return 0;
}
modint<ll> c = 1;
for (auto e : G[cur]) {
if (e == par) continue;
c *= k;
--k;
}
for (auto e : G[cur]) {
if (e == par) continue;
c *= dfs(K, G, e, cur);
}
return c;
}
int main() {
// cin.tie(0);
// ios_base::sync_with_stdio(false);
ll N, K;
cin >> N >> K;
vector<vector<ll>> G(N);
rep (i, N - 1) {
ll a, b;
cin >> a >> b;
--a; --b;
G[a].pb(b);
G[b].pb(a);
}
modint<ll> ans = dfs(K, G, 0, -1) * K;
cout << ans << endl;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Virus Tree 2 |
| User | gochiusa |
| Language | C++14 (Clang 3.8.0) |
| Score | 500 |
| Code Size | 3924 Byte |
| Status | AC |
| Exec Time | 179 ms |
| Memory | 11648 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | s1.txt, s2.txt, s3.txt |
| All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, s1.txt, s2.txt, s3.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01.txt | AC | 1 ms | 256 KiB |
| 02.txt | AC | 1 ms | 256 KiB |
| 03.txt | AC | 1 ms | 256 KiB |
| 04.txt | AC | 1 ms | 256 KiB |
| 05.txt | AC | 1 ms | 256 KiB |
| 06.txt | AC | 1 ms | 256 KiB |
| 07.txt | AC | 1 ms | 256 KiB |
| 08.txt | AC | 1 ms | 256 KiB |
| 09.txt | AC | 1 ms | 256 KiB |
| 10.txt | AC | 32 ms | 1408 KiB |
| 11.txt | AC | 55 ms | 2176 KiB |
| 12.txt | AC | 142 ms | 5120 KiB |
| 13.txt | AC | 163 ms | 5888 KiB |
| 14.txt | AC | 163 ms | 5888 KiB |
| 15.txt | AC | 160 ms | 5760 KiB |
| 16.txt | AC | 173 ms | 6392 KiB |
| 17.txt | AC | 174 ms | 6392 KiB |
| 18.txt | AC | 173 ms | 6392 KiB |
| 19.txt | AC | 166 ms | 6516 KiB |
| 20.txt | AC | 165 ms | 6516 KiB |
| 21.txt | AC | 166 ms | 6516 KiB |
| 22.txt | AC | 173 ms | 6392 KiB |
| 23.txt | AC | 175 ms | 6392 KiB |
| 24.txt | AC | 169 ms | 6392 KiB |
| 25.txt | AC | 165 ms | 6516 KiB |
| 26.txt | AC | 166 ms | 6516 KiB |
| 27.txt | AC | 165 ms | 6516 KiB |
| 28.txt | AC | 173 ms | 6392 KiB |
| 29.txt | AC | 174 ms | 6392 KiB |
| 30.txt | AC | 172 ms | 6392 KiB |
| 31.txt | AC | 165 ms | 6516 KiB |
| 32.txt | AC | 165 ms | 6516 KiB |
| 33.txt | AC | 163 ms | 6516 KiB |
| 34.txt | AC | 179 ms | 11648 KiB |
| 35.txt | AC | 179 ms | 11520 KiB |
| s1.txt | AC | 1 ms | 256 KiB |
| s2.txt | AC | 1 ms | 256 KiB |
| s3.txt | AC | 1 ms | 256 KiB |