QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#227454#5487. Movie NightgondozuAC ✓34ms19320kbC++142.4kb2023-10-27 16:09:072023-10-27 16:09:08

Judging History

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

  • [2023-10-27 16:09:08]
  • 评测
  • 测评结果:AC
  • 用时:34ms
  • 内存:19320kb
  • [2023-10-27 16:09:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+1;
const int MOD = 1e9+7;

ll add(ll a, ll b) {
    a += b;
    if (a >= MOD) a -= MOD;
    return a;
}
ll sub(ll a, ll b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}
ll mul(ll a, ll b) { return a * b % MOD; }

vector<int> adj[N], adj_inv[N];
bool inCycle[N], vis[N];
int inDegree[N];

/**
 * calculates the number of ways to pick the subtree of v
 * @param v the subtree root
 * @return the number of ways to pick the subtree of v
 */
ll calc(int v){
    ll ways = 1;
    for(int u : adj_inv[v]){
        if(inCycle[u])
            continue;
        // pick or leave each child's subtree
        ways = mul(ways, add(calc(u), 1));
    }
    return ways;
}

int main() {
    int n;
    cin >> n;

    // each person has exactly one partner
    for (int i = 1; i <= n; ++i) {
        int partner;
        cin >> partner;
        adj[i].push_back(partner);
        adj_inv[partner].push_back(i);
        inDegree[partner]++;

        // assume all nodes inCycle, then exclude later.
        inCycle[i] = true;
    }

    // topological sort to get the inCycle nodes
    vector<int> free;
    for (int i = 1; i <= n; ++i) {
        if(inDegree[i] == 0)
            free.push_back(i);
    }

    while (!free.empty()){
        int node = free.back();
        free.pop_back();

        inCycle[node] = false;
        for(int u : adj[node]){
            inDegree[u]--;
            if(inDegree[u] == 0)
                free.push_back(u);
        }
    }

    vector<vector<int>> allCycles;
    // get a cycle
    vector<int> cycle;
    for (int i = 1; i <= n; ++i) {
        if(!inCycle[i] || vis[i])
            continue;
        cycle.clear();
        int v = i;
        do {
            cycle.push_back(v);
            vis[v] = true;
            v = adj[v][0];
        } while (v != i);
        allCycles.push_back(cycle);
    }

    // calculate number of ways to pick a subset of friends with their partners
    ll ans = 1;
    for(auto& cyc : allCycles){
        ll curWays = 1;
        for(int v : cyc)
            curWays = mul(curWays, calc(v));
        // pick or leave the cycle
        ans = mul(ans, add(curWays, 1));
    }
    // exclude the empty subset
    ans = sub(ans, 1);

    cout << ans;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
2
3
4
3

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 3ms
memory: 13192kb

input:

5
2
3
1
5
4

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 0ms
memory: 13580kb

input:

6
2
4
2
6
1
3

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 14116kb

input:

8
2
3
4
1
3
3
1
4

output:

16

result:

ok single line: '16'

Test #5:

score: 0
Accepted
time: 2ms
memory: 13540kb

input:

4
3
3
4
3

output:

4

result:

ok single line: '4'

Test #6:

score: 0
Accepted
time: 2ms
memory: 13808kb

input:

8
8
6
8
1
3
3
3
6

output:

24

result:

ok single line: '24'

Test #7:

score: 0
Accepted
time: 0ms
memory: 13824kb

input:

8
6
6
6
1
8
7
1
7

output:

24

result:

ok single line: '24'

Test #8:

score: 0
Accepted
time: 0ms
memory: 13616kb

input:

7
2
3
1
5
4
7
6

output:

7

result:

ok single line: '7'

Test #9:

score: 0
Accepted
time: 0ms
memory: 13712kb

input:

11
2
5
2
2
6
7
5
5
10
11
10

output:

56

result:

ok single line: '56'

Test #10:

score: 0
Accepted
time: 2ms
memory: 12988kb

input:

30
11
27
25
25
18
18
4
9
27
30
7
16
22
22
28
11
4
30
6
7
13
9
29
26
16
29
28
13
6
26

output:

35936

result:

ok single line: '35936'

Test #11:

score: 0
Accepted
time: 2ms
memory: 13008kb

input:

192
2
3
2
5
6
5
8
9
8
11
12
11
14
15
14
17
18
17
20
21
20
23
24
23
26
27
26
29
30
29
32
33
32
35
36
35
38
39
38
41
42
41
44
45
44
47
48
47
50
51
50
53
54
53
56
57
56
59
60
59
62
63
62
65
66
65
68
69
68
71
72
71
74
75
74
77
78
77
80
81
80
83
84
83
86
87
86
89
90
89
92
93
92
95
96
95
98
99
98
101
102
...

output:

767713260

result:

ok single line: '767713260'

Test #12:

score: 0
Accepted
time: 34ms
memory: 19320kb

input:

100000
37545
63739
77335
76669
41730
7658
16542
96644
93229
26821
44918
85782
59893
20668
23347
7485
66696
52156
83919
58211
21501
74582
15535
95723
93846
56664
45820
76652
119
87653
94275
7476
10922
22482
97813
44052
1939
82364
97592
50820
14029
72907
60215
75940
69655
61420
91810
80030
20680
10647...

output:

547375115

result:

ok single line: '547375115'

Test #13:

score: 0
Accepted
time: 11ms
memory: 17704kb

input:

50000
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
...

output:

49999

result:

ok single line: '49999'

Test #14:

score: 0
Accepted
time: 14ms
memory: 18416kb

input:

50000
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...

output:

49999

result:

ok single line: '49999'

Test #15:

score: 0
Accepted
time: 29ms
memory: 18916kb

input:

100000
57960
17255
59039
48160
38428
5639
81797
7727
18708
13985
41346
12136
56190
53735
1276
36510
37480
62557
86522
26392
35428
31380
29697
50833
34282
87940
30357
10461
89546
67687
4208
86692
76322
93916
43063
66954
33774
17718
82112
77402
83276
8368
46588
94129
55329
29682
45681
48861
96804
7131...

output:

432418538

result:

ok single line: '432418538'

Test #16:

score: 0
Accepted
time: 0ms
memory: 12996kb

input:

61
2
1
1
3
3
5
6
6
8
9
9
9
9
9
9
9
9
17
18
18
18
21
22
22
22
22
26
27
27
29
30
30
30
33
34
34
34
37
38
38
40
41
41
41
41
45
46
46
48
49
49
51
52
52
52
55
56
56
58
59
59

output:

1000000006

result:

ok single line: '1000000006'

Test #17:

score: 0
Accepted
time: 0ms
memory: 14088kb

input:

30
12
4
12
5
21
10
5
18
22
23
1
10
4
27
25
20
21
2
29
22
18
29
25
2
1
14
20
27
14
23

output:

35936

result:

ok single line: '35936'

Test #18:

score: 0
Accepted
time: 2ms
memory: 13620kb

input:

30
21
24
23
29
10
24
23
13
28
25
25
29
26
6
16
28
15
3
16
12
6
13
22
12
15
3
22
10
21
26

output:

35936

result:

ok single line: '35936'

Test #19:

score: 0
Accepted
time: 30ms
memory: 18748kb

input:

100000
72618
91180
3790
30618
34208
43583
94119
79711
48308
36108
85044
51194
74204
18991
31877
92374
73453
12032
26681
7580
75575
82736
34435
59741
59569
99610
7494
44013
66363
13389
7466
64392
23606
69281
20480
97960
66000
85199
80899
68005
80090
64840
27829
51429
55054
41363
28605
49684
88440
738...

output:

547375115

result:

ok single line: '547375115'

Test #20:

score: 0
Accepted
time: 29ms
memory: 18740kb

input:

100000
88014
10135
94637
65906
91613
99597
45265
8537
62533
59138
80592
50465
52337
92026
82938
43125
24906
10617
90253
58778
72035
36224
494
65096
19587
68722
52748
92097
65962
15249
22890
38496
15079
18595
53512
43467
30824
29415
19050
6012
54915
57203
19051
41770
33428
5869
60324
72368
10390
7780...

output:

547375115

result:

ok single line: '547375115'

Test #21:

score: 0
Accepted
time: 2ms
memory: 12932kb

input:

500
325
490
281
458
157
376
100
325
30
66
389
355
348
201
49
466
252
56
76
384
83
346
394
360
309
410
414
482
39
131
456
296
71
72
118
30
129
404
267
173
84
257
35
285
49
98
179
37
425
356
274
160
369
330
373
235
71
157
94
472
6
220
53
308
49
222
121
4
322
386
451
485
215
444
299
352
256
125
268
470...

output:

391188899

result:

ok single line: '391188899'

Test #22:

score: 0
Accepted
time: 2ms
memory: 13496kb

input:

1000
480
179
215
115
370
484
3
677
437
59
910
819
807
305
630
98
992
78
225
778
158
239
722
126
948
554
246
25
640
768
668
931
694
129
636
811
806
890
390
114
535
951
166
259
41
430
59
58
653
314
272
385
890
961
344
872
508
732
651
375
481
626
558
785
857
623
278
726
770
163
248
67
159
523
624
747
2...

output:

822079567

result:

ok single line: '822079567'

Test #23:

score: 0
Accepted
time: 2ms
memory: 13020kb

input:

1000
118
384
413
740
496
830
970
6
586
351
414
394
432
511
331
136
308
322
353
764
213
27
682
105
463
752
95
996
266
385
845
511
65
92
996
143
806
272
914
7
313
572
792
57
338
670
749
976
141
319
470
517
714
498
966
656
569
215
774
725
699
834
993
100
941
241
644
416
836
189
454
636
592
409
762
532
...

output:

966469138

result:

ok single line: '966469138'

Test #24:

score: 0
Accepted
time: 3ms
memory: 13300kb

input:

2000
1896
75
429
694
792
645
1668
186
1947
685
1086
1911
1659
1130
665
133
570
1389
771
1446
1477
38
1884
1213
269
1242
476
363
549
1550
1678
1072
1870
893
1870
1763
507
796
1796
1351
500
245
1761
951
1726
1305
418
1758
1862
550
1051
1980
409
742
710
1694
246
1821
891
706
406
1229
102
1808
556
1316
...

output:

644178398

result:

ok single line: '644178398'

Test #25:

score: 0
Accepted
time: 3ms
memory: 13088kb

input:

3000
2364
1481
2590
1996
579
1727
2286
1180
58
377
1394
49
901
1173
2008
1438
1640
2845
2009
200
443
2721
965
1016
1839
1935
1463
1107
1718
1189
776
1541
1468
1523
2156
1633
2453
85
2055
1828
82
302
42
130
1861
1823
2025
1518
98
2671
1792
1978
361
2282
1505
806
483
1113
2791
497
1231
270
2291
1700
2...

output:

754652312

result:

ok single line: '754652312'

Test #26:

score: 0
Accepted
time: 0ms
memory: 13136kb

input:

4000
3636
1565
3099
2783
1766
2087
1998
1947
3298
1656
2837
1388
1236
2995
3416
2177
3018
2919
517
2465
3102
771
3804
4
3250
974
1169
245
2411
1199
3318
1971
1681
91
1241
647
3934
2127
3357
1642
161
1443
104
3109
674
3551
2978
1832
3137
2220
3103
3224
3017
1607
1996
3454
99
3221
2321
1874
1
2319
320...

output:

484968853

result:

ok single line: '484968853'

Test #27:

score: 0
Accepted
time: 0ms
memory: 14424kb

input:

5000
3160
123
625
3013
2545
4213
4220
1733
3035
2658
414
2338
2058
2125
669
2442
1222
1751
2228
1913
2938
1138
731
2450
4119
430
2025
3632
4156
2958
3078
3333
726
12
4966
4759
4038
4019
4597
4423
2037
1170
4652
3208
129
516
4873
4359
826
1468
1702
3095
4623
4720
1207
2477
2457
4865
4423
3499
3130
16...

output:

682856026

result:

ok single line: '682856026'

Test #28:

score: 0
Accepted
time: 0ms
memory: 13496kb

input:

6000
5533
2049
3299
748
5206
3836
5207
1317
2922
2135
378
5925
1046
4009
5921
5052
718
5887
1894
2174
1247
2306
3968
3997
1674
5894
2761
56
5774
3537
2244
2249
251
5217
5021
3376
2632
2472
345
5992
5728
2502
2675
5386
5003
3127
2725
694
5703
1156
2778
3376
5678
1038
2049
2483
4888
1650
1807
5587
170...

output:

617175553

result:

ok single line: '617175553'

Test #29:

score: 0
Accepted
time: 3ms
memory: 13468kb

input:

7000
5099
6898
3621
1968
2952
4791
5414
6981
2525
944
5724
5570
2045
607
436
1200
6770
4232
307
4399
4951
1497
6311
6794
3077
853
5844
3167
1218
3955
2791
3140
1289
5880
5037
6942
5282
206
5795
2845
6082
1771
470
2584
538
5167
2625
3596
792
3012
1961
892
418
2765
4541
1809
5784
2526
3781
2113
931
17...

output:

630137128

result:

ok single line: '630137128'

Test #30:

score: 0
Accepted
time: 4ms
memory: 13468kb

input:

8000
5815
5925
1603
3236
6301
5618
3707
7820
3684
3460
5139
2078
1265
4116
6540
2116
882
5858
7107
2352
6869
1698
2917
3050
632
5705
3615
516
5904
6058
1253
7691
3111
2886
6213
1481
6352
5395
488
1350
2153
5938
214
2642
4116
5233
3322
5983
7823
1480
7711
1010
5349
4081
7254
5235
1719
395
884
4717
70...

output:

210497575

result:

ok single line: '210497575'

Test #31:

score: 0
Accepted
time: 4ms
memory: 14552kb

input:

9000
1411
5606
2636
2938
3912
901
7360
8163
7962
6189
4991
8855
6701
4734
3036
5842
6724
8314
1025
5024
5278
1376
8854
7525
6711
8356
1892
6144
1555
3357
7938
3625
681
8715
405
3511
1366
5726
2333
6931
1200
4154
7830
5650
4127
1603
8089
8062
8372
3669
1556
7416
6859
7800
5961
3781
18
3946
6005
3794
...

output:

539302110

result:

ok single line: '539302110'

Test #32:

score: 0
Accepted
time: 0ms
memory: 14524kb

input:

10000
1009
8333
4871
4853
4106
6316
873
9516
7679
8600
735
140
7426
6318
3181
9075
7853
9208
9860
8017
2241
6135
8591
7405
6532
5655
7579
2683
7641
2339
7033
620
7782
1111
5542
6895
9460
3079
2743
3022
6331
2884
8661
7785
4573
816
4731
1968
8612
3203
966
3409
9852
2486
8341
6473
924
5459
9400
8887
5...

output:

781031023

result:

ok single line: '781031023'

Test #33:

score: 0
Accepted
time: 15ms
memory: 16868kb

input:

100000
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

output:

151930880

result:

ok single line: '151930880'