QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#576085 | #8078. Spanning Tree | xinhuo2005 | WA | 33ms | 10980kb | C++20 | 1.9kb | 2024-09-19 18:19:00 | 2024-09-19 18:19:02 |
Judging History
answer
#include<bits/stdc++.h>
using ll = long long;
const int mod = 998244353;
inline ll qpow(ll a, ll b) {
if (b == 0) return 1ll;
if (b == 1) return a;
ll res = qpow(a, b / 2);
res = res * res % mod;
if (b & 1) return res * a % mod;
return res;
}
void solve() {
int n;
std::cin >> n;
std::vector<int> fa(n), dep(n), dis(n, 0x3f3f3f3f), siz(n, 1);
std::vector<std::vector<int>> adj(n);
std::vector<std::pair<int, int>> a(n - 1);
for (int i = 0; i < n - 1; i++) {
std::cin >> a[i].first >> a[i].second;
a[i].first --;
a[i].second --;
}
for (int i = 1; i < n; i++) {
int x, y;
std::cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
auto dfs = [&](auto& self, int u, int fno) -> void {
for (auto v : adj[u]) {
if (v == fno) continue;
dep[v] = dep[u] + 1;
self(self, v, u);
}
};
dfs(dfs, 0, -1);
auto find = [&](auto& self, int x) -> int {
if (fa[x] == x) {
return x;
}
int t = self(self, fa[x]);
fa[x] = t;
return t;
};
auto merge = [&](int x, int y) {
x = find(find, x);
y = find(find, y);
if (dep[x] < dep[y]) std::swap(x, y);
fa[x] = y;
siz[y] += siz[x];
};
ll ans = 1;
for (int i = 0; i < n - 1; i++) {
int x = a[i].first, y = a[i].second;
int fx = find(find, x), fy = find(find, y);
if (abs(dep[fx] - dep[fy]) <= 1) {
ans = ((ans * siz[x]) % mod) * siz[y] % mod;
merge(x, y);
}
}
std::cout << qpow(ans, mod - 2) << "\n";
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 33ms
memory: 10980kb
input:
100000 2183 5543 22424 59062 8387 24544 14668 74754 15788 58136 26846 99981 13646 57752 29585 62862 27383 65052 2013 13116 24490 91945 26006 61595 19000 78817 31371 67837 29311 82989 4298 20577 23212 77962 16510 85839 26010 98714 25314 96063 17206 82359 7478 76540 13890 99556 23277 64321 25684 92613...
output:
0
result:
wrong answer 1st lines differ - expected: '330864231', found: '0'