QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#547291#8078. Spanning TreepotentialWA 103ms23940kbC++202.6kb2024-09-04 20:14:352024-09-04 20:14:36

Judging History

你现在查看的是最新测评结果

  • [2024-09-04 20:14:36]
  • 评测
  • 测评结果:WA
  • 用时:103ms
  • 内存:23940kb
  • [2024-09-04 20:14:35]
  • 提交

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(find(fa[xx]) == yy || find(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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 13844kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Wrong Answer
time: 103ms
memory: 23940kb

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:

207904134

result:

wrong answer 1st lines differ - expected: '330864231', found: '207904134'