QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#816942#3292. Counting Cyclesship2077AC ✓83ms20944kbC++231.6kb2024-12-16 19:20:282024-12-16 19:20:28

Judging History

This is the latest submission verdict.

  • [2024-12-16 19:20:28]
  • Judged
  • Verdict: AC
  • Time: 83ms
  • Memory: 20944kb
  • [2024-12-16 19:20:28]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int M=1e5+5;
map<int,int>edge[M];
vector<pair<int,int>>adj[M];
int n,m,ans,res,lim;queue<int>q;
bool in[M],vis[M][3];
int read(){
    int x=0;char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
    return x;
}
void insert(int x){
    if (!edge[x].empty()&&edge[x].size()<3&&!vis[x][edge[x].size()])
        vis[x][edge[x].size()]=1,q.push(x);
}
void dfs(int x,int f,int c){
    in[x]=1;
    for (auto [y,z]:adj[x])
        if (y>=lim&&y!=f){
            if (!in[y]) dfs(y,x,c*z);
            else if (y==lim) res+=c*z;
        }
    in[x]=0;
}
int main(){
    n=read();m=read();
    for (int i=1;i<=m;i++){
        int x=read(),y=read();
        edge[x][y]=edge[y][x]=1;
    }
    for (int i=1;i<=n;i++) insert(i);
    while (!q.empty()){
        int x=q.front();q.pop();
        if (edge[x].size()==1){
            auto [y,y_]=*edge[x].begin();
            edge[y].erase(x);insert(y);
        }
        if (edge[x].size()==2){
            auto [y,y_]=*edge[x].begin();
            auto [z,z_]=*edge[x].rbegin();
            const int tmp=y_*z_;
            if (edge[y].count(z)) ans+=tmp*edge[y][z];
            edge[y][z]+=tmp;edge[z][y]+=tmp;
            edge[y].erase(x);edge[z].erase(x);insert(y);insert(z);
        }
        edge[x].clear();
    }
    for (int x=1;x<=n;x++)
        for (auto [y,z]:edge[x])
            adj[x].emplace_back(y,z);
    for (int i=1;i<=n;i++)
        lim=i,dfs(i,0,1);
    printf("%d\n",ans+res/2);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 14ms
memory: 19380kb

input:

100000 100015
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
...

output:

8466

result:

ok single line: '8466'

Test #2:

score: 0
Accepted
time: 18ms
memory: 20176kb

input:

100000 100015
1 2
1 6250
1 12500
1 18750
1 25000
1 31250
1 37500
1 43750
1 50000
1 56250
1 62500
1 68750
1 75000
1 81250
1 87500
1 93750
1 100000
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
3...

output:

136

result:

ok single line: '136'

Test #3:

score: 0
Accepted
time: 12ms
memory: 10012kb

input:

28 42
1 2
1 24
1 25
2 3
2 13
3 4
3 26
4 5
4 22
5 6
5 18
6 7
6 12
7 8
7 28
8 9
8 25
9 10
9 26
10 11
10 21
11 12
11 27
12 13
13 14
14 15
14 20
15 16
15 28
16 17
16 26
17 18
17 27
18 19
19 20
19 25
20 21
21 22
22 23
23 24
23 28
24 27

output:

21192

result:

ok single line: '21192'

Test #4:

score: 0
Accepted
time: 5ms
memory: 9976kb

input:

24 36
1 2
1 13
1 24
2 3
2 9
3 4
3 20
4 5
4 16
5 6
5 12
6 7
6 23
7 8
7 19
8 9
8 15
9 10
10 11
10 22
11 12
11 18
12 13
13 14
14 15
14 21
15 16
16 17
17 18
17 24
18 19
19 20
20 21
21 22
22 23
23 24

output:

5608

result:

ok single line: '5608'

Test #5:

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

input:

100000 100014
1 2
1 99996
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
5...

output:

21192

result:

ok single line: '21192'

Test #6:

score: 0
Accepted
time: 27ms
memory: 19184kb

input:

100000 100012
1 2
1 100000
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
...

output:

5608

result:

ok single line: '5608'

Test #7:

score: 0
Accepted
time: 33ms
memory: 19112kb

input:

100000 100015
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60...

output:

16

result:

ok single line: '16'

Test #8:

score: 0
Accepted
time: 19ms
memory: 20200kb

input:

100000 100015
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60...

output:

5953

result:

ok single line: '5953'

Test #9:

score: 0
Accepted
time: 19ms
memory: 19720kb

input:

100000 100015
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

4969

result:

ok single line: '4969'

Test #10:

score: 0
Accepted
time: 59ms
memory: 20444kb

input:

100000 99999
76235 80241
29221 97585
21008 23980
47740 76939
103 33315
24471 55390
50190 52597
4160 26422
38051 59055
22046 73425
49884 59477
87358 99262
70377 79619
1051 88184
1559 38971
57661 93290
61717 70251
19640 59202
24598 36946
91245 93898
30018 39093
33340 59413
34207 34936
24657 84289
1581...

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 62ms
memory: 20036kb

input:

100000 100000
76175 83403
76446 96179
4526 99780
785 6469
45991 47392
12734 69430
39352 81048
45629 98869
19937 75269
36379 38184
11143 67068
26713 64586
63698 65629
18570 69450
30170 99117
41324 63550
29095 96250
591 67771
16702 91236
4419 7491
12683 20609
7902 83450
4710 15561
27445 71214
21803 54...

output:

1

result:

ok single line: '1'

Test #12:

score: 0
Accepted
time: 67ms
memory: 20192kb

input:

100000 100014
10560 62636
12751 48139
3518 47210
7198 72437
11558 56269
6476 41342
31882 56222
23308 60642
65206 68806
79399 88462
40185 45389
38028 99983
67908 69479
56768 86271
1438 11088
63637 92306
5234 33618
36864 42336
64124 81309
35775 76910
39200 62280
62797 79919
4059 98889
63174 79344
1563...

output:

9574

result:

ok single line: '9574'

Test #13:

score: 0
Accepted
time: 64ms
memory: 18612kb

input:

100000 100015
34213 34365
37444 81370
34711 89230
76048 97476
46275 53814
50715 53668
56085 75519
33360 42763
49901 84889
52500 74201
65352 77631
20778 57637
59913 82386
46135 50269
43567 99690
39314 90866
17720 20755
1156 78278
15811 23432
22662 59301
89733 96071
15592 44539
38656 71656
48274 67371...

output:

13880

result:

ok single line: '13880'

Test #14:

score: 0
Accepted
time: 51ms
memory: 19396kb

input:

100000 100015
35453 35454
38440 38441
63016 63017
68040 68041
92616 92617
9438 9439
15854 15855
16914 16915
23330 23331
1503 1504
26079 26080
67039 67040
91615 91616
44704 44705
45392 45393
85664 85665
86352 86353
8660 8661
21172 21173
44364 44365
56876 56877
74196 74197
86708 86709
89190 89191
1512...

output:

40299

result:

ok single line: '40299'

Test #15:

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

input:

100000 100015
35453 35454
38440 38441
63016 63017
68040 68041
92616 92617
9438 9439
15854 15855
16914 16915
23330 23331
1503 1504
26079 26080
67039 67040
91615 91616
44704 44705
45392 45393
85664 85665
86352 86353
8660 8661
21172 21173
44364 44365
56876 56877
74196 74197
86708 86709
89190 89191
1512...

output:

38007

result:

ok single line: '38007'

Test #16:

score: 0
Accepted
time: 83ms
memory: 19516kb

input:

100000 100015
29872 60881
46491 93664
18423 64670
11098 67861
2187 76456
19105 67629
13831 83309
62390 89327
15185 25247
19168 51473
15002 33282
2871 94190
27514 47637
59737 91371
95741 95879
18935 74896
11443 32220
625 64989
54592 79704
5751 72387
12059 46193
8722 43017
27674 76935
812 13354
43489 ...

output:

41400

result:

ok single line: '41400'

Test #17:

score: 0
Accepted
time: 12ms
memory: 10204kb

input:

28 42
1 2
1 24
1 25
2 3
2 13
3 4
3 26
4 5
4 22
5 6
5 18
6 7
6 12
7 8
7 28
8 9
8 25
9 10
9 26
10 11
10 21
11 12
11 27
12 13
13 14
14 15
14 20
15 16
15 28
16 17
16 26
17 18
17 27
18 19
19 20
19 25
20 21
21 22
22 23
23 24
23 28
24 27

output:

21192

result:

ok single line: '21192'

Test #18:

score: 0
Accepted
time: 67ms
memory: 20216kb

input:

100000 100000
54638 57610
20178 74487
22724 62554
46927 49549
88678 96840
48762 92658
22449 43331
32147 86440
18980 53057
33013 44181
973 86561
50845 53295
17916 97282
14577 66075
1480 2302
62766 99003
51447 61374
26767 35113
20159 44021
68761 79327
12020 47898
54033 58906
12801 31895
18196 34472
76...

output:

1

result:

ok single line: '1'

Test #19:

score: 0
Accepted
time: 64ms
memory: 19340kb

input:

100000 100001
3767 38942
16506 60351
25975 49326
1272 63749
27701 84448
18537 59861
13549 52282
35345 51562
9156 61759
52672 56145
65204 90260
14181 98690
87702 93372
57875 74353
5061 42956
28041 95781
60031 87081
67835 74565
58564 85239
38119 42275
4371 21226
24792 36632
37916 43031
62874 88928
992...

output:

2

result:

ok single line: '2'

Test #20:

score: 0
Accepted
time: 77ms
memory: 20944kb

input:

100000 100014
52710 79701
9196 85887
90411 95739
24476 48417
37889 55914
72461 84979
52203 66233
22364 50711
16734 67249
20227 42834
60562 76542
53151 72246
29491 78822
18319 38266
10261 17501
5593 65701
7184 29654
40812 42796
22859 89967
21747 96105
37464 79579
58667 66601
69790 89764
49195 96446
1...

output:

15

result:

ok single line: '15'

Test #21:

score: 0
Accepted
time: 69ms
memory: 18532kb

input:

100000 100015
59603 89996
12143 27309
11266 53642
68597 77180
73710 76418
13771 77325
11888 11938
87216 87721
88893 90456
20335 22290
5287 8768
12295 18481
56724 67439
44444 92334
65030 99408
20782 36444
19630 63474
26781 92003
75768 88333
31730 94915
82385 91546
63287 97017
58679 71778
87016 87280
...

output:

16

result:

ok single line: '16'

Test #22:

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

input:

3 2
1 2
2 3

output:

0

result:

ok single line: '0'

Test #23:

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

input:

3 3
1 2
2 3
1 3

output:

1

result:

ok single line: '1'

Test #24:

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

input:

4 3
1 2
2 3
3 4

output:

0

result:

ok single line: '0'

Test #25:

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

input:

4 3
1 2
1 3
3 4

output:

0

result:

ok single line: '0'

Test #26:

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

input:

4 3
1 2
1 3
1 4

output:

0

result:

ok single line: '0'

Test #27:

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

input:

100000 100015
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

136

result:

ok single line: '136'