QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71627#3000. 希望He_Ren64 699ms75180kbC++236.1kb2023-01-11 16:32:032023-01-11 16:33:32

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-11 16:33:32]
  • 评测
  • 测评结果:64
  • 用时:699ms
  • 内存:75180kb
  • [2023-01-11 16:32:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 5;
const int mod = 998244353;

inline void add_mod(int &a, int b) {
    a += b;

    if (a >= mod)
        a -= mod;
}

inline ll pw(ll a, ll b) {
    ll res = 1;

    while (b) {
        if (b & 1)
            res = res * a % mod;

        a = a * a % mod;
        b >>= 1;
    }

    return res;
}

struct Segment_Tree {
    int tagm[MAXN << 2], taga[MAXN << 2];
#define ls(u) ((u)<<1)
#define rs(u) ((u)<<1|1)
#define lson(u) ls(u),l,mid
#define rson(u) rs(u),mid+1,r
    inline void upd_tagm(int u, int k) {
        tagm[u] = (ll)tagm[u] * k % mod;
        taga[u] = (ll)taga[u] * k % mod;
    }
    inline void upd_taga(int u, int k) {
        add_mod(taga[u], k);
    }
    inline void push_down(int u) {
        if (tagm[u] != 1) {
            upd_tagm(ls(u), tagm[u]);
            upd_tagm(rs(u), tagm[u]);
            tagm[u] = 1;
        }

        if (taga[u]) {
            upd_taga(ls(u), taga[u]);
            upd_taga(rs(u), taga[u]);
            taga[u] = 0;
        }
    }
    void update_mul(int u, int l, int r, int ql, int qr, int k) {
        if (ql <= l && r <= qr) {
            upd_tagm(u, k);
            return;
        }

        push_down(u);
        int mid = (l + r) >> 1;

        if (ql <= mid)
            update_mul(lson(u), ql, qr, k);

        if (mid < qr)
            update_mul(rson(u), ql, qr, k);
    }
    void update_add(int u, int l, int r, int ql, int qr, int k) {
        if (ql <= l && r <= qr) {
            upd_taga(u, k);
            return;
        }

        push_down(u);
        int mid = (l + r) >> 1;

        if (ql <= mid)
            update_add(lson(u), ql, qr, k);

        if (mid < qr)
            update_add(rson(u), ql, qr, k);
    }
    int get(int u, int l, int r, int q) {
        if (l == r)
            return taga[u];

        push_down(u);
        int mid = (l + r) >> 1;
        return q <= mid ? get(lson(u), q) : get(rson(u), q);
    }

    int n;
    void update_mul(int ql, int qr, int k) {
        if (ql <= qr)
            update_mul(1, 0, n, ql, qr, k);
    }
    void update_add(int ql, int qr, int k) {
        if (ql <= qr)
            update_add(1, 0, n, ql, qr, k);
    }
    int get(int q) {
        return get(1, 0, n, q);
    }
} trU, trD;

int n, d;
vector<int> g[MAXN];

int dep[MAXN], h[MAXN], son[MAXN];
void dfs_tree(int u, int fa) {
    if (fa)
        g[u].erase(find(g[u].begin(), g[u].end(), fa));

    for (int v : g[u])
        if (v != fa) {
            dep[v] = dep[u] + 1;
            dfs_tree(v, u);

            if (son[u] == 0 || h[v] > h[son[u]])
                son[u] = v;

            h[u] = max(h[u], h[v] + 1);
        }
}

int dfn[MAXN], seq[MAXN], top[MAXN], curdfn = 0;
void dfs_dfn(int u, int tp) {
    top[u] = tp;
    dfn[u] = ++curdfn;
    seq[curdfn] = u;

    if (son[u])
        dfs_dfn(son[u], tp);

    for (int v : g[u])
        if (v != son[u])
            dfs_dfn(v, v);
}

int f1[MAXN], f2[MAXN], f3[MAXN];

void merge_trU(int u, int type) {
    if (type == 1) {
        trU.update_add(dfn[u], dfn[u] + h[u], 1);

        for (int v : g[u])
            if (v != son[u]) {
                for (int i = 0; i <= h[v]; ++i)
                    trU.update_mul(dfn[u] + i + 1, dfn[u] + i + 1, (trU.get(dfn[v] + i) + 1) % mod);

                trU.update_mul(dfn[u] + h[v] + 2, dfn[u] + h[u], (trU.get(dfn[v] + h[v]) + 1) % mod);
            }
    } else {
        for (int v : g[u])
            if (v != son[u]) {
                for (int i = 0; i <= h[v]; ++i)
                    trU.update_mul(dfn[u] + i + 1, dfn[u] + i + 1, pw((trU.get(dfn[v] + i) + 1) % mod, mod - 2));

                trU.update_mul(dfn[u] + h[v] + 2, dfn[u] + h[u], pw((trU.get(dfn[v] + h[v]) + 1) % mod, mod - 2));
            }

        trU.update_add(dfn[u], dfn[u] + h[u], mod - 1);
    }
}

void dfs_trU(int u) {
    for (int v : g[u])
        dfs_trU(v);

    merge_trU(u, 1);

    f1[u] = trU.get(dfn[u] + min(h[u], d));
    f2[u] = d != 0 ? trU.get(dfn[u] + min(h[u], d - 1)) : 0;
}

void dfs_trD(int u) {
    merge_trU(u, -1);

    int secmx = 0;

    for (int v : g[u])
        if (v != son[u])
            secmx = max(secmx, h[v] + 1);

    f3[u] = trD.get(dep[u]);

    auto upd = [&](int v, int type) {
        int cur = v == son[u] ? secmx : h[u];
        int l = d - 1 - cur, r = d - 2;
        l = max(l, 0);

        if (l > r)
            return;

        for (int i = l; i <= r && i <= h[v]; ++i) {
            int pos = dep[u] + (d - 1 - i);
            int k = (trU.get(dfn[v] + i) + 1) % mod;
            trD.update_mul(pos, pos, type == 1 ? k : pw(k, mod - 2));
        }

        if (h[v] + 1 <= r) {
            int pos = dep[u] + (d - 1 - h[v] - 1);
            int k = (trU.get(dfn[v] + h[v]) + 1) % mod;
            trD.update_mul(dep[u] + 1, pos, type == 1 ? k : pw(k, mod - 2));
        }
    };

    trD.update_add(dep[u] + 1, dep[u] + min(h[u], d), 1);

    for (int v : g[u])
        upd(v, 1);

    for (int v : g[u]) {
        upd(v, -1);
        dfs_trD(v);
        upd(v, 1);
    }

    for (int v : g[u])
        upd(v, -1);

    trD.update_add(dep[u] + 1, dep[u] + h[u], mod - 1);

    merge_trU(u, 1);
}

int main(void) {
    int m;
    scanf("%d%d%d", &n, &d, &m);

    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs_tree(1, 0);
    dfs_dfn(1, 1);

    trU.n = trD.n = n;
    dfs_trU(1);
    dfs_trD(1);

    // for(int u=1; u<=n; ++u)
    // {
    //  printf("f[%d] = {%d, %d, %d}\n",u,f1[u],f2[u],f3[u]);
    // }

    ll ans1 = 0, ans2 = 0;

    for (int i = 1; i <= n; ++i) {
        ans1 = (ans1 + pw((ll)f1[i] * (f3[i] + 1) % mod, m)) % mod;
        ans2 = (ans2 + pw((ll)f2[i] * f3[i] % mod, m)) % mod;
    }

    ll ans = (ans1 - ans2 + mod) % mod;

    printf("%lld\n", ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 3ms
memory: 38468kb

input:

16 3 1
15 13
7 5
7 3
13 4
13 11
16 9
13 9
11 2
1 6
14 13
11 12
8 9
7 14
10 6
6 16

output:

1243

result:

ok single line: '1243'

Test #2:

score: 4
Accepted
time: 8ms
memory: 40720kb

input:

10 2 2
6 4
7 3
6 5
8 4
9 8
1 7
2 7
6 7
10 4

output:

9430

result:

ok single line: '9430'

Test #3:

score: 4
Accepted
time: 4ms
memory: 40688kb

input:

10 10 9
2 4
1 8
5 8
7 8
10 9
8 2
1 10
8 3
5 6

output:

287143445

result:

ok single line: '287143445'

Test #4:

score: 4
Accepted
time: 9ms
memory: 40688kb

input:

10 2 10
9 5
4 7
10 4
6 8
3 4
4 6
4 2
5 6
1 10

output:

736013382

result:

ok single line: '736013382'

Test #5:

score: 4
Accepted
time: 1ms
memory: 38524kb

input:

497 497 9
421 44
120 434
367 497
332 122
115 101
5 348
14 144
210 359
141 242
439 276
215 289
303 375
28 77
143 428
151 127
457 288
62 351
421 187
25 261
347 257
344 339
452 474
216 387
144 24
167 148
135 133
178 331
274 50
339 369
60 211
350 292
115 76
393 473
475 387
294 332
43 91
368 81
481 27
95...

output:

331917336

result:

ok single line: '331917336'

Test #6:

score: 4
Accepted
time: 4ms
memory: 40596kb

input:

1000 100 1
382 224
432 411
634 456
768 851
345 832
659 985
177 604
834 849
397 593
156 24
43 819
438 78
188 80
936 395
91 529
959 911
104 355
503 618
164 972
505 652
746 845
907 301
662 531
486 660
360 724
340 112
155 499
308 974
123 97
829 600
709 781
415 678
874 272
675 879
583 881
968 414
769 359...

output:

836607737

result:

ok single line: '836607737'

Test #7:

score: 4
Accepted
time: 133ms
memory: 50644kb

input:

49999 29 10
16726 12082
6732 37340
40485 10847
44869 24166
36684 39431
3249 27243
45483 37633
19260 22383
19143 13528
6706 7421
11839 47225
46922 11790
47525 3196
43850 42710
4893 2839
37556 41101
5873 45077
2696 21251
39836 36172
42780 38464
41481 9865
33864 24756
3474 13763
31198 28752
46506 3786
...

output:

562505285

result:

ok single line: '562505285'

Test #8:

score: 0
Wrong Answer
time: 262ms
memory: 55648kb

input:

99990 95 1
73567 71079
3326 88384
36374 90162
37653 80637
66761 72061
61493 34910
70782 93885
15325 87771
81082 95813
1016 32294
3583 89414
63054 88734
902 70455
89078 71307
5572 73684
90068 2718
7522 87784
1880 77032
9206 13625
40074 66039
84079 94646
7226 26891
10757 47921
52558 82703
77478 24017
...

output:

367318978

result:

wrong answer 1st lines differ - expected: '625332568', found: '367318978'

Test #9:

score: 0
Wrong Answer
time: 277ms
memory: 57812kb

input:

100000 100 9
71142 34813
51716 56682
16198 52726
21972 98808
8782 18661
64438 93384
62968 16698
66396 20240
57089 24568
53302 66919
46404 72330
31924 16432
72942 87322
42977 1430
31924 75941
59006 24077
93987 11904
11298 45520
31924 66491
52351 81769
98877 37816
48438 29764
10467 60332
46402 82645
3...

output:

658955924

result:

wrong answer 1st lines differ - expected: '854741172', found: '658955924'

Test #10:

score: 0
Time Limit Exceeded

input:

999992 282751 9
539736 453462
540111 477459
971608 731703
333352 646885
474338 449903
682998 756565
514706 652705
319529 600819
450009 862650
270005 950006
215331 949892
900851 316199
20848 879879
601074 688617
353471 60779
988869 786268
982113 354443
464498 645452
282384 979334
404667 275071
465382...

output:


result:


Test #11:

score: 4
Accepted
time: 69ms
memory: 44212kb

input:

29997 29997 1
17887 27517
28638 28750
17146 20323
2484 14534
14496 9485
28598 1702
16812 20561
12792 10249
21147 12075
26413 21147
23535 12297
12912 2957
16747 4815
3695 4722
15400 11359
12046 1690
23085 4025
15400 29146
11317 21167
21147 27124
26258 22579
27050 28822
21147 12210
23688 24822
16221 1...

output:

208568713

result:

ok single line: '208568713'

Test #12:

score: 4
Accepted
time: 113ms
memory: 43268kb

input:

39999 9999 1
17847 8767
34288 3223
31953 15747
19891 38574
22166 12007
22545 10176
15852 4478
25563 38574
25054 22861
19512 38574
25737 7144
4838 36701
36463 6714
23038 3579
18929 21494
14933 19526
1630 30203
28237 23398
4178 14268
16048 18342
9864 7531
22278 29214
14797 32203
20 13687
3267 27370
29...

output:

622134586

result:

ok single line: '622134586'

Test #13:

score: 0
Wrong Answer
time: 152ms
memory: 46024kb

input:

49937 9618 9
28900 21057
23426 3158
11352 23430
22070 36326
3612 43977
6755 2034
33497 5866
26127 35204
45896 2194
2698 6508
47136 7404
25263 35056
14916 24075
25469 5476
2913 24445
38256 32997
2634 14195
26348 45158
39795 30522
32668 23847
48619 32881
11604 33190
32221 44730
39330 46327
37184 15880...

output:

79249244

result:

wrong answer 1st lines differ - expected: '808296259', found: '79249244'

Test #14:

score: 4
Accepted
time: 283ms
memory: 58960kb

input:

99997 24814 1
70294 6008
96698 51718
85730 12149
7399 68219
19240 98520
6806 60763
60944 9942
95042 84051
52276 11791
92228 26864
31607 4577
50572 55556
91020 39530
99630 58302
46987 75388
13481 8988
73782 79423
13592 71106
68888 24123
69314 52558
1827 1930
2403 39363
71227 97979
62460 62818
93260 8...

output:

889032739

result:

ok single line: '889032739'

Test #15:

score: 4
Accepted
time: 499ms
memory: 58464kb

input:

149934 23430 1
19524 118124
102651 94642
105977 88780
22851 21085
118124 26220
51965 41865
94877 88657
45954 42676
15024 104515
88216 18289
71244 65571
85897 126980
121323 96801
149044 144348
118914 98362
41777 84779
95166 106140
50549 5071
69568 144894
133722 26506
32976 45985
139222 118124
57802 8...

output:

967739919

result:

ok single line: '967739919'

Test #16:

score: 4
Accepted
time: 565ms
memory: 73720kb

input:

199993 199993 10
22574 120810
132171 199046
85192 167635
166147 64640
92778 56014
52056 77741
141487 104029
192429 193787
24289 66737
56456 33117
12406 121250
139510 72340
37754 175679
27730 106773
34512 45901
169159 145205
167199 9455
124988 27796
183030 167303
176967 58418
116279 112701
122931 786...

output:

210281061

result:

ok single line: '210281061'

Test #17:

score: 4
Accepted
time: 565ms
memory: 67052kb

input:

199995 199995 1
74797 7556
81442 135821
185092 167712
42234 63649
134023 46082
134318 22976
128405 107157
36520 80426
167712 198927
132017 118773
288 1341
132649 22081
174449 167712
94580 179706
151008 160583
48868 128462
4161 125098
142488 88139
167712 47267
153225 129410
167712 100469
16050 89116
...

output:

241088863

result:

ok single line: '241088863'

Test #18:

score: 4
Accepted
time: 699ms
memory: 75180kb

input:

199996 25550 1
153865 144748
17792 151663
128934 59764
43644 133644
130769 176056
95288 123429
93190 140114
194626 118534
30624 86065
30624 103291
63137 117845
45411 57110
93790 4225
44261 92925
163904 119841
194626 20904
26921 194626
191171 32365
50763 194808
133250 162111
174639 5106
39176 139051
...

output:

346947170

result:

ok single line: '346947170'

Test #19:

score: 4
Accepted
time: 643ms
memory: 72772kb

input:

200000 24970 9
177589 184389
22449 43171
11368 15434
177589 124467
34715 172282
160344 28685
138184 4137
18031 177589
123360 2900
167503 74776
150552 180476
177589 198321
191168 145709
26761 40657
76252 155219
199611 140454
80426 54961
64064 134456
48347 114493
190498 177589
177589 2360
154689 13729...

output:

659235481

result:

ok single line: '659235481'

Test #20:

score: 4
Accepted
time: 682ms
memory: 70068kb

input:

200000 24973 9
118915 67700
120517 143573
66710 142095
140916 9440
144299 173165
179864 150849
108850 86634
183838 27249
56514 33874
45014 108850
185494 52388
160940 76515
27076 33073
194583 114506
127559 66004
44294 183831
7460 108850
198987 150508
45758 70252
59682 157110
49593 150673
67251 108850...

output:

371741367

result:

ok single line: '371741367'

Test #21:

score: 0
Wrong Answer
time: 655ms
memory: 62020kb

input:

199999 15797 9
111155 118437
116181 103286
171596 117870
169742 58066
37077 12461
115452 191571
126892 134514
115497 151658
189077 76880
28368 17080
186378 14214
62745 18534
142023 76880
150833 175023
26865 15461
49984 107836
92202 136608
184737 112189
86026 148269
76880 147485
101065 111876
39958 1...

output:

861413271

result:

wrong answer 1st lines differ - expected: '923057136', found: '861413271'

Test #22:

score: 0
Time Limit Exceeded

input:

999996 144346 1
424407 435560
510418 674573
150671 452726
128147 920948
510027 239220
972006 448279
281095 505374
655082 730449
15872 689268
70185 87349
741472 521817
723804 255286
811374 494975
443017 139873
406268 753301
20508 211500
191993 316128
730092 917846
868622 770478
738161 787331
87502 24...

output:


result:


Test #23:

score: 0
Time Limit Exceeded

input:

999997 139853 10
920585 690093
197610 611492
23489 349039
700388 280314
98990 218300
673469 707504
635638 180992
710821 330460
525503 990906
707504 708854
273138 955457
931868 88040
878331 611354
968123 352800
971264 681683
844600 397899
843885 192964
615112 555275
242987 872652
20593 782797
330349 ...

output:


result:


Test #24:

score: 0
Time Limit Exceeded

input:

999955 103989 10
925289 178114
308672 659094
669662 430933
561446 607489
799171 728376
452539 684799
9192 983985
974919 329141
696226 20531
172583 956538
197830 414632
820211 684799
180437 882577
684799 945146
18047 421707
266023 709380
377290 420390
920102 395704
702819 208991
700053 332653
479543 ...

output:


result:


Test #25:

score: 0
Time Limit Exceeded

input:

999936 123275 9
536599 787920
7994 306084
494368 947491
507729 297675
824018 568122
28934 711706
235386 798753
343255 798753
953136 324491
67553 200106
466356 485763
569760 432140
864787 466356
236533 560892
469895 353561
686792 47275
575895 202643
798753 887888
735885 356054
335977 648100
900249 62...

output:


result: