QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#569340 | #8078. Spanning Tree | longyin | WA | 56ms | 69404kb | C++20 | 2.0kb | 2024-09-16 22:14:53 | 2024-09-16 22:14:53 |
Judging History
answer
#include <bits/stdc++.h>
//#define int long long
#pragma GCC optimize(2)
#define endl "\n"
using namespace std;
using ll = long long;
const int MOD = 998244353;
const int N = 1e6 + 5;
ll quickPower(ll a, ll n) {
if (a == 0)
return 0;
ll res = 1;
while (n) {
if (n & 1)
res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
ll inv(ll x) {
return quickPower(x, MOD - 2);
}
set<int> edges[N];
pair<int, int> query[N];
int dep[N];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
for (int v : edges[u]) {
if (v == fa)
continue;
dfs(v, u);
}
}
struct DSU {
int fa[N];
int cnt[N];
int dep[N];
DSU(int n = N - 1) {
for (int i = 1; i <= n; i++) {
fa[i] = i;
cnt[i] = 1;
}
}
void init(int n, int* dep) {
for (int i = 1; i <= n; i++) {
this->dep[i] = dep[i];
}
}
int find(int x) {
return fa[x] = x == fa[x] ? x : find(fa[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (dep[u] > dep[v]) {
fa[u] = v;
cnt[v] += cnt[u];
}
else {
fa[v] = u;
cnt[u] += cnt[v];
}
}
} dsu;
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> query[i].first >> query[i].second;
}
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
edges[u].emplace(v);
edges[v].emplace(u);
}
dfs(1, 0);
dsu.init(n, dep);
ll ans = 1;
for (int i = 1; i <= n - 1; i++) {
auto [u, v] = query[i];
u = dsu.find(u), v = dsu.find(v);
if (!edges[u].count(v))
ans = 0;
ans = ans * dsu.cnt[u] % MOD * dsu.cnt[v] % MOD;
dsu.merge(u, v);
}
cout << inv(ans) << endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 9ms
memory: 59560kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 56ms
memory: 69404kb
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'