QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#547287 | #8078. Spanning Tree | potential | WA | 85ms | 25872kb | C++20 | 2.6kb | 2024-09-04 20:11:36 | 2024-09-04 20:11:36 |
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 * pp % MOD;
ans = ans * p[yy] % MOD;
}else{
p[xx] = p[xx] * pp % MOD * p[yy] % 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;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 13868kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 85ms
memory: 25872kb
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'