Submission #7357403
Source Code Expand
#include <bits/stdc++.h>
#define GET_REP(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__)
#define rep1(...) GET_REP()(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__)
#define _rep(i, n) irep (i, 0, n)
#define _rep1(i, n) irep1(i, 1, n)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(X, x) for (auto &&X : x)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "../../Lib/cout_container.hpp"
#define debug(x) cerr << #x " => " << x << endl
#else
#define debug(x) 0
#endif
using lint = long long;
constexpr int INF = 1 << 30;
constexpr lint INFL = 1LL << 62;
constexpr int MOD = (int)1e9 + 7;
constexpr double EPS = 1e-9;
using namespace std;
namespace { struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } INIT; }
using Weight = int;
struct Edge {
int src, dst;
Weight weight;
Edge(void) {}
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
Graph edges_to_graph(const Edges &edges, const int vertices_num, const bool directed = true) {
Graph ret(vertices_num);
for (const auto &e : edges) {
ret[e.src].push_back(e);
if (!directed) ret[e.dst].push_back(Edge(e.dst, e.src, e.weight));
}
return ret;
}
template <long long mod_num>
class ModInt {
long long n_;
public:
constexpr ModInt(const long long num = 0) : n_(num % mod_num) {}
constexpr const long long&value() const { return n_; }
constexpr bool operator==(const ModInt &r) const { return this->n_ == r.n_; }
constexpr bool operator==(const long long &r) const { return this->n_ == r; }
constexpr bool operator!=(const ModInt &r) const { return this->n_ != r.n_; }
constexpr bool operator!=(const long long &r) const { return this->n_ != r; }
constexpr bool operator<(const ModInt &r) const { return this->n_ < r.n_; }
constexpr bool operator<(const long long &r) const { return this->n_ < r; }
constexpr bool operator>(const ModInt &r) const { return this->n_ > r.n_; }
constexpr bool operator>(const long long &r) const { return this->n_ > r; }
constexpr bool operator<=(const ModInt &r) const { return this->n_ <= r.n_; }
constexpr bool operator<=(const long long &r) const { return this->n_ <= r; }
constexpr bool operator>=(const ModInt &r) const { return this->n_ >= r.n_; }
constexpr bool operator>=(const long long &r) const { return this->n_ >= r; }
constexpr ModInt operator+(const ModInt &r) const { return ModInt(*this) += r; }
constexpr ModInt operator-(const ModInt &r) const { return ModInt(*this) -= r; }
constexpr ModInt operator*(const ModInt &r) const { return ModInt(*this) *= r; }
constexpr ModInt operator/(const ModInt &r) const { return ModInt(*this) /= r; }
constexpr ModInt operator+(const long long &r) const { return ModInt(*this) += ModInt(r); }
constexpr ModInt operator-(const long long &r) const { return ModInt(*this) -= ModInt(r); }
constexpr ModInt operator*(const long long &r) const { return ModInt(*this) *= ModInt(r); }
constexpr ModInt operator/(const long long &r) const { return ModInt(*this) /= ModInt(r); }
friend ModInt operator+(const long long &l, const ModInt &r) { return ModInt(l) += r; }
friend ModInt operator-(const long long &l, const ModInt &r) { return ModInt(l) -= r; }
friend ModInt operator*(const long long &l, const ModInt &r) { return ModInt(l) *= r; }
friend ModInt operator/(const long long &l, const ModInt &r) { return ModInt(l) /= r; }
constexpr ModInt &operator++() { return *this += 1; }
constexpr ModInt &operator--() { return *this -= 1; }
constexpr ModInt &operator+=(const ModInt &r) {
n_ += r.n_;
if (n_ >= mod_num) n_ -= mod_num;
return *this;
}
constexpr ModInt &operator-=(const ModInt &r) {
if (n_ < r.n_) n_ += mod_num;
n_ -= r.n_;
return *this;
}
constexpr ModInt &operator*=(const ModInt &r) {
n_ = n_ * r.n_ % mod_num;
return *this;
}
constexpr ModInt &operator/=(ModInt r) {
long long exp = mod_num - 2;
while (exp) {
if (exp & 2) *this *= r;
r *= r;
exp /= 2;
}
return *this;
}
friend istream &operator>>(istream &is, ModInt<mod_num> &r) {
is >> r.n_;
r.n_ %= r.mod_num;
return is;
}
friend ostream &operator<<(ostream &os, const ModInt<mod_num> &r) { return os << r.n_; }
explicit operator int() const { return n_; }
explicit operator long long() const { return n_; }
explicit operator double() const { return n_; }
};
using mint = ModInt<MOD>;
void dfs(const Graph &g, int v, int p, int k, mint &sum) {
if (g[v].size() == 1) return;
rep (i, g[v].size() - 1) {
sum *= k - 2 - i;
}
for (const auto &es : g[v]) {
if (es.dst == p) continue;
dfs(g, es.dst, v, k, sum);
}
}
Graph graph(100001);
Edges edges(100001);
int main(void) {
int n, k;
cin >> n >> k;
// Edges edges(n - 1);
rep (i, n - 1) {
cin >> edges[i].src >> edges[i].dst;
--edges[i].src;
--edges[i].dst;
}
// Graph graph = edges_to_graph(edges, n, false);
rep (i, n - 1) {
graph[edges[i].src].push_back(edges[i]);
graph[edges[i].dst].push_back(Edge(edges[i].dst, edges[i].src, edges[i].weight));
}
int start, next;
rep (i, n) {
if (graph[i].size() == 1) {
start = i;
next = graph[i][0].dst;
break;
}
}
mint sum = k;
if (n > 1) {
sum *= (k - 1);
dfs(graph, next, start, k, sum);
}
cout << sum << endl;
return 0;
}
Submission Info
| Submission Time |
|
| Task |
E - Virus Tree 2 |
| User |
icia |
| Language |
C++14 (GCC 5.4.1) |
| Score |
500 |
| Code Size |
5888 Byte |
| Status |
AC |
| Exec Time |
39 ms |
| Memory |
14720 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 |
2 ms |
2560 KiB |
| 02.txt |
AC |
2 ms |
2560 KiB |
| 03.txt |
AC |
2 ms |
2560 KiB |
| 04.txt |
AC |
2 ms |
2560 KiB |
| 05.txt |
AC |
2 ms |
2560 KiB |
| 06.txt |
AC |
2 ms |
2560 KiB |
| 07.txt |
AC |
2 ms |
2560 KiB |
| 08.txt |
AC |
2 ms |
2560 KiB |
| 09.txt |
AC |
2 ms |
2560 KiB |
| 10.txt |
AC |
8 ms |
3584 KiB |
| 11.txt |
AC |
13 ms |
4352 KiB |
| 12.txt |
AC |
31 ms |
7040 KiB |
| 13.txt |
AC |
33 ms |
7680 KiB |
| 14.txt |
AC |
35 ms |
7552 KiB |
| 15.txt |
AC |
35 ms |
7552 KiB |
| 16.txt |
AC |
34 ms |
7864 KiB |
| 17.txt |
AC |
34 ms |
7864 KiB |
| 18.txt |
AC |
34 ms |
7864 KiB |
| 19.txt |
AC |
29 ms |
8116 KiB |
| 20.txt |
AC |
29 ms |
8116 KiB |
| 21.txt |
AC |
28 ms |
8116 KiB |
| 22.txt |
AC |
34 ms |
7864 KiB |
| 23.txt |
AC |
34 ms |
7864 KiB |
| 24.txt |
AC |
34 ms |
7864 KiB |
| 25.txt |
AC |
29 ms |
8116 KiB |
| 26.txt |
AC |
29 ms |
8116 KiB |
| 27.txt |
AC |
29 ms |
8116 KiB |
| 28.txt |
AC |
34 ms |
7864 KiB |
| 29.txt |
AC |
34 ms |
7864 KiB |
| 30.txt |
AC |
34 ms |
7864 KiB |
| 31.txt |
AC |
30 ms |
8116 KiB |
| 32.txt |
AC |
29 ms |
8116 KiB |
| 33.txt |
AC |
29 ms |
8116 KiB |
| 34.txt |
AC |
39 ms |
14720 KiB |
| 35.txt |
AC |
39 ms |
14720 KiB |
| s1.txt |
AC |
2 ms |
2560 KiB |
| s2.txt |
AC |
2 ms |
2560 KiB |
| s3.txt |
AC |
2 ms |
2560 KiB |