/*
*|---------------------------------------------|*
*| Author : Le Hoang An |*
*| From : Bien Hoa Gifted High School |*
*|---------------------------------------------|*
*| ADOMINATION |*
*|---------------------------------------------|*
*/
#pragma GCC optimize("02,unroll-loops")
#include <bits/stdc++.h>
#define f0(i, n) for(int i = 0; i < n; i++)
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define el cout << "\n";
//#define sz(A) (int)A.size()
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define FOD(i, r, l) for (int i = r; i >= l; i--)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define all(x) x.begin(), x.end()
#define AtLeastThirdPrizeVOI \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define AnWillWin signed main()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef string str;
typedef vector<int> vi;
using ull = unsigned long long;
const int maxn = 150000;
const ll inf = 5e18;
const int mod = 998244353;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
template <class X, class Y>
bool maximize(X &x, const Y &y){
if(x < y){
x = y;
return true;
}
return false;
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
if(x > y){
x = y;
return true;
}
return false;
}
void file(){
#define TASK "BanhsEs"
if(fopen(TASK ".INP", "r")){
freopen(TASK ".INP", "r", stdin);
freopen(TASK ".OUT", "w", stdout);
}
}
bool canWin[maxn];
int cnt[maxn], cric[maxn], sumW[maxn], sumL[maxn], dp[maxn];
vi adj[maxn];
void dfs_game(int u, int par){
canWin[u] = false;
cnt[u] = 0;
for(int v : adj[u]) if(v != par){
dfs_game(v, u);
canWin[u] |= !canWin[v];
cnt[u] += !canWin[v];
}
}
void calc(int u){
if(canWin[u]){
if(cnt[u] >= 2) cric[u] = 0;
else cric[u] = sumL[u];
}
else cric[u] = sumW[u] + 1;
}
void dfs_cric(int u, int p){
sumW[u] = sumL[u] = 0;
for(int v : adj[u]) if(v != p){
dfs_cric(v, u);
if(canWin[v]) sumW[u] += cric[v];
else sumL[u] += cric[v];
}
calc(u);
}
void reroot(int u, int v){
if(canWin[v]) sumW[v] -= cric[v];
else {
sumL[u] -= cric[v];
cnt[u]--;
}
canWin[u] = (cnt[u] > 0);
calc(u);
if(canWin[u]) sumW[v] += cric[u];
else{
sumL[v] += cric[u];
cnt[v]++;
}
canWin[v] = (cnt[v] > 0);
calc(v);
}
int loser = 0, c = 0;
void dfs(int u, int p){
loser += !canWin[u];
c += (canWin[u] ? 1 : -1) * cric[u];
for(int v : adj[u]) if(v != p){
reroot(u, v); dfs(v, u);
reroot(v, u);
}
}
int Pow(int x, int n){
if(n == 0) return 1;
int p = Pow(x, n / 2);
p = 1ll * p * p % mod;
if(n & 1) p = 1ll * p * x % mod;
return p;
}
int n, D;
AnWillWin{
AtLeastThirdPrizeVOI;
file();
cin >> n >> D;
FOR(i, 1, n - 1){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
c = 0;
FOR(r, 1, n){
dfs_game(r, 0);
dfs_cric(r, 0);
c += (canWin[r] ? 1 : -1) * cric[r];
loser += !canWin[r];
}
while(c < 0) c += mod;
dp[0] = loser;
FOR(i, 1, D - 1)
dp[i] = (loser * Pow(n, 2 * i) + c * dp[i - 1]) % mod;
dfs_game(1, 0); dfs_cric(1, 0);
if(canWin[1])
cout << (Pow(n, 2 * D) - dp[D - 1] * cric[1] % mod + 1ll * mod * mod) % mod;
else cout << dp[D - 1] * cric[1] % mod;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
startrek.cpp: In function 'void file()':
startrek.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(TASK ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
startrek.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(TASK ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |