QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#579323 | #8078. Spanning Tree | SDNUyuqi | WA | 78ms | 17052kb | C++23 | 1.3kb | 2024-09-21 12:19:39 | 2024-09-21 12:19:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
const int N = 1e6 + 100;
vector<int>nx[N];
ll fa[N],fr[N],d[N],si[N];
void dfs(int be, int ne)
{
fr[ne] = be;
d[ne] = d[be] + 1;
for (auto i : nx[ne])
{
if (be == i)
{
continue;
}
dfs(ne, i);
}
}
int find(int x)
{
if (x != fa[x])
{
fa[x] = find(fa[x]);
}
return fa[x];
}
void hb(int x, int y)
{
int a = find(x);
int b = find(y);
if (d[a] <= d[b])
{
fa[b] = a;
si[a] += si[b];
}
else
{
fa[a] = b;
si[b] += si[a];
}
}
ll P = 998244353;
ll qp(ll a, ll b)
{
ll an = 1;
for (a %= P; b; a = a * a % P, b >>= 1)
{
if (b & 1)
{
an = an * a % P;
}
}
return an;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
fa[i] = i;
si[i] = 1;
}
vector<pair<int, int>>vr(n-1);
for (auto &[a,b]:vr)
{
cin >> a >> b;
}
for (int i = 2; i <= n; i++)
{
int a, b;
cin >> a >> b;
nx[a].push_back(b);
nx[b].push_back(a);
}
dfs(0, 1);
ll an = 1;
for (auto& [a, b] : vr)
{
int faa = find(a);
int fab = find(b);
if (find(fr[faa]) == fab || find(fr[fab] == faa))
{
an = an * qp(si[faa]*si[fab], P - 2) % P;
hb(faa, fab);
}
else
{
an = 0;
break;
}
}
cout << an << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7696kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 78ms
memory: 17052kb
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'