QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#556846 | #8078. Spanning Tree | szlhc | WA | 43ms | 43596kb | C++14 | 1.4kb | 2024-09-10 21:25:29 | 2024-09-10 21:25:29 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
const int N = 1e6 + 5, mod = 998244353;
inline long long fpow (long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res % mod;
}
int n, S[N], siz[N], fa[N], dep[N];
long long inv[N];
pair <int, int> a[N];
vector <int> g[N];
int getf (int x) {return x == S[x] ? x : S[x] = getf(S[x]);}
void dfs (int u, int nfa) {
fa[u] = nfa, dep[u] = dep[nfa] + 1;
for (int v : g[u]) if (v != nfa) dfs(v, u);
}
int main() {
scanf ("%d", &n);
for (int i = 1; i < n; i++) scanf ("%d%d", &a[i].first, &a[i].second);
for (int i = 1, u, v; i < n; i++) {
scanf ("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) S[i] = i, siz[i] = 1;
for (int i = 1; i <= n; i++) inv[i] = fpow(i, mod - 2);
long long ans = 1;
for (int i = 1; i < n; i++) {
int u = a[i].first, v = a[i].second;
int x = getf(u), y = getf(v);
if (x == y || (fa[x] != y && fa[y] != x)) {printf ("0\n"); return 0;}
ans = ans * inv[siz[x]] % mod * inv[siz[y]] % mod;
if (dep[x] > dep[y]) S[x] = y, siz[y] += siz[x];
else S[y] = x, siz[x] += siz[y];
}
printf ("%lld\n", ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 37036kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 43ms
memory: 43596kb
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'