#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <atcoder/all>
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
long long ka() {
long long x = 0;
bool z = false;
while (1)
{
char y = getchar();
if (y == '-')
z = true;
else if ('0' <= y && y <= '9')
x = x * 10 + y - '0';
else
{
if (z)
x *= -1;
return x;
}
}
}
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
fastIO();
if (str != "") {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
} else if(str == "input") {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const long long mod = 1e9 + 7;
const int N = 2e5 + 10, M = 2e5 + 10;
vector<int> adj[N];
long long n, d;
long long binPowByMod(long long a, long long b) {
a %= mod;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long add(long long a, long long b) {
return (a + b) % mod;
}
long long mult(long long a, long long b) {
return (a * b) % mod;
}
long long sub(long long a, long long b) {
return (a - b + 2 * mod) % mod;
}
long long c[N], win, lose;
bool lw[N], curr[N];
int dfs_win(int node, int parent) {
int ok = 0;
for(auto i: adj[node]) {
if(i == parent) continue;
int cur = dfs_win(i, node);
if(!cur) ok = 1;
}
curr[node] = ok;
return ok;
}
void dfs_up(int node, int parent, bool par) {
int on = 0, off = 0;
for(auto i: adj[node]) {
if(i == parent) continue;
if(curr[i]) on++;
else off++;
}
if(par) on++;
else off++;
if(off) {
lw[node] = 1;
win++;
}
else lose++;
for(auto i: adj[node]) {
if(i == parent) continue;
if(curr[i]) on--;
else off--;
if(off) dfs_up(i, node, true);
else dfs_up(i, node, false);
if(curr[i]) on++;
else off++;
}
}
long long cc = 0;
struct tp {
bool valid, state;
int crit;
};
tp dp[N], ans[N];
void dfs_ct(int node, int parent) {
dp[node].valid = true;
dp[node].state = 0;
dp[node].crit = 1;
int los = 0;
for(auto i: adj[node]) {
if(i == parent) continue;
dfs_ct(i, node);
if(dp[i].state == 0) {
dp[node].state = 1;
los++;
}
}
if(los >= 2) {
dp[node].valid = false;
dp[node].crit = 0;
} else if(los == 1) {
dp[node].crit = 0;
for(auto i: adj[node]) {
if(i == parent) continue;
if(dp[i].state == dp[node].state) {
continue;
}
dp[node].crit += dp[i].crit;
}
} else if(los == 0) {
for(auto i: adj[node]) {
if(i == parent) continue;
dp[node].crit += dp[i].crit;
}
}
}
void dfs_ct_up(int node, int parent, tp par) {
int los = 0;
ans[node].valid = true;
ans[node].state = 0;
ans[node].crit = 1;
vector<pair<long long, long long>> pri;
for(auto i: adj[node]) {
if(i == parent) continue;
if(dp[i].state == 0) {
ans[node].state = 1;
pri.emplace_back(i, dp[i].crit);
los++;
}
}
if(par.state == 0) {
ans[node].state = 1;
pri.emplace_back(n + 1, par.crit);
los++;
}
long long lans = 0;
if(los >= 2) {
ans[node].valid = false;
ans[node].crit = 0;
} else if(los == 1) {
ans[node].crit = 0;
for(auto i: adj[node]) {
if(i == parent) continue;
if(dp[i].state == ans[node].state) {
lans += dp[i].crit;
continue;
}
lans += dp[i].crit;
ans[node].crit += dp[i].crit;
}
lans += par.crit;
if(ans[node].state != par.state) {
ans[node].crit += par.crit;
}
} else if(los == 0) {
for(auto i: adj[node]) {
if(i == parent) continue;
ans[node].crit += dp[i].crit;
}
ans[node].crit += par.crit;
}
for(auto i: adj[node]) {
if(i == parent) continue;
auto curent = ans[node];
if(dp[i].state == 0) {
los--;
if(los >= 2) {
assert(curent.valid == false);
assert(curent.crit == 0);
assert(curent.state == 1);
dfs_ct_up(i, node, curent);
} else if(los == 1) {
curent.valid = true;
curent.crit = 0;
curent.state = 1;
for(auto j: pri) {
if(j.first == i) continue;
curent.crit += j.second;
}
dfs_ct_up(i, node, curent);
} else {
curent.valid = true;
curent.state = 0;
curent.crit = 1;
curent.crit += lans;
curent.crit -= dp[i].crit;
dfs_ct_up(i, node, curent);
}
los++;
} else {
if(los == 0) {
curent.crit -= dp[i].crit;
}
dfs_ct_up(i, node, curent);
}
}
}
struct Matrix {
long long a[2][2] = {{0, 0}, {0, 0}};
Matrix operator *(const Matrix& another) {
Matrix product;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
long long cur = mult(a[i][j], another.a[j][k]);
product.a[i][k] = add(product.a[i][k], cur);
}
}
}
return product;
}
};
Matrix binpow(Matrix a, long long power) {
power--;
Matrix answ = a;
while(power > 0) {
if(power & 1) {
answ = answ * a;
}
a = a * a;
power /= 2;
}
return answ;
}
void solve_() {
cin >> n >> d;
for(int i = 1; i <= n - 1; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
// calc win_lose
int cur = dfs_win(1, 0);
dfs_up(1, 0, true);
dfs_ct(1, 0);
dfs_ct_up(1, 0, {0, 1, 0});
// calc crit
long long E = 0;
for(int i = 1; i <= n; i++) {
c[i] = ans[i].crit;
if(lw[i]) {
E += c[i];
E %= mod;
} else {
E -= c[i];
E += 2 * mod;
E %= mod;
}
}
E += 2 * mod;
E %= mod;
long long answ = 0;
Matrix mt;
mt.a[0][0] = (n * n) % mod, mt.a[0][1] = 0;
mt.a[1][0] = lose, mt.a[1][1] = E;
mt = binpow(mt, d);
mt.a[1][0] %= mod;
if(lw[1]) {
answ = sub(binPowByMod(n, 2 * d), mult(c[1], mt.a[1][0]));
} else {
answ = add(answ, mult(c[1], mt.a[1][0]));
}
cout << answ << '\n';
}
int main() {
setIO();
auto solve = [&](int test_case)-> void {
while(test_case--) {
solve_();
}
};
int test_cases = 1;
// cin >> test_cases;
solve(test_cases);
return 0;
}
Compilation message
startrek.cpp: In function 'void solve_()':
startrek.cpp:361:7: warning: unused variable 'cur' [-Wunused-variable]
361 | int cur = dfs_win(1, 0);
| ^~~
startrek.cpp: In function 'void setIO(std::string)':
startrek.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5028 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
12 |
Correct |
67 ms |
19720 KB |
Output is correct |
13 |
Correct |
66 ms |
29064 KB |
Output is correct |
14 |
Correct |
32 ms |
11828 KB |
Output is correct |
15 |
Correct |
63 ms |
11940 KB |
Output is correct |
16 |
Correct |
60 ms |
11852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5160 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5028 KB |
Output is correct |
25 |
Correct |
4 ms |
5028 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5204 KB |
Output is correct |
28 |
Correct |
2 ms |
5076 KB |
Output is correct |
29 |
Correct |
2 ms |
5088 KB |
Output is correct |
30 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
12 |
Correct |
67 ms |
19720 KB |
Output is correct |
13 |
Correct |
66 ms |
29064 KB |
Output is correct |
14 |
Correct |
32 ms |
11828 KB |
Output is correct |
15 |
Correct |
63 ms |
11940 KB |
Output is correct |
16 |
Correct |
60 ms |
11852 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5028 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
5020 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5160 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5028 KB |
Output is correct |
30 |
Correct |
4 ms |
5028 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
2 ms |
5076 KB |
Output is correct |
34 |
Correct |
2 ms |
5088 KB |
Output is correct |
35 |
Correct |
3 ms |
5032 KB |
Output is correct |
36 |
Correct |
49 ms |
19676 KB |
Output is correct |
37 |
Correct |
65 ms |
28980 KB |
Output is correct |
38 |
Correct |
32 ms |
11812 KB |
Output is correct |
39 |
Correct |
41 ms |
11892 KB |
Output is correct |
40 |
Correct |
56 ms |
11836 KB |
Output is correct |
41 |
Correct |
52 ms |
24588 KB |
Output is correct |
42 |
Correct |
68 ms |
26888 KB |
Output is correct |
43 |
Correct |
40 ms |
11048 KB |
Output is correct |
44 |
Correct |
64 ms |
11876 KB |
Output is correct |
45 |
Correct |
43 ms |
11832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5032 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5036 KB |
Output is correct |
19 |
Correct |
67 ms |
19720 KB |
Output is correct |
20 |
Correct |
66 ms |
29064 KB |
Output is correct |
21 |
Correct |
32 ms |
11828 KB |
Output is correct |
22 |
Correct |
63 ms |
11940 KB |
Output is correct |
23 |
Correct |
60 ms |
11852 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
5024 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
5020 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
2 ms |
4948 KB |
Output is correct |
33 |
Correct |
3 ms |
5160 KB |
Output is correct |
34 |
Correct |
4 ms |
5204 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5028 KB |
Output is correct |
37 |
Correct |
4 ms |
5028 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
2 ms |
5076 KB |
Output is correct |
41 |
Correct |
2 ms |
5088 KB |
Output is correct |
42 |
Correct |
3 ms |
5032 KB |
Output is correct |
43 |
Correct |
49 ms |
19676 KB |
Output is correct |
44 |
Correct |
65 ms |
28980 KB |
Output is correct |
45 |
Correct |
32 ms |
11812 KB |
Output is correct |
46 |
Correct |
41 ms |
11892 KB |
Output is correct |
47 |
Correct |
56 ms |
11836 KB |
Output is correct |
48 |
Correct |
52 ms |
24588 KB |
Output is correct |
49 |
Correct |
68 ms |
26888 KB |
Output is correct |
50 |
Correct |
40 ms |
11048 KB |
Output is correct |
51 |
Correct |
64 ms |
11876 KB |
Output is correct |
52 |
Correct |
43 ms |
11832 KB |
Output is correct |
53 |
Correct |
56 ms |
29004 KB |
Output is correct |
54 |
Correct |
86 ms |
25532 KB |
Output is correct |
55 |
Correct |
38 ms |
10484 KB |
Output is correct |
56 |
Correct |
48 ms |
19328 KB |
Output is correct |
57 |
Correct |
41 ms |
11912 KB |
Output is correct |
58 |
Correct |
58 ms |
11904 KB |
Output is correct |
59 |
Correct |
41 ms |
11832 KB |
Output is correct |
60 |
Correct |
46 ms |
11904 KB |
Output is correct |