This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 400010
#define INF (ll)(1e9+10)
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
int N,D,a,b;
vi adj[maxn];
int qexp(int a,int b){
int ans = 1;
while (b){
//~ cout << a << ' ' << b << ' ' << ans << '\n';
if (b & 1) (ans *= a) %= MOD;
(a *= a) %= MOD;
b >>= 1;
}
return ans;
}
bool state[maxn];
int dp[maxn], num[maxn];
set <int> st[maxn];
int sm[maxn];
void dfs(int x,int p){
state[x] = 0; dp[x] = num[x] = 0;
aFOR(i,adj[x]) if (i != p){
dfs(i,x);
if (!state[i]){
num[x]++; st[x].insert(i);state[x] = 1;
}
sm[x] += dp[i];
}
if (num[x] == 1){
aFOR(i, adj[x]) if (i != p && !state[i]) dp[x] = dp[i];
}else if (num[x] == 0){
aFOR(i,adj[x]) if (i != p) dp[x] += dp[i];
}
if (!state[x]) dp[x]++;
}
bool win[maxn];
int crit[maxn];
void reroot(int x,int p){
crit[x] = dp[x];
win[x] = state[x];
aFOR(i,adj[x]) if (i != p){
int statex = state[x], statei = state[i], numx = num[x], numi = num[i], dpx = dp[x], dpi = dp[i];
if (!state[i]) num[x]--;
state[x] = (num[x] > 0);
if (!state[x]) st[i].insert(x);
if (!state[i]) st[x].erase(i);
if (!state[x]) num[i]++;
state[i] = (num[i] > 0);
sm[x] -= dp[i];
dp[x] = 0;
if (num[x] == 1) dp[x] = dp[*st[x].begin()];
else if (num[x] == 0) dp[x] = sm[x];
if (!state[x]) dp[x]++;
sm[i] += dp[x];
dp[i] = 0;
if (num[i] == 1) dp[i] = dp[*st[i].begin()];
else if (num[i] == 0) dp[i] = sm[i];
if (!state[i]) dp[i]++;
reroot(i,x);
if (!state[x]) st[i].erase(x);
state[x] = statex; state[i] = statei;
if (!state[i]) st[x].insert(i);
num[x] = numx; num[i] = numi;
sm[i] -= dp[x];
dp[x] = dpx; dp[i] = dpi;
sm[x] += dp[i];
}
}
int32_t main(){
fast;
cin >> N >> D;
FOR(i,1,N-1){
cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
}
dfs(1,-1);
reroot(1,-1);
int E = 0;
FOR(i,1,N){
if (win[i]) E += crit[i];
else E -= crit[i];
//cout << crit[i] << ' ';
}
//cout << '\n';
int L = 0;
FOR(i,1,N) if (!win[i]) L++;
//cout << L << ' ' << E << '\n';
D--;
int invN = qexp(N, MOD - 2);
int EN2 = (E * ((invN * invN) % MOD)) % MOD;
int N2D = qexp(N, 2*D);
int LD1 = (((((L * N2D) % MOD) * (qexp(EN2, D) - 1)) % MOD) * qexp(EN2 - 1, MOD - 2)) % MOD;
(LD1 += (qexp(E, D) * L) % MOD) %= MOD;
D++;
//cout << LD1 << ' ';
int ans;
if (win[1]) ans = (crit[1] * LD1) % MOD;
else ans = qexp(N, 2*D) - (crit[1] * LD1) % MOD;
ans = qexp(N, 2*D) - ans;
ans %= MOD; ans += MOD; ans %= MOD;
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |