QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577680#8078. Spanning TreeOranger_WA 756ms116200kbC++232.1kb2024-09-20 14:00:422024-09-20 14:00:43

Judging History

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

  • [2024-09-20 14:00:43]
  • 评测
  • 测评结果:WA
  • 用时:756ms
  • 内存:116200kb
  • [2024-09-20 14:00:42]
  • 提交

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 = (res * qmi(sz[pl] * sz[pr], mod - 2)) % mod;
        
        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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 13856kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: 0
Accepted
time: 65ms
memory: 26108kb

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:

330864231

result:

ok single line: '330864231'

Test #3:

score: 0
Accepted
time: 68ms
memory: 25968kb

input:

100000
2431 82555
18148 99107
3941 5966
1071 47115
15967 53202
27778 80193
17295 91006
21981 69106
8487 92235
7229 17953
6531 81170
18678 84896
25646 98753
3087 34488
4008 80883
18556 30802
250 3839
10378 92520
27250 87378
18484 75316
12912 82770
6142 78903
18637 58366
5716 82933
25060 76764
7868 78...

output:

207623640

result:

ok single line: '207623640'

Test #4:

score: 0
Accepted
time: 60ms
memory: 28096kb

input:

100000
3677 53591
26136 80586
14460 75864
5626 70315
2145 17733
681 92982
19076 64996
1672 82873
7909 80493
1362 87683
3121 81083
1881 92956
19448 31658
26638 78109
17888 74591
31114 97492
30263 93965
15990 51796
25387 47368
26827 50839
21100 85993
8497 95004
3017 3343
12299 97797
16723 59533
19266 ...

output:

315464493

result:

ok single line: '315464493'

Test #5:

score: 0
Accepted
time: 57ms
memory: 26064kb

input:

100000
337 80784
277 63525
309 68073
50 65780
223 85983
229 99952
419 97414
210 73844
149 98533
19 92591
254 75626
413 28608
31 13397
355 9807
248 86299
177 92507
75 77718
238 94072
440 73716
34 33639
39 92470
177 76740
210 93812
460 86098
79 38556
500 4414
306 93377
341 46557
260 18020
170 70281
99...

output:

782103077

result:

ok single line: '782103077'

Test #6:

score: 0
Accepted
time: 55ms
memory: 26056kb

input:

100000
77 61768
159 75045
189 96344
27 90011
80 70404
100 10876
196 6710
83 69730
122 61478
95 34246
121 66123
76 91590
183 84813
30 22860
21 64208
197 52093
136 95550
104 74693
141 43684
76 24573
180 96323
27 63135
78 77847
110 49459
67 89691
82 98960
53 62386
94 95145
15 72896
28 92579
145 94344
9...

output:

574681591

result:

ok single line: '574681591'

Test #7:

score: 0
Accepted
time: 58ms
memory: 26192kb

input:

100000
7 93472
18 98769
12 97619
17 73647
10 16818
1 91671
7 87544
6 72485
3 92155
8 72652
13 69083
18 95717
19 96402
6 18778
9 99294
18 90017
17 79729
14 97967
11 92694
8 63979
10 97859
7 44859
2 85081
4 58827
18 98750
6 75751
11 94889
4 22678
8 84842
15 84960
5 76729
2 90310
19 55608
12 87558
16 9...

output:

45611347

result:

ok single line: '45611347'

Test #8:

score: 0
Accepted
time: 53ms
memory: 26280kb

input:

100000
7 58890
3 58280
7 27055
7 93537
1 91565
5 88595
4 84795
3 87969
5 98167
3 89215
8 68189
1 9565
2 75124
5 57314
2 80828
6 69452
7 94539
5 95670
2 69584
7 58461
2 88826
4 60983
4 64741
5 94797
5 85145
3 63668
3 29256
2 80180
3 1473
7 34495
3 25487
6 49093
4 92049
6 32240
6 60954
1 23309
2 7334
...

output:

359179257

result:

ok single line: '359179257'

Test #9:

score: 0
Accepted
time: 48ms
memory: 23812kb

input:

100000
7 52065
5 79374
8 93846
5 98472
10 72503
4 54043
2 81067
5 25410
9 66642
4 96261
2 69048
5 53093
8 72619
5 25253
7 92182
10 85942
2 96401
10 56808
1 65642
4 94067
8 76278
5 92554
5 16028
8 95421
9 96769
9 38660
4 37431
6 89486
5 12160
5 96259
3 84728
1 51495
4 96438
6 49380
1 95929
1 27527
8 ...

output:

407203766

result:

ok single line: '407203766'

Test #10:

score: 0
Accepted
time: 42ms
memory: 22168kb

input:

100000
78 73288
63 78335
71 60255
14 89232
33 31513
96 31412
63 64415
87 60736
56 60604
9 91650
29 86482
65 90995
94 91106
15 93202
56 15471
69 40408
83 88007
42 94851
33 68754
81 58316
87 69603
1 17260
95 78585
8 39063
58 31243
20 13440
94 53628
17 88933
58 98863
65 93116
22 82543
28 86883
27 59282...

output:

517010519

result:

ok single line: '517010519'

Test #11:

score: 0
Accepted
time: 44ms
memory: 24384kb

input:

100000
442 69813
189 43985
356 86661
74 29978
381 51503
808 84079
837 91324
12 91061
238 90167
910 98749
28 45930
834 97170
636 56426
592 99796
752 67695
489 66396
688 49989
151 94252
280 5651
363 78766
223 93772
937 84602
336 78368
264 74532
701 54957
277 78254
891 97873
637 22363
582 43788
887 873...

output:

968562725

result:

ok single line: '968562725'

Test #12:

score: 0
Accepted
time: 52ms
memory: 21932kb

input:

100000
31518 99113
23679 31891
224 4623
1819 48802
11333 71708
11245 92859
8309 75148
10033 89063
297 93596
18282 88408
16020 98356
27633 30998
15471 53295
8023 78199
25209 44744
27250 66553
27805 70208
31835 62514
8589 42520
10650 99871
2095 21829
21341 40322
4584 98667
25361 87279
25248 30530
2625...

output:

0

result:

ok single line: '0'

Test #13:

score: 0
Accepted
time: 49ms
memory: 26052kb

input:

100000
29557 93649
10753 56671
24466 72701
1384 99687
6546 20050
17335 97139
602 85076
27423 94808
6671 69540
9012 24231
22086 89387
3911 53451
1125 24156
15603 93219
21598 95879
29120 77832
1122 77121
30596 78906
3562 20826
9711 58720
26577 93105
1239 74813
22634 90533
16580 70202
9151 91423
9138 3...

output:

0

result:

ok single line: '0'

Test #14:

score: 0
Accepted
time: 42ms
memory: 26024kb

input:

100000
11052 93507
32763 42377
3539 95722
28478 42428
28822 73108
17800 32034
21291 54942
32182 98922
28240 87649
27633 91285
29059 51198
2192 5282
29820 87910
12552 94901
32374 98661
6618 61155
16911 39354
17408 83301
13524 84942
16147 92960
16986 82804
2008 98635
16746 30250
25311 90367
27942 6702...

output:

0

result:

ok single line: '0'

Test #15:

score: 0
Accepted
time: 23ms
memory: 24324kb

input:

100000
21541 68499
23622 95286
19569 92366
17503 86493
1973 88783
26799 91342
13858 84932
2055 99415
11930 40465
8120 94109
19501 71619
8091 18971
19457 64055
12649 71789
16657 89312
29364 94208
10524 90079
4147 97402
6424 93538
22519 57930
19027 74021
555 99344
19677 85788
30071 45237
12313 51129
2...

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 649ms
memory: 116200kb

input:

1000000
6469 999561
13878 967683
21843 921179
25097 928554
25703 987943
14790 975820
8363 975184
19685 962213
22207 995737
13821 985366
6646 992255
32437 895192
16156 984517
13685 875123
3856 988805
16070 969018
32049 962285
23741 935777
23886 949268
11506 993212
25067 999053
30691 970950
15900 9574...

output:

464421553

result:

ok single line: '464421553'

Test #17:

score: -100
Wrong Answer
time: 756ms
memory: 115952kb

input:

1000000
24460 991332
13731 995432
19665 962294
11791 931863
16817 983768
2476 965018
21135 986410
10868 966675
17874 902265
3570 991677
30055 996521
15049 999752
3413 978934
30326 998661
12433 933275
28362 989240
20699 982498
31977 943563
17769 954244
20208 847131
17105 987971
20504 932740
4492 9736...

output:

974604328

result:

wrong answer 1st lines differ - expected: '350281136', found: '974604328'