QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#630546#4504. Book ClubjdelaureAC ✓48ms324388kbC113.1kb2024-10-11 19:08:132024-10-11 19:08:14

Judging History

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

  • [2024-10-11 19:08:14]
  • 评测
  • 测评结果:AC
  • 用时:48ms
  • 内存:324388kb
  • [2024-10-11 19:08:13]
  • 提交

answer

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

uint16_t graphAsArray[20002][20002];
//uint16_t graphAsGraph[20002][20002]; 
uint16_t graphNbEdges[20002];
uint16_t path[20002];

int nbPeople;

/*
void sanityCheck() {
    for (int src = 0; src <= nbPeople*2+1; src++) {
        for (int i = 0; i < graphNbEdges[src]; i++) {
            int dst = graphAsArray[src][i];
            if (!graphAsGraph[src][dst]) {
                fprintf(stderr, "what the fuck %d %d\n", src, dst);
                printf("[%i][%i]\n", src, i);
            }
        }
    }
}
*/

uint16_t getEdgeIndex(uint16_t src, uint16_t dst) {
    for (uint16_t i = 0; i < graphNbEdges[src]; i++) {
        if (graphAsArray[src][i] == dst) {
            return i;
        }
    }
    return (uint16_t)-1;
}

void addEdge(uint16_t src, uint16_t dst) {
    if (getEdgeIndex(src, dst) != (uint16_t)-1) return;

    //if (!graphAsGraph[src][dst]) {
        int index = graphNbEdges[src]++;
        //graphAsGraph[src][dst] = index + 1;
        graphAsArray[src][index] = dst;

    //}

    //sanityCheck();
}

void removeEdge(uint16_t src, uint16_t dst) {
    int index, replace;
    uint16_t newDst;

    //if (graphAsGraph[src][dst]) {
        index = getEdgeIndex(src, dst); //graphAsGraph[src][dst] - 1;
        if (index == (uint16_t)-1) return;

        replace = --graphNbEdges[src];
        newDst = graphAsArray[src][replace];

        //graphAsGraph[src][dst] = 0;
        graphAsArray[src][index] = newDst;
        //graphAsGraph[src][newDst] = index + 1;
    //}

    //sanityCheck();
}

bool searchPath(uint16_t source, uint16_t target) {
    if (source == target)
        return true;
    
    for (int i = 0; i < graphNbEdges[source]; i++) {
        uint16_t dst = graphAsArray[source][i];
        if (path[dst] == (uint16_t)-1) {
            path[dst] = source;
            if (searchPath(dst, target))
                return true;
        }
    }

    return false;
}

int fordFulkerson(uint16_t source, uint16_t target) {
    uint16_t u, v;
    int maxFlow = 0;

    for (;;) {
        memset(path, -1, sizeof(path));
        if (!searchPath(source, target))
            break;

        //printf("%i %i\n", source, target);
        
        v = target;
        while (v != source) {
            u = path[v];
            removeEdge(u, v);
            addEdge(v, u);
            v = path[v];
        }

        maxFlow++;
    }

    return maxFlow;
}

int main(void) {
    //freopen("J.in", "r", stdin);

    int nbDeclarations;
    int person, preference;
    scanf("%d %d", &nbPeople, &nbDeclarations);

    for (int i = 0; i < nbDeclarations; i++) {
        scanf("%d %d", &person, &preference);
        addEdge(person*2, preference*2+1);
    }

    for (int i = 0; i < nbPeople; i++) {
        addEdge(nbPeople*2, i*2);
        addEdge(i*2+1, nbPeople*2+1);
    }

    int result = fordFulkerson(nbPeople*2, nbPeople*2+1);
    if (result == nbPeople) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
}

详细

Test #1:

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

input:

5 5
0 1
1 2
2 3
3 4
4 0

output:

YES

result:

ok single line: 'YES'

Test #2:

score: 0
Accepted
time: 8ms
memory: 81580kb

input:

1000 5000
0 271
0 320
0 325
0 378
0 562
0 942
0 957
1 88
1 235
1 271
1 579
1 604
1 751
1 875
1 929
2 70
2 403
2 470
2 909
2 912
3 352
3 657
3 810
4 201
4 361
4 414
4 418
4 505
4 531
4 597
4 833
5 14
5 123
5 281
5 878
5 938
6 311
6 392
6 564
6 994
7 246
7 353
7 372
7 710
7 751
7 772
8 149
8 435
8 682...

output:

NO

result:

ok single line: 'NO'

Test #3:

score: 0
Accepted
time: 36ms
memory: 300416kb

input:

10000 10000
0 3378
1 8751
2 9999
3 7769
4 6001
5 9998
6 9311
7 9996
8 7722
9 1572
10 9994
11 3865
12 186
13 7614
14 9992
15 9991
16 4424
17 9807
18 5031
19 7909
20 4994
21 292
22 9990
23 8180
24 5837
25 9989
26 9554
27 7552
28 8027
29 5135
30 9988
31 4067
32 702
33 9984
34 8831
35 6971
36 7354
37 81...

output:

YES

result:

ok single line: 'YES'

Test #4:

score: 0
Accepted
time: 35ms
memory: 298560kb

input:

10000 10000
0 3378
1 8751
2 9999
3 7769
4 6001
5 9998
6 9311
7 9996
8 7722
9 1572
10 9994
11 3865
12 186
13 7614
14 9992
15 9991
16 4424
17 9807
18 5031
19 7909
20 4994
21 292
22 9990
23 8180
24 5837
25 9989
26 9554
27 7552
28 8027
29 5135
30 9988
31 4067
32 702
33 9984
34 8831
35 6971
36 7354
37 81...

output:

NO

result:

ok single line: 'NO'

Test #5:

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

input:

10000 20000
0 3378
0 6398
1 3271
1 6775
1 8751
2 9999
3 7769
4 1201
4 6001
4 6908
5 9997
6 9311
7 9995
8 2240
8 7722
9 1572
10 9994
11 3865
12 186
12 6999
12 8543
13 6075
13 6124
13 7614
13 8433
14 9986
15 3587
16 1761
16 3977
16 4424
16 5036
16 8714
16 9441
17 2109
17 9807
18 3502
18 5031
19 7909
2...

output:

NO

result:

ok single line: 'NO'

Test #6:

score: 0
Accepted
time: 40ms
memory: 324388kb

input:

10000 20000
0 3378
0 6398
1 3271
1 6775
1 8751
2 9999
3 7769
4 1201
4 6001
4 6908
5 9997
6 9311
7 9995
8 2240
8 7722
9 1572
10 9994
11 3865
12 186
12 6999
12 8543
13 6075
13 6124
13 7614
13 8433
14 9986
15 3587
16 1761
16 3977
16 4424
16 5036
16 8714
16 9441
17 2109
17 9807
18 3502
18 5031
19 7909
2...

output:

YES

result:

ok single line: 'YES'

Test #7:

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

input:

3 4
0 1
1 0
0 2
2 0

output:

NO

result:

ok single line: 'NO'

Test #8:

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

input:

5 4
0 1
1 2
2 3
3 4

output:

NO

result:

ok single line: 'NO'

Test #9:

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

input:

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

output:

YES

result:

ok single line: 'YES'

Test #10:

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

input:

5 12
0 1
0 2
0 3
1 0
1 2
1 3
2 0
2 1
2 3
3 0
3 1
3 2

output:

NO

result:

ok single line: 'NO'

Test #11:

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

input:

5 16
0 1
0 2
0 3
1 0
1 2
1 3
2 0
2 1
2 3
3 0
3 1
3 2
4 0
4 1
4 2
4 3

output:

NO

result:

ok single line: 'NO'

Test #12:

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

input:

5 16
0 1
0 2
0 3
1 0
1 2
1 3
2 0
2 1
2 3
3 0
3 1
3 2
0 4
1 4
2 4
3 4

output:

NO

result:

ok single line: 'NO'

Test #13:

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

input:

1000 1000
0 325
1 998
2 997
3 995
4 833
5 14
6 994
7 993
8 722
9 173
10 480
11 634
12 209
13 757
14 992
15 506
16 224
17 807
18 470
19 991
20 989
21 266
22 58
23 761
24 88
25 388
26 300
27 987
28 27
29 559
30 985
31 117
32 154
33 984
34 764
35 951
36 808
37 537
38 979
39 2
40 977
41 848
42 624
43 75...

output:

YES

result:

ok single line: 'YES'

Test #14:

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

input:

1000 1000
0 325
1 998
2 997
3 995
4 833
5 14
6 994
7 993
8 722
9 173
10 480
11 634
12 209
13 757
14 992
15 506
16 224
17 807
18 470
19 991
20 989
21 266
22 58
23 761
24 88
25 388
25 936
26 300
27 987
28 27
29 559
30 985
31 117
32 154
33 984
34 764
35 951
36 808
37 537
38 979
39 2
40 977
41 848
42 62...

output:

NO

result:

ok single line: 'NO'

Test #15:

score: 0
Accepted
time: 6ms
memory: 81596kb

input:

1000 10000
0 271
0 320
0 325
0 378
0 562
0 569
0 633
0 657
0 728
0 737
0 942
0 957
1 31
1 75
1 88
1 183
1 213
1 235
1 256
1 271
1 579
1 604
1 751
1 869
1 875
1 929
2 70
2 403
2 470
2 909
2 912
2 965
2 998
3 303
3 352
3 641
3 657
3 769
3 810
3 841
3 921
4 1
4 63
4 174
4 201
4 319
4 358
4 361
4 365
4 ...

output:

YES

result:

ok single line: 'YES'