QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569240#8078. Spanning TreelongyinML 82ms257784kbC++203.0kb2024-09-16 21:27:162024-09-16 21:27:16

Judging History

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

  • [2024-09-16 21:27:16]
  • 评测
  • 测评结果:ML
  • 用时:82ms
  • 内存:257784kb
  • [2024-09-16 21:27:16]
  • 提交

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);
}

vector<int> edges[N];
pair<int, int> query[N];

struct DSU {
    int fa[N];
    unordered_set<int> con[N];
    unordered_set<int> nd[N];

    DSU(int n = N - 1) {
        for (int i = 1; i <= n; i++) {
            fa[i] = i;
            con[i].emplace(i);
        }
    }

    void init(int n = N - 1) {
        for (int i = 1; i <= n; i++) {
            for (int j : edges[i]) {
                nd[i].emplace(j);
            }
        }
    }

    int find(int x) {
        return fa[x] == x ? x : find(fa[x]);
    }

    void merge(int u, int v) {
        u = find(u), v = find(v);
        if (con[u].size() > con[v].size()) {
            for (int x : con[v]) {
                con[u].insert(x);
            }
            for (int x : nd[v]) {
                nd[u].insert(x);
            }
            con[v].clear();
            nd[v].clear();
            fa[v] = u;
        }
        else {
            for (int x : con[u]) {
                con[v].insert(x);
            }
            for (int x : nd[u]) {
                nd[v].insert(x);
            }
            con[u].clear();
            nd[u].clear();
            fa[u] = 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_back(v);
        edges[v].emplace_back(u);
    }
    dsu.init(n);

    ll ans = 1;
    for (int i = 1; i <= n - 1; i++) {
        auto [u, v] = query[i];
        int fu = dsu.find(u);
        int fv = dsu.find(v);
        if (dsu.nd[fu].size() > dsu.nd[fv].size()) {
            for (int x : dsu.nd[fv]) {
                if (dsu.con[fv].count(x))
                    continue;
                if (!dsu.con[fu].count(x))
                    ans = 0;
                else 
                    ans = ans * inv(dsu.con[fu].size()) % MOD * inv(dsu.con[fv].size()) % MOD;
            }
        }
        else {
            for (int x : dsu.nd[fu]) {
                if (dsu.con[fu].count(x))
                    continue;
                if (!dsu.con[fv].count(x))
                    ans = 0;
                else 
                    ans = ans * inv(dsu.con[fu].size()) % MOD * inv(dsu.con[fv].size()) % MOD;
            }
        }
        dsu.merge(u, v);
    }
    cout << ans << endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 82ms
memory: 257784kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Memory Limit Exceeded

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: