QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#547270 | #8078. Spanning Tree | potential | WA | 75ms | 25924kb | C++20 | 2.7kb | 2024-09-04 19:56:15 | 2024-09-04 19:56:16 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
# define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
# define int long long
# define lowbit(x) (x & (-x))
# define fi first
# define se second
// # define endl '\n'
inline int Read();
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int N = 1e6 + 10;
map <int, int> mp;
int qmi(int a, int b){
a %= MOD;
int ans = 1;
while(b){
if(b & 1) ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
vector <int> v[N];
vector <PII> u;
int vis[N], f[N], siz[N], p[N], dep[N], fa[N];
int find(int x){
if(x == f[x]) return x;
return f[x] = find(f[x]);
}
void dfs(int x, int y, int ff){
vis[x] = 1;
dep[x] = y;
fa[x] = ff;
for(auto i : v[x]){
if(vis[i]) continue;
dfs(i, y + 1, x);
}
}
void add(int x, int y){
int xx = find(x);
int yy = find(y);
if(xx == yy) return;
if(dep[yy] <= dep[xx]){
f[xx] = yy;
siz[yy] += siz[xx];
}else{
f[yy] = xx;
siz[xx] += siz[yy];
}
}
void Solve(){
int n;
cin >> n;
for(int i = 1; i <= n; i ++){
f[i] = i;
siz[i] = 1;
p[i] = 1;
}
u.push_back({0, 0});
for(int i = 1; i < n; i ++){
int x, y;
cin >> x >> y;
u.push_back({x, y});
}
for(int i = 1; i < n; i ++){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 1, 0);
int ans = 1;
for(int i = 1; i < n; i ++){
int x = u[i].fi;
int y = u[i].se;
int xx = find(x);
int yy = find(y);
if(fa[xx] == yy || fa[yy] == xx){
int num = siz[xx] * siz[yy] % MOD;
int pp = qmi(num, MOD - 2);
if(fa[xx] == yy) {
p[yy] = p[yy] * p[xx] % MOD * p[yy] % MOD;
p[yy] = p[yy] * pp % MOD;
ans = ans * p[yy] % MOD;
}else{
p[xx] = p[xx] * p[xx] % MOD * p[yy] % MOD;
p[xx] = p[xx] * pp % MOD;
ans = ans * p[xx] % MOD;
}
add(x, y);
}else{
ans = 0;
break;
}
}
cout << ans <<"\n";
}
signed main(){
// IOS;
int T = 1;
// cin >> T;
while (T--)
Solve();
return 0;
}
inline int Read(){
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13944kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 75ms
memory: 25924kb
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'