Submission #74717991
Source Code Expand
#include<bits/extc++.h>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx,avx2")
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define coutc "\033[48;5;196m\033[38;5;15m"
#define endc "\033[0m"
#define M(_1, _2, _3, _4, NAME, ...) NAME
#define rep(...) \
M(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rep4(_, x, n, s) \
for (int _ = x; (s < 0) ? _ > n : _ < n; _ += s)
#define rep3(_, x, n) rep4(_, x, n, (x < n ? 1 : -1))
#define rep2(_, n) rep3(_, 0, n)
#define rep1(n) rep2(i, n)
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
#define mp make_pair
// #define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
// #define cin fin
// #define cout fout
// ifstream fin("word.in");
// ofstream fout("word.out");
const int inf = INT_MAX;
const int MOD = 1000000007;
double PI = 4*atan(1);
#ifdef DEBUG
string to_string(char c) { return string({c}); }
// 7
template<class... Ts>
ostream& operator<<(ostream& o, tuple<Ts...> t) {
string s = "(";
apply([&](auto&&... r) {
((s += to_string(r) + ", "), ...); }, t);
return o << s.substr(0, len(s) - 2) + ")";
}
// 3
ostream& operator<<(ostream &o, pair<auto, auto> p) {
return o << "(" << p.fi << ", " << p.se << ")";
}
// 7
template<class C, class T = typename C::value_type,
typename std::enable_if<!std::is_same<C, std::string>
::value>::type* = nullptr>
ostream& operator<<(ostream &o, C c) {
for (auto e : c) o << setw(7) << right << e;
return o << endc << endl << coutc;
}
// 7
void debug(const auto &e, const auto &... r) {
cout << coutc << e;
((cout << " " << r), ..., (cout << endc << endl));
}
#else
#define debug(...)
#endif
map<string,int> mon;
map<int,string> inp;
ll pw(ll a, ll e) {
ll r = 1;
while (e) {
if (e & 1) r = r * a % 998244353;
a = a * a % 998244353;
e >>= 1;
}
return r;
}
void _main(int tc) {
if (tc == 0) {
int T;
cin >> T;
rep(i, T) _main(i + 1);
return;
}
int N, K;
cin >> N >> K;
vector<vi> g(N + 1);
int i = 0;
while (i < N - 1) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
i++;
}
vi p(N + 1), ord;
ord.reserve(N);
stack<int> st;
st.push(1);
p[1] = -1;
while (!st.empty()) {
int u = st.top();
st.pop();
ord.push_back(u);
int j = 0;
while (j < (int)g[u].size()) {
int v = g[u][j];
if (v != p[u]) {
p[v] = u;
st.push(v);
}
j++;
}
}
vi sz(N + 1, 1);
ll s0 = 0;
i = N - 1;
while (i > 0) {
int u = ord[i];
s0 = (s0 + 1LL * sz[u] * (N - sz[u])) % 998244353;
sz[p[u]] += sz[u];
i--;
}
ll up = 1LL * (N + K - 1) % 998244353 * (N + K) % 998244353 * (N + K + 1) % 998244353;
ll dn = 1LL * (N - 1) % 998244353 * N % 998244353 * (N + 1) % 998244353;
ll ans = s0 * up % 998244353 * pw(dn, 998244353 - 2) % 998244353;
cout << ans << endl;
}
// 5
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
_main(0), exit(0);
int tc; cin >> tc; rep(i, tc) _main(i + 1);
}
Submission Info
| Submission Time |
|
| Task |
E - Tree Growing |
| User |
zaahir |
| Language |
C++23 (GCC 15.2.0) |
| Score |
900 |
| Code Size |
3928 Byte |
| Status |
AC |
| Exec Time |
92 ms |
| Memory |
25736 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
900 / 900 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_00.txt |
| All |
00_sample_00.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_00.txt |
AC |
1 ms |
3564 KiB |
| 01_handmade_00.txt |
AC |
1 ms |
3636 KiB |
| 01_handmade_01.txt |
AC |
50 ms |
3632 KiB |
| 01_handmade_02.txt |
AC |
92 ms |
23228 KiB |
| 01_handmade_03.txt |
AC |
82 ms |
23504 KiB |
| 01_handmade_04.txt |
AC |
54 ms |
25736 KiB |
| 02_random_00.txt |
AC |
79 ms |
23356 KiB |
| 02_random_01.txt |
AC |
80 ms |
23368 KiB |
| 02_random_02.txt |
AC |
78 ms |
23436 KiB |
| 02_random_03.txt |
AC |
78 ms |
23368 KiB |
| 02_random_04.txt |
AC |
79 ms |
23360 KiB |
| 02_random_05.txt |
AC |
78 ms |
23436 KiB |
| 02_random_06.txt |
AC |
73 ms |
23624 KiB |
| 02_random_07.txt |
AC |
73 ms |
23624 KiB |
| 02_random_08.txt |
AC |
73 ms |
23624 KiB |
| 02_random_09.txt |
AC |
74 ms |
23624 KiB |
| 02_random_10.txt |
AC |
73 ms |
23504 KiB |
| 02_random_11.txt |
AC |
75 ms |
23624 KiB |
| 02_random_12.txt |
AC |
37 ms |
3724 KiB |
| 02_random_13.txt |
AC |
43 ms |
3764 KiB |
| 02_random_14.txt |
AC |
42 ms |
3996 KiB |
| 02_random_15.txt |
AC |
53 ms |
7168 KiB |
| 02_random_16.txt |
AC |
90 ms |
23376 KiB |
| 02_random_17.txt |
AC |
82 ms |
23360 KiB |
| 02_random_18.txt |
AC |
84 ms |
23368 KiB |
| 02_random_19.txt |
AC |
83 ms |
23376 KiB |
| 02_random_20.txt |
AC |
86 ms |
23368 KiB |
| 02_random_21.txt |
AC |
88 ms |
23304 KiB |
| 02_random_22.txt |
AC |
78 ms |
23616 KiB |
| 02_random_23.txt |
AC |
88 ms |
23288 KiB |
| 02_random_24.txt |
AC |
89 ms |
23228 KiB |
| 02_random_25.txt |
AC |
84 ms |
23496 KiB |