QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577677#8078. Spanning TreeOranger_WA 47ms26040kbC++232.1kb2024-09-20 14:00:122024-09-20 14:00:13

Judging History

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

  • [2024-09-20 14:00:13]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:26040kb
  • [2024-09-20 14:00:12]
  • 提交

answer

#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>

#define x first
#define y second

#define int long long

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const LL mod = 998244353;
const ULL P = 13331;
const int N = 1000010;

int p[N], dep[N], fa[N], sz[N];
PII Q[N];
vector<int> G[N];

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

int qmi(int a, int b)
{
    int res = 1;
    while (b)
    {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void dfs(int u, int father, int depth)
{
    dep[u] = depth, fa[u] = father;
    for (auto j : G[u])
    {
        if (j == father) continue;
        dfs(j, u, depth + 1);
    }
}

void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++) 
    {
        p[i] = i;
        G[i].clear();
        sz[i] = 1;
    }
    
    for (int i = 0; i < n - 1; i ++)
        cin >> Q[i].x >> Q[i].y;
    
    for (int i = 0; i < n - 1; i ++)
    {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b), G[b].push_back(a);
    }
    dfs(1, 0, 1);
    
    int res = 1;
    for (int i = 0; i < n - 1; i ++)
    {
        int l = Q[i].x, r = Q[i].y;
        int pl = find(p[l]), pr = find(p[r]);
        if (dep[pl] < dep[pr]) swap(pl, pr); // 保证pr在上方
        
        
        int con = find(fa[pl]);
        if (con != pr)
        {
            cout << 0 << endl;
            return ;
        }
        //printf ("sz[%lld] = %lld sz[%lld] = %lld\n", pl, sz[pl], pr, sz[pr]);
        res *= qmi(sz[pl] * sz[pr], mod - 2);
        
        p[pl] = pr;
        sz[pr] += sz[pl];
    }
    cout << res << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T --) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Wrong Answer
time: 47ms
memory: 26040kb

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'